【Android】不依赖焦点和选中的TextView跑马灯
来源:互联网 发布:dbxpa调试软件 编辑:程序博客网 时间:2024/06/11 15:25
声明
欢迎转载,但请保留文章原始出处:)
欢迎转载,但请保留文章原始出处:)
博客园: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替换成自己的判定方法即可。
阅读全文
0 0
- 【Android】不依赖焦点和选中的TextView跑马灯【2】
- 【Android】不依赖焦点和选中的TextView跑马灯【2】
- 【Android】不依赖焦点和选中的TextView跑马灯
- 不依赖焦点和选中的TextView跑马灯
- android: 不需要焦点的TextView跑马灯 MarqueeTextView
- android: 不需要焦点的TextView跑马灯 MarqueeTextView
- android: 不需要焦点的TextView跑马灯 MarqueeTextView
- xamarin android TextView跑马灯效果和EditText焦点冲突的问题
- 无需焦点的跑马灯TextView
- android的textView跑马灯
- Android TextView的跑马灯(不获取焦点情况下也可以跑)
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android:TextView不获取焦点可以实现跑马灯的效果
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android自定义不需要焦点的TextView以实现跑马灯效果
- TextView的跑马灯效果,还有焦点问题
- 不持有焦点的跑马灯效果TextView
- Nexus3.6 window版私服搭建 安装、配置教程
- Android 单例模式 封装 Okhttp + RxJava 网络请求(get和post)
- linux安装软件的几种方法
- java--面向对象综合案例二(图书馆)
- 特斯拉的电池容量是否达到了新标准?---凯利讯半导体
- 【Android】不依赖焦点和选中的TextView跑马灯
- 尚学堂nginx 踩过得坑
- APP校验 Token的生成流程
- uboot中挂载U盘,利用FAT文件系统读写U盘文件
- MySQL创建全文索引
- Retrofit源码解析(四)---ServiceMethod相关分析
- @SuppressLint("NewApi")和@TargetApi()的区别章标题
- 【Web前端开发】Vue+AJAX+Bootstrap-table导致失效问题
- 如何从ubuntu14.04下硬盘安装ubuntu