forked from simtice/AndroidCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarqueeText_my.java
More file actions
executable file
·129 lines (113 loc) · 2.98 KB
/
Copy pathMarqueeText_my.java
File metadata and controls
executable file
·129 lines (113 loc) · 2.98 KB
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
package com.kingter.ynjk.widget;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
/**
* 注意:在布局文件引用本view时,paddingLeft,paddingRigh都必须为0dp android:ellipsize="marquee"
* android:singleLine="true" 这两个属性也要加上
*/
public class MarqueeText extends TextView implements Runnable,OnClickListener {
private int currentScrollX;// 当前滚动的位置
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;
private int screenWidth;
private Handler handler = new Handler();
private Runnable runnable = new Runnable(){
@Override
public void run() {
startScroll();
}
};
public MarqueeText(Context context) {
super(context);
init(context);
// TODO Auto-generated constructor stub
}
private void init(Context context) {
this.setOnClickListener(this);
DisplayMetrics dm = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels; // 屏幕宽(dip,如:320dip)
}
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (!isMeasure) {// 文字宽度只需获取一次就可以了
getTextWidth();
isMeasure = true;
}
if(getScrollX()==0){
this.stopScroll();
}
}
/**
* 获取文字高度
*/
private void getTextWidth() {
Paint paint = this.getPaint();
String str = this.getText().toString();
textWidth = (int) paint.measureText(str);
}
//重写setText 在setText的时候重新计算text的宽度
@Override
public void setText(CharSequence text, BufferType type) {
// TODO Auto-generated method stub
super.setText(text, type);
this.isMeasure = false;
startFor0();
}
@Override
public void run() {
if (isStop) {
return;
}
currentScrollX += 2;// 滚动速度
scrollTo(currentScrollX, 0);
if (getScrollX() >=textWidth) {
scrollTo(-screenWidth, 0);
currentScrollX = -screenWidth;
// return;
}
postDelayed(this, 50);
}
// 开始滚动
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}
// 停止滚动
public void stopScroll() {
isStop = true;
}
// 从头开始滚动
public void startFor0() {
currentScrollX = 0;
startScroll();
}
@Override
public void onClick(View v) {
// stopScroll();
// handler.removeCallbacks(runnable);
// handler.postDelayed(runnable, 5000);
this.startScroll();
}
}