前言
之前有寫一篇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);
}
};
}
/** 是否停止滾動 */
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替換成自己的判定方法即可。