【Android】不依賴焦點和選中的TextView跑馬燈【2】

農民伯伯發表於2014-04-25

 

前言

之前有寫一篇TextView跑馬燈的效果,後來實際專案中有發現新的問題,比如還是無法自動跑,文字超過了顯示區域就擷取的問題,今天換了一種思路來實現,更簡單更好用。 


宣告
歡迎轉載,但請保留文章原始出處:)

部落格園:http://www.cnblogs.com

農民伯伯: http://over140.cnblogs.com 


正文

public class MarqueeTextView extends TextView {

    /** 是否停止滾動 */
    private boolean mStopMarquee;
    private String mText;
    private float mCoordinateX;
    private float mTextWidth;

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

    public void setText(String text) {
        this.mText = text;
        mTextWidth = getPaint().measureText(mText);
        if (mHandler.hasMessages(0))
            mHandler.removeMessages(0);
        mHandler.sendEmptyMessageDelayed(0, 2000);
    }

    @Override
    protected void onAttachedToWindow() {
        mStopMarquee = false;
        if (!StringUtils.isEmpty(mText))
            mHandler.sendEmptyMessageDelayed(0, 2000);
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        mStopMarquee = true;
        if (mHandler.hasMessages(0))
            mHandler.removeMessages(0);
        super.onDetachedFromWindow();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!StringUtils.isEmpty(mText))
            canvas.drawText(mText, mCoordinateX, 15, getPaint());
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                if (Math.abs(mCoordinateX) > (mTextWidth + 100)) {
                    mCoordinateX = 0;
                    invalidate();
                    if (!mStopMarquee) {
                        sendEmptyMessageDelayed(0, 2000);
                    }
                } else {
                    mCoordinateX -= 1;
                    invalidate();
                    if (!mStopMarquee) {
                        sendEmptyMessageDelayed(0, 30);
                    }
                }

                break;
            }
            super.handleMessage(msg);
        }
    };

}

程式碼說明:

1、2000表示延遲2秒開始跑馬燈效果

2、mTextWidth + 100 表示跑出螢幕100畫素再重新開始跑 

3、每30毫秒移動1畫素

4、原理很簡單,就是定時刷,用法很簡單,直接setText就行,和用系統的一樣,但是不能通過設定xml的值來直接跑,這個可以自己修改。

5、注意onDraw時判定一下text是否為空,這裡StringUtils.isEmpty替換成自己的判定方法即可。

   

相關文章