文本的跑马灯,代码如下
1 import android.content.Context;
2 import android.graphics.Canvas;
3 import android.graphics.Paint;
4 import android.os.Parcel;
5 import android.os.Parcelable;
6 import android.util.AttributeSet;
7 import android.view.Display;
8 import android.view.View;
9 import android.view.WindowManager;
10 import android.view.View.OnClickListener;
11 import android.widget.TextView;
12
13 /** *//**
14 *
15 * TODO 单行文本跑马灯控件
16 *
17 * @author tianlu
18 * @version 1.0
19 * Create At : 2010-2-16 下午09:35:03
20 */
21 public class AutoScrollTextView extends TextView implements OnClickListener {
22 public final static String TAG = AutoScrollTextView.class.getSimpleName();
23
24 private float textLength = 0f;//文本长度
25 private float viewWidth = 0f;
26 private float step = 0f;//文字的横坐标
27 private float y = 0f;//文字的纵坐标
28 private float temp_view_plus_text_length = 0.0f;//用于计算的临时变量
29 private float temp_view_plus_two_text_length = 0.0f;//用于计算的临时变量
30 public boolean isStarting = false;//是否开始滚动
31 private Paint paint = null;//绘图样式
32 private String text = "";//文本内容
33
34
35 public AutoScrollTextView(Context context) {
36 super(context);
37 initView();
38 }
39
40 public AutoScrollTextView(Context context, AttributeSet attrs) {
41 super(context, attrs);
42 initView();
43 }
44
45 public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
46 super(context, attrs, defStyle);
47 initView();
48 }
49
50 /** *//**
51 * 初始化控件
52 */
53 private void initView()
54 {
55 setOnClickListener(this);
56 }
57
58 /** *//**
59 * 文本初始化,每次更改文本内容或者文本效果等之后都需要重新初始化一下
60 */
61 public void init(WindowManager windowManager)
62 {
63 paint = getPaint();
64 text = getText().toString();
65 textLength = paint.measureText(text);
66 viewWidth = getWidth();
67 if(viewWidth == 0)
68 {
69 if(windowManager != null)
70 {
71 Display display = windowManager.getDefaultDisplay();
72 viewWidth = display.getWidth();
73 }
74 }
75 step = textLength;
76 temp_view_plus_text_length = viewWidth + textLength;
77 temp_view_plus_two_text_length = viewWidth + textLength * 2;
78 y = getTextSize() + getPaddingTop();
79 }
80
81 @Override
82 public Parcelable onSaveInstanceState()
83 {
84 Parcelable superState = super.onSaveInstanceState();
85 SavedState ss = new SavedState(superState);
86
87 ss.step = step;
88 ss.isStarting = isStarting;
89
90 return ss;
91
92 }
93
94 @Override
95 public void onRestoreInstanceState(Parcelable state)
96 {
97 if (!(state instanceof SavedState)) {
98 super.onRestoreInstanceState(state);
99 return;
100 }
101 SavedState ss = (SavedState)state;
102 super.onRestoreInstanceState(ss.getSuperState());
103
104 step = ss.step;
105 isStarting = ss.isStarting;
106
107 }
108
109 public static class SavedState extends BaseSavedState {
110 public boolean isStarting = false;
111 public float step = 0.0f;
112 SavedState(Parcelable superState) {
113 super(superState);
114 }
115
116 @Override
117 public void writeToParcel(Parcel out, int flags) {
118 super.writeToParcel(out, flags);
119 out.writeBooleanArray(new boolean[]{isStarting});
120 out.writeFloat(step);
121 }
122
123
124 public static final Parcelable.Creator<SavedState> CREATOR
125 = new Parcelable.Creator<SavedState>() {
126
127 public SavedState[] newArray(int size) {
128 return new SavedState[size];
129 }
130
131 @Override
132 public SavedState createFromParcel(Parcel in) {
133 return new SavedState(in);
134 }
135 };
136
137 private SavedState(Parcel in) {
138 super(in);
139 boolean[] b = null;
140 in.readBooleanArray(b);
141 if(b != null && b.length > 0)
142 isStarting = b[0];
143 step = in.readFloat();
144 }
145 }
146
147 /** *//**
148 * 开始滚动
149 */
150 public void startScroll()
151 {
152 isStarting = true;
153 invalidate();
154 }
155
156 /** *//**
157 * 停止滚动
158 */
159 public void stopScroll()
160 {
161 isStarting = false;
162 invalidate();
163 }
164
165
166 @Override
167 public void onDraw(Canvas canvas) {
168 canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
169 if(!isStarting)
170 {
171 return;
172 }
173 step += 0.5;
174 if(step > temp_view_plus_two_text_length)
175 step = textLength;
176 invalidate();
177
178 }
179
180 @Override
181 public void onClick(View v) {
182 if(isStarting)
183 stopScroll();
184 else
185 startScroll();
186
187 }
188
189 }
实现了宽度的判断,文本自动滚动及开始和停止滚动等功能。
在UI xml中的配置如下:
1 <cn.tigertian.ui.AutoScrollTextView android:id="@+id/TextViewNotice"
2 android:layout_height="30px" android:layout_width="fill_parent"
3 android:text="@string/test_notice_1" android:textColor="#000" android:inputType="text"
4 android:background="#EEE" android:textSize="20px"></cn.tigertian.ui.AutoScrollTextView>
在Activity中的使用方法如下:
1 //启动公告滚动条
2 autoScrollTextView = (AutoScrollTextView)findViewById(R.id.TextViewNotice);
3 autoScrollTextView.init(getWindowManager());
4 autoScrollTextView.startScroll();
如果想改变跑马灯的文字内容或者文字效果,则在调用完setText方法之后,需要再调用一下init(WindowManager windowManager)方法,重新进行初始化和相关参数的计算。
浙公网安备 33010602011771号