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.
2626import android .content .Context ;
2727import android .content .res .TypedArray ;
2828import android .graphics .Color ;
29+ import android .graphics .Point ;
2930import android .os .Build ;
3031import android .os .Handler ;
3132import android .os .Looper ;
3233import android .util .AttributeSet ;
3334import android .view .MotionEvent ;
3435import android .view .View ;
36+ import android .view .ViewConfiguration ;
3537import android .view .ViewGroup ;
3638import android .view .WindowManager ;
37- import android .graphics .Point ;
3839
3940import 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