Android中跑馬燈水平滑動AutoScrollTextView

一、先看自定義控件,再來說如何使用

public class AutoScrollTextView extends TextView implements View.OnClickListener {

    public final static String TAG = AutoScrollTextView.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 AutoScrollTextView(Context context) {
        super(context);
        initView();
    }

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

    public AutoScrollTextView(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();
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
        if(!isStarting) {
            return;
        }
        step += 0.5;
        if(step > temp_view_plus_two_text_length)
            step = textLength;
        invalidate();
    }

    @Override
    public void onClick(View v) {
        if(isStarting)
            stopScroll();
        else
            startScroll();

    }
}

二、使用方式

1. 佈局

<com.example.zyt.myautoscrolltextview.utils.AutoScrollTextView
        android:id="@+id/TextViewNotice"
        android:layout_height="50dp"
        android:layout_width="fill_parent"
        android:text="@string/app_name"
        android:textColor="#000"
        android:inputType="text"
        android:background="#EEE"
        android:textSize="20sp" />

2. Activity中啓動跑馬燈

        mTextViewNotice = (AutoScrollTextView) findViewById(R.id.TextViewNotice);
        mTextViewNotice.init(getWindowManager());
        mTextViewNotice.startScroll();

如果想改變跑馬燈的文字內容或者文字效果,則在調用完setText方法之後,需要再調用一下init(WindowManager windowManager)方法,重新進行初始化和相關參數的計算。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章