Android自定义TextView,可实现跑马灯效果,不限文字长短。

很多场景中,我们都会用到TextView,面对如今各种各样的需求,很多人都选择自定义TextView实现各种各样的需求,今天就来实现一个自定义跑马灯的效果。
可实现:不论文字长短,都可有跑马灯效果
可随时暂停跑马灯效果

直接上代码:

public class MyTextView extends TextView implements OnClickListener{
        public final static String TAG = MyTextView.class.getSimpleName();
        private float textLength = 0f;
        private float viewWidth = 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 = "";


        public MyTextView(Context context) {
            super(context);
            initView();
        }

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

        public MyTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            initView();
        }


        private void initView() {
            //点击事件初始化
            setOnClickListener(this);
        }


        public void init(WindowManager windowManager) {
            paint = getPaint();
            text = getText().toString();
            textLength = paint.measureText(text);
            viewWidth = getWidth();
            if(viewWidth == 0)
            {
                if(windowManager != null)
                {
                    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;

        }
        
        public static class SavedState extends BaseSavedState {
            public boolean isStarting = false;
            public 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 Parcelable.Creator<SavedState> CREATOR
                    = new Parcelable.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();
        }

    /**
     * 绘制跑动效果
     * @param canvas
     */
        @Override
        public void onDraw(Canvas canvas) {
            canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
            if(!isStarting)
            {
                return;
            }
            step += 3;//速度控制  可调节快慢
            if(step > temp_view_plus_two_text_length)
                step = textLength;
            invalidate();

        }

    /**
     * 点击就可以暂停  如果不需要可以删除
     * @param v
     */
    @Override
        public void onClick(View v) {
            if(isStarting)
                stopScroll();
            else
                startScroll();
        }

}

可通过onDraw()方法中的step来控制速度

   public void onDraw(Canvas canvas) {
            canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
            if(!isStarting)
            {
                return;
            }
            step += 3;//速度控制  可调节快慢 越大越快
            if(step > temp_view_plus_two_text_length)
                step = textLength;
            invalidate();
        }

启动

	myTextView.setText("哈哈哈哈哈哈哈");
   	myTextView.init(getWindowManager());
    myTextView.startScroll();//启动
    myTextView.stopScroll(); //停止

ok,实现了.

你可能感兴趣的:(Android)