Skip to content

Commit fc4fc04

Browse files
committed
fixed issue #4
1 parent b37ccd0 commit fc4fc04

File tree

4 files changed

+67
-46
lines changed

4 files changed

+67
-46
lines changed

app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ android {
2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
2424
compile 'com.android.support:appcompat-v7:23.+'
25-
//compile 'com.moxun:tagcloudlib:1.1.0'
2625
compile project(path: ':tagcloudlib')
2726
}

app/src/main/java/com/moxun/tagcloud/TextTagsAdapter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.support.annotation.NonNull;
5+
import android.util.Log;
56
import android.view.Gravity;
67
import android.view.View;
78
import android.view.ViewGroup;
@@ -32,12 +33,18 @@ public int getCount() {
3233
}
3334

3435
@Override
35-
public View getView(Context context, int position, ViewGroup parent) {
36+
public View getView(Context context, final int position, ViewGroup parent) {
3637
TextView tv = new TextView(context);
3738
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(100, 100);
3839
tv.setLayoutParams(lp);
3940
tv.setText("No." + position);
4041
tv.setGravity(Gravity.CENTER);
42+
tv.setOnClickListener(new View.OnClickListener() {
43+
@Override
44+
public void onClick(View v) {
45+
Log.e("Click","Tag " + position + " clicked.");
46+
}
47+
});
4148
return tv;
4249
}
4350

tagcloudlib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.github.dcendents.android-maven'
33
apply plugin: 'com.jfrog.bintray'
44

5-
version = "1.1.0"
5+
version = "1.1.1"
66
android {
77
compileSdkVersion 23
88
buildToolsVersion "23.0.1"

tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloudView.java

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
/**
44
* Copyright © 2016 moxun
5-
*
5+
* <p/>
66
* Permission is hereby granted, free of charge, to any person obtaining
77
* a copy of this software and associated documentation files (the “Software”),
88
* to deal in the Software without restriction, including without limitation the
99
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1010
* sell copies of the Software, and to permit persons to whom the Software is
1111
* furnished to do so, subject to the following conditions:
12-
*
12+
* <p/>
1313
* The above copyright notice and this permission notice shall be
1414
* included in all copies or substantial portions of the Software.
15-
*
15+
* <p/>
1616
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
1717
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -26,15 +26,16 @@
2626
import android.content.Context;
2727
import android.content.res.TypedArray;
2828
import android.graphics.Color;
29+
import android.graphics.Point;
2930
import android.os.Build;
3031
import android.os.Handler;
3132
import android.os.Looper;
3233
import android.util.AttributeSet;
3334
import android.view.MotionEvent;
3435
import android.view.View;
36+
import android.view.ViewConfiguration;
3537
import android.view.ViewGroup;
3638
import android.view.WindowManager;
37-
import android.graphics.Point;
3839

3940
import com.moxun.tagcloudlib.R;
4041

@@ -49,7 +50,7 @@ public class TagCloudView extends ViewGroup implements Runnable, TagsAdapter.OnD
4950
private float radius;
5051
private float radiusPercent = 0.9f;
5152

52-
private float[] darkColor = new float[]{1f, 0f, 0f, 1f};//rgba
53+
private float[] darkColor = new float[]{1f, 0f, 0f, 1f};//rgba
5354
private float[] lightColor = new float[]{0.9412f, 0.7686f, 0.2f, 1f};//rgba
5455

5556
public static final int MODE_DISABLE = 0;
@@ -59,7 +60,7 @@ public class TagCloudView extends ViewGroup implements Runnable, TagsAdapter.OnD
5960
private int left, right, top, bottom;
6061

6162
private MarginLayoutParams layoutParams;
62-
private int minSize;
63+
private int minSize;
6364

6465
private boolean isOnTouch = false;
6566
private Handler handler = new Handler(Looper.getMainLooper());
@@ -68,17 +69,17 @@ public class TagCloudView extends ViewGroup implements Runnable, TagsAdapter.OnD
6869

6970
public TagCloudView(Context context) {
7071
super(context);
71-
init(context,null);
72+
init(context, null);
7273
}
7374

7475
public TagCloudView(Context context, AttributeSet attrs) {
7576
super(context, attrs);
76-
init(context,attrs);
77+
init(context, attrs);
7778
}
7879

7980
public TagCloudView(Context context, AttributeSet attrs, int defStyleAttr) {
8081
super(context, attrs, defStyleAttr);
81-
init(context,attrs);
82+
init(context, attrs);
8283
}
8384

8485
private void init(Context context, AttributeSet attrs) {
@@ -90,22 +91,22 @@ private void init(Context context, AttributeSet attrs) {
9091
String m = typedArray.getString(R.styleable.TagCloudView_autoScrollMode);
9192
mode = Integer.valueOf(m);
9293

93-
int light = typedArray.getColor(R.styleable.TagCloudView_lightColor,Color.WHITE);
94+
int light = typedArray.getColor(R.styleable.TagCloudView_lightColor, Color.WHITE);
9495
setLightColor(light);
9596

96-
int dark = typedArray.getColor(R.styleable.TagCloudView_darkColor,Color.BLACK);
97+
int dark = typedArray.getColor(R.styleable.TagCloudView_darkColor, Color.BLACK);
9798
setDarkColor(dark);
9899

99-
float p = typedArray.getFloat(R.styleable.TagCloudView_radiusPercent,radiusPercent);
100+
float p = typedArray.getFloat(R.styleable.TagCloudView_radiusPercent, radiusPercent);
100101
setRadiusPercent(p);
101102

102-
float s = typedArray.getFloat(R.styleable.TagCloudView_scrollSpeed,2f);
103+
float s = typedArray.getFloat(R.styleable.TagCloudView_scrollSpeed, 2f);
103104
setScrollSpeed(s);
104105

105106
typedArray.recycle();
106107
}
107108

108-
WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE);
109+
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
109110
Point point = new Point();
110111
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
111112
wm.getDefaultDisplay().getSize(point);
@@ -115,7 +116,7 @@ private void init(Context context, AttributeSet attrs) {
115116
}
116117
int screenWidth = point.x;
117118
int screenHeight = point.y;
118-
minSize = screenHeight < screenWidth ? screenHeight : screenWidth;
119+
minSize = screenHeight < screenWidth ? screenHeight : screenWidth;
119120
}
120121

121122
public void setAutoScrollMode(int mode) {
@@ -130,21 +131,21 @@ public final void setAdapter(TagsAdapter adapter) {
130131

131132
public void setLightColor(int color) {
132133
float[] argb = new float[4];
133-
argb[3] = Color.alpha(color) /1.0f / 0xff;
134-
argb[0] = Color.red(color) /1.0f / 0xff;
135-
argb[1] = Color.green(color) /1.0f / 0xff;
136-
argb[2] = Color.blue(color) /1.0f / 0xff;
134+
argb[3] = Color.alpha(color) / 1.0f / 0xff;
135+
argb[0] = Color.red(color) / 1.0f / 0xff;
136+
argb[1] = Color.green(color) / 1.0f / 0xff;
137+
argb[2] = Color.blue(color) / 1.0f / 0xff;
137138

138139
lightColor = argb.clone();
139140
onChange();
140141
}
141142

142143
public void setDarkColor(int color) {
143144
float[] argb = new float[4];
144-
argb[3] = Color.alpha(color) /1.0f / 0xff;
145-
argb[0] = Color.red(color) /1.0f / 0xff;
146-
argb[1] = Color.green(color) /1.0f / 0xff;
147-
argb[2] = Color.blue(color) /1.0f / 0xff;
145+
argb[3] = Color.alpha(color) / 1.0f / 0xff;
146+
argb[0] = Color.red(color) / 1.0f / 0xff;
147+
argb[1] = Color.green(color) / 1.0f / 0xff;
148+
argb[2] = Color.blue(color) / 1.0f / 0xff;
148149

149150
darkColor = argb.clone();
150151
onChange();
@@ -202,12 +203,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
202203

203204
measureChildren(widthMode, heightMode);
204205

205-
if(layoutParams == null) {
206+
if (layoutParams == null) {
206207
layoutParams = (MarginLayoutParams) getLayoutParams();
207208
}
208209

209-
int dimensionX = widthMode == MeasureSpec.EXACTLY ? contentWidth : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
210-
int dimensionY = heightMode == MeasureSpec.EXACTLY ? contentHeight : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
210+
int dimensionX = widthMode == MeasureSpec.EXACTLY ? contentWidth : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
211+
int dimensionY = heightMode == MeasureSpec.EXACTLY ? contentHeight : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
211212
setMeasuredDimension(dimensionX, dimensionY);
212213
}
213214

@@ -225,21 +226,21 @@ protected void onDetachedFromWindow() {
225226

226227
@SuppressLint("WrongCall")
227228
private void updateChild() {
228-
onLayout(false, left,top,right,bottom);
229+
onLayout(false, left, top, right, bottom);
229230
}
230231

231232
@Override
232233
protected void onLayout(boolean changed, int l, int t, int r, int b) {
233-
left = l;
234-
right = r;
235-
top = t;
234+
left = l;
235+
right = r;
236+
top = t;
236237
bottom = b;
237-
238+
238239
for (int i = 0; i < getChildCount(); i++) {
239240
View child = getChildAt(i);
240241
if (child.getVisibility() != GONE) {
241242
Tag tag = mTagCloud.get(i);
242-
tagsAdapter.onThemeColorChanged(child,tag.getColor());
243+
tagsAdapter.onThemeColorChanged(child, tag.getColor());
243244
child.setScaleX(tag.getScale());
244245
child.setScaleY(tag.getScale());
245246
int left, top;
@@ -272,32 +273,46 @@ public boolean onTrackballEvent(MotionEvent e) {
272273
return true;
273274
}
274275

276+
@Override
277+
public boolean onInterceptTouchEvent(MotionEvent ev) {
278+
handleTouchEvent(ev);
279+
return false;
280+
}
281+
275282
@Override
276283
public boolean onTouchEvent(MotionEvent e) {
277-
float x = e.getX();
278-
float y = e.getY();
284+
handleTouchEvent(e);
285+
return true;
286+
}
287+
288+
private float downX, downY;
279289

290+
private void handleTouchEvent(MotionEvent e) {
280291
switch (e.getAction()) {
281292
case MotionEvent.ACTION_DOWN:
293+
downX = e.getX();
294+
downY = e.getY();
282295
isOnTouch = true;
283-
break;
284296
case MotionEvent.ACTION_MOVE:
285297
//rotate elements depending on how far the selection point is from center of cloud
286-
float dx = x - centerX;
287-
float dy = y - centerY;
288-
mAngleX = (dy / radius) * tspeed * TOUCH_SCALE_FACTOR;
289-
mAngleY = (-dx / radius) * tspeed * TOUCH_SCALE_FACTOR;
290-
291-
processTouch();
292-
298+
float dx = e.getX() - downX;
299+
float dy = e.getY() - downY;
300+
if (isValidMove(dx, dy)) {
301+
mAngleX = (dy / radius) * tspeed * TOUCH_SCALE_FACTOR;
302+
mAngleY = (-dx / radius) * tspeed * TOUCH_SCALE_FACTOR;
303+
processTouch();
304+
}
293305
break;
294306
case MotionEvent.ACTION_UP:
295307
case MotionEvent.ACTION_CANCEL:
296308
isOnTouch = false;
297309
break;
298310
}
311+
}
299312

300-
return true;
313+
private boolean isValidMove(float dx, float dy) {
314+
int minDistance = ViewConfiguration.get(getContext()).getScaledTouchSlop();
315+
return (Math.abs(dx) > minDistance || Math.abs(dy) > minDistance);
301316
}
302317

303318
private void processTouch() {

0 commit comments

Comments
 (0)