水平滚动文字——HorizonScrollTextView

HorizonScrollTextView介绍

消息滚动.gif

主要功能

1、文字居中滚动。
2、可自定义滚动几遍结束

HorizonScrollTextView相关代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class HorizonScrollTextView extends AppCompatTextView {
    private float textLength = 0f;// 文本长度
    private float step = 0f;// 文字的横坐标
//    private float y = 0f;// 文字的纵坐标
    private float temp_view_plus_text_length = 0.0f;// 用于计算的临时变量
    private float temp_view_plus_two_text_length = 0.0f;// 用于计算的临时变量
    public boolean isStarting = false;// 是否开始滚动
    private Paint paint = null;// 绘图样式
    private String text = "";// 文本内容
    private onTextScrollListener mListener;
    private int mTimes = 1;//次数
    private int mTimesCount;

    public HorizonScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
    }

    public void init(WindowManager windowManager) {
        int color = getTextColors().getColorForState(getDrawableState(), 0);
        paint = getPaint();
        //设置滚动字体颜色
        paint.setColor(color);
        text = getText().toString();
        textLength = paint.measureText(text);
        float viewWidth = getWidth();
        if (viewWidth == 0) {
            if (windowManager != null) {
                DisplayMetrics metrics = new DisplayMetrics();
                windowManager.getDefaultDisplay().getMetrics(metrics);
                viewWidth = metrics.widthPixels;
//                Display display = windowManager.getDefaultDisplay();
//                viewWidth = display.getWidth();
            }
        }
        step = textLength;
        temp_view_plus_text_length = viewWidth + textLength;
        temp_view_plus_two_text_length = viewWidth + textLength * 2;
//        y = getTextSize() + getPaddingTop();
    }

    @Override
    public Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);

        ss.step = step;
        ss.isStarting = isStarting;

        return ss;

    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        if (!(state instanceof SavedState)) {
            super.onRestoreInstanceState(state);
            return;
        }
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());

        step = ss.step;
        isStarting = ss.isStarting;
    }

    private static class SavedState extends BaseSavedState {
        boolean isStarting = false;
        float step = 0.0f;

        SavedState(Parcelable superState) {
            super(superState);
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeBooleanArray(new boolean[]{isStarting});
            out.writeFloat(step);
        }

        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }

            @Override
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }
        };

        private SavedState(Parcel in) {
            super(in);
            boolean[] b = null;
            in.readBooleanArray(b);
            if (b != null && b.length > 0)
                isStarting = b[0];
            step = in.readFloat();
        }
    }

    public void startScroll() {
        isStarting = true;
        invalidate();
    }

    public void stopScroll() {
        isStarting = false;
        invalidate();
    }

    public boolean getHasStarting() {
        return isStarting;
    }

    @Override
    public void onDraw(Canvas canvas) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float fontHeight = fontMetrics.bottom - fontMetrics.top;
        float textBaseY = getHeight() - (getHeight() - fontHeight) / 2 - fontMetrics.bottom;
        canvas.drawText(text, temp_view_plus_text_length - step, textBaseY, paint);
//      canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
        if (!isStarting) {
            return;
        }
        step += 1.5f;// 文字滚动速度。
        if (step > temp_view_plus_two_text_length) {
            step = textLength;
            if (mTimesCount <= mTimes) {
                mListener.onFinish();
            }
            mTimes++;
        } else {
            mListener.onReset(temp_view_plus_text_length - step);
        }
        invalidate();
    }

    public interface onTextScrollListener {
        void onReset(float x);

        void onFinish();
    }

    /**
     * 监听滚动次数并监听滚动结束
     * @param times 滚动次数
     * @param listener 文字滚动监听
     */
    public void setTextScrollListener(int times, onTextScrollListener listener) {
        mTimesCount = times;
        mListener = listener;
    }
}

在xml中引用和textview没有差别

<com.zdy.tv.ui.next.view.HorizonScrollTextView
        android:id="@+id/tv_message_title"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_marginTop="103dp"
        android:background="@mipmap/ic_main_video_notice"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="20dp"
        android:visibility="gone"
        tools:text="消息推送" />

activity中使用的主要代码为

mTxtNotice.init(getWindowManager());
mTxtNotice.startScroll();//mTxtNotice为HorizonScrollTextView实例

监听滚动部分:

 mTxtNotice.setTextScrollListener(1, new HorizonScrollTextView.onTextScrollListener() {
            @Override
            public void onReset(float x) {
                //滚动中
            }

            @Override
            public void onFinish() {
                //滚动结束
                mTxtNotice.stopScroll();
            }
        });

总结

   至此HorizonScrollTextView的使用就结束了,自测了使用效果,暂时还没发现有什么问题存在。
   其实主要代码是出自一个大神之手,我主要还是做了修改而已,比如字体居中,滚动监听等。大家也可以在这个基础上继续修改。
   本人还是在学习阶段,这个部分对于我自己来说还是不错的。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,322评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,721评论 0 17
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,204评论 4 61
  • 育儿的路上,有许多与先生不同的观念。最近这段时间就抱与不抱产生了几次争执,我的观念是亲密育儿,就是给予宝贝足够的安...
    小米粒妈妈阅读 1,917评论 0 0
  • 整理东西发现了刚上班的一只日记本,才发现曾经的自己是那么的志高远大,竟然摘录了那么多的文字,还有英语,哦买噶,现在...
    紫狸阅读 3,196评论 0 1