首页 > 代码库 > android 自定义文字跑马灯 支持拖拽,按住停止滚动,自定义速度

android 自定义文字跑马灯 支持拖拽,按住停止滚动,自定义速度

android的textview自带跑马灯效果,一般使用足够了。不过也有不一般的情况,所以我实现了一个自定义textview控件,用来针对这种不一般情况下的跑马灯效果实现。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;
 
/**
 * 自定义跑马灯文本框,支持拖拽查看文本内容,点击暂停文字 先设置要显示文本,然后调用Start()方法运行跑马灯
 *
 * @author sy
 */
public class MarqueeTextView extends TextView implements Runnable,
        OnTouchListener {
    public MarqueeTextView(Context context) {
        super(context);
    }
 
    /** 是否停止滚动 */
    private boolean mStopMarquee;
    private String mText;
    public int mCoordinateX;
    int xOffset;
    private int mTextWidth;
    GestureDetector gestureDetector;
 
    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    /**
     * 开始滚动
     *
     * @param text
     */
    public void Start() {
        this.setOnTouchListener(this);
 
        gestureDetector = new GestureDetector(getContext(),
                new OnGestureListener() {
                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        // TODO Auto-generated method stub
                        return false;
                    }
 
                    @Override
                    public void onShowPress(MotionEvent e) {
                        // TODO Auto-generated method stub
 
                    }
 
                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                            float distanceX, float distanceY) {
                        mCoordinateX += (int) distanceX;
                        scrollTo(mCoordinateX, 0);
                        // TODO:设置偏移量,distanceX为滑动距离
                        return true;
                    }
 
                    @Override
                    public void onLongPress(MotionEvent e) {
                        // TODO Auto-generated method stub
 
                    }
 
                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2,
                            float velocityX, float velocityY) {
                        return false;
                    }
 
                    @Override
                    public boolean onDown(MotionEvent e) {
                        // TODO Auto-generated method stub
                        return false;
                    }
                });
 
        xOffset = 0;
        mStopMarquee = false;
        mText = this.getText().toString();// 获取文本框文本
        mCoordinateX = 0;
        mTextWidth = (int) Math.abs(getPaint().measureText(mText));
        post(this);
    }
 
    @Override
    public void run() {
 
        if (!mStopMarquee) {
            mCoordinateX += 3;// 滚动速度
            scrollTo(mCoordinateX, 0);
            if (mCoordinateX > mTextWidth) {
                scrollTo(0, 0);
                mCoordinateX = 0;
            }
            postDelayed(this, 50);
        }
 
    }
 
    // 继续滚动
    public void Continue() {
        if (mStopMarquee) {
            mStopMarquee = false;
            post(this);
        }
    }
 
    // 暂停滚动
    public void Paush() {
        mStopMarquee = true;
    }
 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_SCROLL:
            Paush();
            break;
        default:
            Continue();
            break;
        }
 
        gestureDetector.onTouchEvent(event);
        return true;
    }
 
}

  首先调用setText设置文本内容,然后调用start()开始滚动。滚动速度为每50毫米移动3像素,这是我调试之后感觉比较平滑的一个速度。如果代码有什么错误或者可以改进的地方,希望你们能在评论中指出。谢谢!