-
Notifications
You must be signed in to change notification settings - Fork 290
/
TagCloudView.java
executable file
·339 lines (281 loc) · 11 KB
/
TagCloudView.java
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.moxun.tagcloudlib.view;
/**
* Copyright © 2016 moxun
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the “Software”),
* to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.graphics.Point;
import com.moxun.tagcloudlib.R;
public class TagCloudView extends ViewGroup implements Runnable, TagsAdapter.OnDataSetChangeListener {
private final float TOUCH_SCALE_FACTOR = .8f;
private final float TRACKBALL_SCALE_FACTOR = 10;
private float tspeed = 2f;
private TagCloud mTagCloud;
private float mAngleX = 0.5f;
private float mAngleY = 0.5f;
private float centerX, centerY;
private float radius;
private float radiusPercent = 0.9f;
private float[] darkColor = new float[]{1f, 0f, 0f, 1f};//rgba
private float[] lightColor = new float[]{0.9412f, 0.7686f, 0.2f, 1f};//rgba
public static final int MODE_DISABLE = 0;
public static final int MODE_DECELERATE = 1;
public static final int MODE_UNIFORM = 2;
public int mode;
private int left, right, top, bottom;
private MarginLayoutParams layoutParams;
private int minSize;
private boolean isOnTouch = false;
private Handler handler = new Handler(Looper.getMainLooper());
private TagsAdapter tagsAdapter;
public TagCloudView(Context context) {
super(context);
init(context,null);
}
public TagCloudView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
public TagCloudView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
setFocusableInTouchMode(true);
mTagCloud = new TagCloud();
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TagCloudView);
String m = typedArray.getString(R.styleable.TagCloudView_autoScrollMode);
mode = Integer.valueOf(m);
int light = typedArray.getColor(R.styleable.TagCloudView_lightColor,Color.WHITE);
setLightColor(light);
int dark = typedArray.getColor(R.styleable.TagCloudView_darkColor,Color.BLACK);
setDarkColor(dark);
float p = typedArray.getFloat(R.styleable.TagCloudView_radiusPercent,radiusPercent);
setRadiusPercent(p);
float s = typedArray.getFloat(R.styleable.TagCloudView_scrollSpeed,2f);
setScrollSpeed(s);
typedArray.recycle();
}
WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE);
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
wm.getDefaultDisplay().getSize(point);
} else {
point.x = wm.getDefaultDisplay().getWidth();
point.y = wm.getDefaultDisplay().getHeight();
}
int screenWidth = point.x;
int screenHeight = point.y;
minSize = screenHeight < screenWidth ? screenHeight : screenWidth;
}
public void setAutoScrollMode(int mode) {
this.mode = mode;
}
public final void setAdapter(TagsAdapter adapter) {
tagsAdapter = adapter;
tagsAdapter.setOnDataSetChangeListener(this);
onChange();
}
public void setLightColor(int color) {
float[] argb = new float[4];
argb[3] = Color.alpha(color) /1.0f / 0xff;
argb[0] = Color.red(color) /1.0f / 0xff;
argb[1] = Color.green(color) /1.0f / 0xff;
argb[2] = Color.blue(color) /1.0f / 0xff;
lightColor = argb.clone();
onChange();
}
public void setDarkColor(int color) {
float[] argb = new float[4];
argb[3] = Color.alpha(color) /1.0f / 0xff;
argb[0] = Color.red(color) /1.0f / 0xff;
argb[1] = Color.green(color) /1.0f / 0xff;
argb[2] = Color.blue(color) /1.0f / 0xff;
darkColor = argb.clone();
onChange();
}
public void setRadiusPercent(float percent) {
if (percent > 1f || percent < 0f) {
throw new IllegalArgumentException("percent value not in range 0 to 1");
} else {
radiusPercent = percent;
onChange();
}
}
private void initFromAdapter() {
this.postDelayed(new Runnable() {
@Override
public void run() {
centerX = (getRight() - getLeft()) / 2;
centerY = (getBottom() - getTop()) / 2;
radius = Math.min(centerX * radiusPercent, centerY * radiusPercent);
mTagCloud.setRadius((int) radius);
mTagCloud.setTagColorLight(lightColor);//higher color
mTagCloud.setTagColorDark(darkColor);//lower color
mTagCloud.clear();
removeAllViews();
for (int i = 0; i < tagsAdapter.getCount(); i++) {
TagCloudView.this.mTagCloud.add(new Tag(tagsAdapter.getPopularity(i)));
addView(tagsAdapter.getView(getContext(), i, TagCloudView.this));
}
mTagCloud.create(true);
mTagCloud.setAngleX(mAngleX);
mTagCloud.setAngleY(mAngleY);
mTagCloud.update();
}
}, 0);
}
public void setScrollSpeed(float scrollSpeed) {
tspeed = scrollSpeed;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
int contentHeight = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
measureChildren(widthMode, heightMode);
if(layoutParams == null) {
layoutParams = (MarginLayoutParams) getLayoutParams();
}
int dimensionX = widthMode == MeasureSpec.EXACTLY ? contentWidth : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
int dimensionY = heightMode == MeasureSpec.EXACTLY ? contentHeight : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
setMeasuredDimension(dimensionX, dimensionY);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(this);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacksAndMessages(null);
}
@SuppressLint("WrongCall")
private void updateChild() {
onLayout(false, left,top,right,bottom);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
left = l;
right = r;
top = t;
bottom = b;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
Tag tag = mTagCloud.get(i);
tagsAdapter.onThemeColorChanged(child,tag.getColor());
child.setScaleX(tag.getScale());
child.setScaleY(tag.getScale());
int left, top;
left = (int) (centerX + tag.getLoc2DX()) - child.getMeasuredWidth() / 2;
top = (int) (centerY + tag.getLoc2DY()) - child.getMeasuredHeight() / 2;
child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());
}
}
}
public void reset() {
mTagCloud.reset();
updateChild();
}
@Override
public boolean onTrackballEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
mAngleX = (y) * tspeed * TRACKBALL_SCALE_FACTOR;
mAngleY = (-x) * tspeed * TRACKBALL_SCALE_FACTOR;
mTagCloud.setAngleX(mAngleX);
mTagCloud.setAngleY(mAngleY);
mTagCloud.update();
updateChild();
return true;
}
@Override
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
isOnTouch = true;
break;
case MotionEvent.ACTION_MOVE:
//rotate elements depending on how far the selection point is from center of cloud
float dx = x - centerX;
float dy = y - centerY;
mAngleX = (dy / radius) * tspeed * TOUCH_SCALE_FACTOR;
mAngleY = (-dx / radius) * tspeed * TOUCH_SCALE_FACTOR;
processTouch();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
isOnTouch = false;
break;
}
return true;
}
private void processTouch() {
if (mTagCloud != null) {
mTagCloud.setAngleX(mAngleX);
mTagCloud.setAngleY(mAngleY);
mTagCloud.update();
}
updateChild();
}
@Override
public void onChange() {
initFromAdapter();
}
@Override
public void run() {
if (!isOnTouch && mode != MODE_DISABLE) {
if (mode == MODE_DECELERATE) {
if (mAngleX > 0.04f) {
mAngleX -= 0.02f;
}
if (mAngleY > 0.04f) {
mAngleY -= 0.02f;
}
if (mAngleX < -0.04f) {
mAngleX += 0.02f;
}
if (mAngleY < 0.04f) {
mAngleY += 0.02f;
}
}
processTouch();
}
handler.postDelayed(this, 50);
}
}