android TextView实现实现跑马灯效果,并监听滚动完成动作。
2014-07-17 20:07
405 查看
因项目需求,需要实现监听跑马灯效果滚动完成的动作,因此继承textview,添加监听滚动完成动作的监听器。
package com.example.textviewtest; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.ViewDebug.ExportedProperty; import android.widget.TextView; public class MarqueeTextView extends TextView implements Runnable { private static final int MARQUEE_DELAY = 1200; private static final int MARQUEE_RESTART_DELAY = 10; private int currentScrollX; private boolean isStop = false; private int textWidth; private boolean isMeasure = false; private int mMarqueeRepeatLimit = 1; private int mMarqueeVelocity = 2; private OnMarqueeCompleteListener marqueeCompleteListener; public MarqueeTextView(Context context) { this(context, null); } public MarqueeTextView(Context context, AttributeSet attrs) { this(context, attrs,0); } public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mMarqueeRepeatLimit = getMarqueeRepeatLimit(); } interface OnMarqueeCompleteListener { void onMarqueeComplete(); } public OnMarqueeCompleteListener getMarqueeCompleteListener() { return marqueeCompleteListener; } public void setOnMarqueeCompleteListener( OnMarqueeCompleteListener marqueeCompleteListener) { this.marqueeCompleteListener = marqueeCompleteListener; } /** * 获取文字滚动的速度,每秒移动的像素 * @return */ public int getMarqueeVelocity() { return mMarqueeVelocity; } /** * 设置文字的滚动的速度 * @param mMarqueeVelocity 每秒移动的像素 */ public void setMarqueeVelocity(int velocity) { this.mMarqueeVelocity = velocity ; } @Override @ExportedProperty(category = "focus") public boolean isFocused() { return false; } @Override @ExportedProperty public boolean isSelected() { return false; } @Override public void setSelected(boolean selected) { super.setSelected(selected); startStopMarquee(selected); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); startStopMarquee(focused); } public void startStopMarquee(boolean bool){ if(bool){ startScroll(); }else{ stopScroll(); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!isMeasure) { textWidth = getTextWidth(); isMeasure = true; } } /** * 获取文字宽度 */ private int getTextWidth() { Paint paint = this.getPaint(); String str = this.getText().toString(); return (int) paint.measureText(str); } @Override public void run() { if(textWidth <= getWidth()){ if(null != marqueeCompleteListener){ marqueeCompleteListener.onMarqueeComplete(); } return; } if (isStop) { currentScrollX = 0; scrollTo(currentScrollX, 0); return; } currentScrollX += mMarqueeVelocity; scrollTo(currentScrollX, 0); if (textWidth != 0 && getScrollX() >= textWidth) { mMarqueeRepeatLimit--; if(mMarqueeRepeatLimit <= 0){ if(null != marqueeCompleteListener){ marqueeCompleteListener.onMarqueeComplete(); } return; } currentScrollX = -getWidth(); } postDelayed(this, MARQUEE_RESTART_DELAY); } /** * 开始滚动 */ private void startScroll() { isStop = false; this.removeCallbacks(this); this.invalidate(); currentScrollX = 0; postDelayed(this, MARQUEE_DELAY); } /** * 停止滚动 */ private void stopScroll() { isStop = true; } /** * 从头开始滚动 */ public void startFor0() { currentScrollX = 0; startScroll(); } }
相关文章推荐
- Android之如何用TextView实现滚动字幕的效果【跑马灯】
- Android中TextView实现文字跑马灯效果(滚动轮播)
- Android TextView实现垂直滚动的跑马灯效果
- 一分钟实现 Android textview 跑马灯文字滚动效果
- Android中TextView实现垂直滚动(轮换效果,非跑马灯)
- Android自定义textview实现竖直滚动跑马灯效果
- Android TextView实现滚动跑马灯效果Marquee
- android TextView的跑马灯效果的实现
- Android TextView 跑马灯效果的实现
- android TextView不用获得焦点实现跑马灯效果 两种方法
- Android实现多个TextView同时显示跑马灯效果
- Android开发之TextView文字水平滚动效果实现
- Android之TextView内容实现跑马灯效果
- android用TextView实现跑马灯效果
- Android常用UI编程_TextView实现跑马灯效果
- android在Gridview实现Textview跑马灯效果
- android用TextView实现跑马灯效果
- android TextView实现跑马灯效果
- Android对ScorllView滚动监听,实现美团、大众点评的购买悬浮效果
- Android开发 TextView实现跑马灯效果