textview跑马灯效果
来源:互联网 发布:2013网络购物交易额 编辑:程序博客网 时间:2024/05/18 14:22
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);
}
};
}
0 0
- TextView跑马灯效果
- TextView跑马灯效果
- TextView跑马灯效果
- TextView跑马灯效果
- TextView跑马灯效果
- TextView跑马灯效果
- TextView 跑马灯效果
- TextView 跑马灯效果
- TextView跑马灯效果
- TextView跑马灯效果
- textview跑马灯效果
- textview跑马灯效果
- TextView跑马灯效果
- TextView跑马灯效果
- TextView跑马灯效果
- TextView跑马灯效果
- textview跑马灯效果
- TextView跑马灯效果
- Spring 学习笔记-- Spring ContextLoaderListener 解析
- AndroidStudio开发技巧集锦
- 排序算法--睡眠排序
- 锡拉库萨中文离线地图App上线
- 简单了解了一下 Java线程池 ThreadPoolExecutor
- textview跑马灯效果
- 第三章 函数与程序结构
- javaEE---web 开发
- java,小程序练习while和do while,猜数字
- [Effective Java]第九章 异常
- Linux中的文件描述符与打开文件之间的关系 :http://blog.csdn.net/cywosp/article/details/38965239
- jsoup初识
- js的alert()使用注意
- 快速Android开发系列通信篇之EventBus