【Android】不依赖焦点和选中的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替换成自己的判定方法即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值