先上效果图:
代码:
复制代码
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280package com.example.scrolltextview; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Paint; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.Scroller; import android.widget.SpinnerAdapter; import android.widget.TextView; public class ScrollTextView extends ViewGroup { private static final int SCROLL = 0; private static final int JUSTIFY = 1; private static final int ANIMATION_DURATION = 3000; private int mOrientation; private int mSize; private int mIndex; private int mPosition; private Scroller mScroller; private Map<View, Integer> mViews; private SpinnerAdapter mAdapter; private int mPackedViews; private int mAnimationDuration; private float measuredWidth = 0; private float textSize = 0; private float measuredHeight = 0; private String text= null; private String[] data; private boolean flag = true; public ScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); int[] linerarLayoutAttrs = { android.R.attr.orientation, }; TypedArray a = context.obtainStyledAttributes(attrs, linerarLayoutAttrs); mOrientation = a.getInteger(0, LinearLayout.HORIZONTAL); a.recycle(); mAnimationDuration = ANIMATION_DURATION; mScroller = new Scroller(context); mIndex = -1; mPosition = -1; mPackedViews = -1; mViews = new HashMap<View, Integer>(); setFocusable(true); setFocusableInTouchMode(true); } public float getCharacterWidth(String text, float size){ if(null == text || "".equals(text)) return 0; float width = 0; Paint paint = new Paint(); paint.setTextSize(size); float text_width = paint.measureText(text);//得到总体长度 width = text_width/text.length();//字符的长度 return width; } public void setText(String text) { this.text = text; data = new String[]{this.text}; LayoutInflater inflater= (LayoutInflater) getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE); TextView view = (TextView) inflater.inflate(R.layout.scroll_text_view, null); textSize = view.getTextSize(); measuredHeight = textSize; textSize = getCharacterWidth(text,textSize); } public void beginScroll() { thread.start(); } Thread thread = new Thread(new Runnable() { boolean enabled = true; @Override public void run() { // TODO Auto-generated method stub while(enabled) { try { Thread.sleep(4000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } setNextView(); enabled = mIndex + 1 < mAdapter.getCount(); } } }); @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measuredWidth = getMeasuredWidth(); mSize = mOrientation == LinearLayout.HORIZONTAL? getMeasuredWidth() : getMeasuredHeight(); if(flag && text!=null) { int length = text.length(); Log.i("AAA", "length*textSize:"+length*textSize+" measuredWidth:"+measuredWidth); if(length*textSize >= measuredWidth) { int textCountInOneLine = (int) (measuredWidth/textSize -1); int lineCount =(int) ((length%textCountInOneLine==0)? (length/textCountInOneLine):(length/textCountInOneLine+1)); Log.i("AAA", "length:"+length+" textCountInOneLine:"+textCountInOneLine+" lineCount:"+lineCount); data = new String[lineCount]; for(int i=0;i<lineCount;i++) { if((i+1)*textCountInOneLine<length) data[i] = text.substring(i*textCountInOneLine, (i+1)*textCountInOneLine); else data[i] = text.substring(i*textCountInOneLine, length); Log.i("AAA", "data:"+data[i]); } } else { data = new String[]{text}; } ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.scroll_text_view, data); setAdapter(adapter); flag = false; } View view = getChildAt(0); if(view!=null) { measureChild(view, widthMeasureSpec, widthMeasureSpec); measuredHeight = view.getMeasuredHeight(); } setMeasuredDimension((int)measuredWidth, (int)measuredHeight); } private int getPackedViews(int offset) { int size = mSize; int start = offset / size; int numViews = offset % size != 0? 1 : 0; return start << 1 | numViews; } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { mScroller.computeScrollOffset(); int currX = mScroller.getCurrX(); int delta = mPosition - currX; mPosition = currX; int packed = getPackedViews(mPosition); manageViews(packed); scroll(delta); if (!mScroller.isFinished()) { handler.sendEmptyMessage(msg.what); } else { if (msg.what == SCROLL) { justify(); } else { mIndex = mPosition / mSize; } } } }; private void justify() { int offset = mPosition % mSize; if (offset != 0) { int endPosition = mPosition - offset; if (offset > mSize / 2) { endPosition += mSize; } mScroller.startScroll(mPosition, 0, endPosition - mPosition, 0, mAnimationDuration); handler.sendEmptyMessage(JUSTIFY); } else { mIndex = mPosition / mSize; } } private void scroll(int offset) { if (mOrientation == LinearLayout.HORIZONTAL) { for (View view : mViews.keySet()) { view.offsetLeftAndRight(offset); } } else { for (View view : mViews.keySet()) { view.offsetTopAndBottom(offset); } } invalidate(); } public void setSelection(int index, boolean animate) { if (index == mIndex) { return; } int endPosition = index * mSize; int diff = Math.abs(index - mIndex); int sign = index > mIndex? 1 : -1; mIndex = index; if (diff > 1) { mPosition = endPosition - sign * mSize; } if (animate) { mScroller.startScroll(mPosition, 0, endPosition - mPosition, 0, mAnimationDuration); handler.removeMessages(JUSTIFY); handler.removeMessages(SCROLL); handler.sendEmptyMessage(JUSTIFY); } else { mPosition = endPosition; manageViews(index << 1); invalidate(); } } private void manageViews(int packedViews) { if (packedViews == mPackedViews) { return; } mPackedViews = packedViews; int startIdx = packedViews >> 1; int endIdx = startIdx + (packedViews & 1); int viewIdx = startIdx; while (viewIdx <= endIdx) { if (!mViews.containsValue(viewIdx)) { if (viewIdx >= 0 && viewIdx < mAdapter.getCount()) { View view = mAdapter.getView(viewIdx, null, this); mViews.put(view, viewIdx); addView(view); } } viewIdx++; } // remove not visible views Iterator<View> iterator = mViews.keySet().iterator(); while (iterator.hasNext()) { View view = iterator.next(); int idx = mViews.get(view); if (idx < startIdx || idx > endIdx) { iterator.remove(); removeView(view); } } } public int getSelection() { return mIndex; } public void setPreviousView() { if (mAdapter != null && mIndex > 0) { setSelection(mIndex-1, true); } } public void setNextView() { if (mAdapter != null && mIndex + 1 < mAdapter.getCount()) { setSelection(mIndex+1, true); } } public void setAdapter(SpinnerAdapter adapter) { mAdapter = adapter; if (mAdapter != null) { setSelection(0, false); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { for (View view : mViews.keySet()) { if (view.getWidth() == 0) { // new View: not layout()ed int idx = mViews.get(view); if (mOrientation == LinearLayout.HORIZONTAL) { int left = mSize * idx - mPosition; view.layout(left, 0, left+r-l, b-t); } else { int top = mSize * idx - mPosition; view.layout(0, top, r-l, top+b-t); } } } } }
使用:
复制代码
1
2
3
4String temp = getResources().getString(R.string.info); ScrollTextView switcher1 = (ScrollTextView) findViewById(R.id.switcher1); switcher1.setText(temp); switcher1.beginScroll();
唉,我觉得我真是懒....
还是直接给工程吧!
源码下载
最后
以上就是无奈大船最近收集整理的关于ViewGroup实现可以上下、左右滚动跑马灯效果的全部内容,更多相关ViewGroup实现可以上下、左右滚动跑马灯效果内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复