TextView跑马灯效果-自定义控件
来源:互联网 发布:hugo这家淘宝店正吗 编辑:程序博客网 时间:2024/03/28 22:17
工程目录
AutoScrollTextView自定义的 TextView
主要文件
AutoScrollTextView.java
public class AutoScrollTextView extends TextView{ private Paint paint = null;//绘图样式 private float moveSpeed = 2f;//移动的速度,速度设置:1-10 private float textLength = 0f;//文本长度 private float viewWidth = 0f;//AutoScrollTextView控件的宽度 private float step = 0f, y = 0f;//文字的横坐标;文字的纵坐标 private float temp_view_plus_text_length = 0.0f;//TextView的长度+TextView文本的长度 private float temp_view_plus_two_text_length = 0.0f;//TextView的长度+TextView文本的长度*2 private boolean isStarting = false;//是否开始滚动 private String text = "";//文本内容 private OnMoveStatusListener onMoveStatusListener = null; public AutoScrollTextView(Context context) { super(context); } public AutoScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); } public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void init(WindowManager windowManager) { paint = getPaint(); paint.setColor(this.getCurrentTextColor());//文本颜色 text = getText().toString(); textLength = paint.measureText(text); //measure()方法获取text的长度 viewWidth = getWidth(); if (viewWidth == 0 && windowManager != null) { Display display = windowManager.getDefaultDisplay(); viewWidth = display.getWidth(); } step = viewWidth + textLength; temp_view_plus_text_length = viewWidth + textLength; temp_view_plus_two_text_length = viewWidth + textLength * 2; y = getTextSize() + getPaddingTop(); } /** * 覆写TextView的onDraw()方法,实现文本滚动显示的效果 * * @param canvas */ @Override public void onDraw(Canvas canvas) { /** * Draw the text, with origin at (x,y), using the specified paint. The * origin is interpreted based on the Align setting in the paint. * @param text The text to be drawn 要显示的文本 * @param x The step-coordinate of the origin of the text being drawn 文本显示的x坐标,TextView的最左端的x坐标是0,最右端的x坐标是TextView.getWidth(); * @param y The y-coordinate of the origin of the text being drawn 文本显示的y坐标,TextView的顶端所在的y坐标。 * @param paint The paint used for the text (e.g. color, size, style) 描绘文本的画笔 */ canvas.drawText(text, temp_view_plus_text_length - step, y, paint); if (!isStarting) { return; } step += moveSpeed;//速度设置:1-10 if (step > temp_view_plus_two_text_length) { step = textLength; if(null != onMoveStatusListener){//有文字移动到最后的监听 onMoveStatusListener.onMoveEnded(); stopScroll(); } } invalidate(); } /** * 开始滚动 */ public void startScroll() { isStarting = true; invalidate(); } /** * 停止滚动 */ public void stopScroll() { isStarting = false; invalidate(); } /** * 设置移动到最后的监听类 * @param onMoveStatusListener */ public void setOnMoveStatusListener(OnMoveStatusListener onMoveStatusListener) { this.onMoveStatusListener = onMoveStatusListener; } /** * 文字滑动的监听类 */ public interface OnMoveStatusListener { /** * 最后一个文字移出控件区域 */ public void onMoveEnded(); }}
如果用了SpannableStringBuilder则需要修改init()方法中的代码
如果需要循环指定次数,则需要添加一个控制的变量。实现比较简单这里就不写了
MainActivity.java
public class MainActivity extends Activity { //声明一系列的布局控件 private AutoScrollTextView autoScrollTextView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); //根据ID获取自定义AutoScrollTextView类的对象 autoScrollTextView = (AutoScrollTextView) findViewById(R.id.TextViewNotice); autoScrollTextView.init(getWindowManager()); autoScrollTextView.setOnMoveStatusListener(new AutoScrollTextView.OnMoveStatusListener() { @Override public void onMoveEnded() { Toast.makeText(MainActivity.this, "移到最后了", Toast.LENGTH_SHORT).show(); } }); autoScrollTextView.startScroll(); }}
autoScrollTextView.setOnMoveStatusListener不设置监听,则无限次循环;设置监听则只滚动一次
也可代码调用,
autoScrollTextView.startScroll();autoScrollTextView.stopScroll();
实现开始,停止滚动
运行效果
下载地址
https://github.com/407365100/TextScrollDemo.git
参考文档地址:
http://blog.csdn.net/cao_tao199612/article/details/7475682
1 0
- TextView跑马灯效果-自定义控件
- TextView自定义跑马灯效果
- 自定义TextView跑马灯效果
- android开发自定义控件之TextView跑马灯效果
- Android TextView控件跑马灯效果
- Android控件之TextView跑马灯效果
- TextView控件之跑马灯效果
- Android控件TextView的跑马灯效果
- TextView控件之 跑马灯效果
- 自定义TextView实现跑马灯效果
- 自定义TextView实现跑马灯效果
- 自定义TextView跑马灯效果实例教程
- 自定义TextView实现跑马灯效果
- android 自定义textview跑马灯效果
- 自定义TextView实现跑马灯效果
- Android自定义TextView实现跑马灯效果
- Android 自定义textview实现跑马灯效果
- 自定义控件:滚动的TextView跑马灯
- Mac环境下配置Hadoop-2.6.2版本
- 2016年新鲜出炉的20款网页开发工具推荐
- golang语法总结(十):关于++和--
- iOS UIImage渲染模式 imageWithRenderingMode:
- 重用convertView时防止图片不断刷新
- TextView跑马灯效果-自定义控件
- Java中float的取值范围为什么是那么大?
- 抽象类的概述和讲解
- 向量叉积的几何意义
- File空格的处理方法
- Android HOOK工具Cydia Substrate使用详解
- 读一读Android系统源代码 一:下载源码、编译framework模块
- Rabbit实例代码
- iScroll4 滚动到底部检测