admin
2021-04-08 a8fad18ea31ad23c98fd77174c0cdd4bad0de8ea
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
package com.lcjian.library.widget;
 
import java.util.Arrays;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
 
public class TagCloudLayout extends ViewGroup {
 
    private int radius;
 
    private double mAngleX;
    private double mAngleY;
    private double mAngleZ;
    
    private double sin_mAngleX;
    private double cos_mAngleX;
    private double sin_mAngleY;
    private double cos_mAngleY;
    private double sin_mAngleZ;
    private double cos_mAngleZ;
    
    private TagView[] mcList;
    
    private int mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    
    private OnScrollListener mOnScrollListener;
    
    private int mScrollState;
    
    public OnScrollListener getOnScrollListener() {
        return mOnScrollListener;
    }
 
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.mOnScrollListener = onScrollListener;
    }
 
    public interface OnScrollListener {
 
        public static int SCROLL_STATE_IDLE = 0;
 
        public static int SCROLL_STATE_TOUCH_SCROLL = 1;
 
        public static int SCROLL_STATE_FLING = 2;
 
        public void onScrollStateChanged(int scrollState);
    }
 
    public TagCloudLayout(Context context) {
        super(context);
    }
 
    public TagCloudLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public TagCloudLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    @SuppressLint("NewApi")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int witdh = Math.min(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
        int height = witdh;
        radius = (witdh - 40) / 2;
        if (mcList == null) {
            mcList = new TagView[getChildCount()];
            double phi = 0;
            double theta = 0;
            
            for (int i = 0; i < getChildCount(); i++) {
                mcList[i] = (TagView) getChildAt(i);
                phi = Math.acos((double) -1 + (double) (2 * (i + 1) - 1) / (double) getChildCount());
                theta = Math.sqrt(getChildCount() * Math.PI) * phi;
                // 坐标变换
                mcList[i].cx = radius * Math.cos(theta) * Math.sin(phi);
                mcList[i].cy = radius * Math.sin(theta) * Math.sin(phi);
                mcList[i].cz = radius * Math.cos(phi);
                
                // add perspective
                int diameter = 2 * radius;
                double per = diameter / (diameter + mcList[i].cz);
 
                mcList[i].scale = per;
                mcList[i].alpha = per;
                mcList[i].alpha = (mcList[i].alpha - 0.6) * (10d / 6d);
                ((TextView) mcList[i].mTarget).setTextSize((float) Math.ceil(12 * mcList[i].scale / 2) + 8);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    ((TextView) mcList[i].mTarget).setAlpha((float) mcList[i].alpha);
                }
            }
        }
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(witdh, height);
    }
 
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.layout((int) mcList[i].cx + getMeasuredWidth() / 2 - child.getMeasuredWidth() / 2,
                    (int) mcList[i].cy + getMeasuredHeight() / 2 - child.getMeasuredHeight() / 2,
                    (int) mcList[i].cx + getMeasuredWidth() / 2 + child.getMeasuredWidth() / 2,
                    (int) mcList[i].cy + getMeasuredHeight() / 2 + child.getMeasuredHeight() / 2);
        }
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            preX = -(ev.getX() - radius);
            preY = ev.getY() - radius;
            downX = ev.getX();
            downY = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            if (Math.sqrt((ev.getX() - downX) * (ev.getX() - downX) + (ev.getY() - downY) * (ev.getY() - downY))
                    > mTouchSlop) {
                return true;
            }
        default:
            break;
        }
        return super.onInterceptTouchEvent(ev);
    }
 
    private double preX;
    private double preY;
    private double preZ;
    private double downX;
    private double downY;
    private boolean allowRotating;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            preX = -(event.getX() - radius);
            preY = event.getY() - radius;
            allowRotating = false;
//            preZ = calculateZ(preX, preY);
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            allowRotating = true;
            post((new FlingRunnable(mAngleX, mAngleY, mAngleZ)));
            break;
        case MotionEvent.ACTION_MOVE:
            double x = -(event.getX() - radius);
            double y = event.getY() - radius;
//            double z = calculateZ(x, y);
 
            double dx = x - preX;
            double dy = y - preY;
//            double dz = z - preZ;
            
            mAngleX = dy / radius * 2;
            mAngleY = dx / radius * 2;
            mAngleZ = 0;
            
            preX = x;
            preY = y;
//            preZ = z;
 
            rotate(mAngleX, mAngleY, mAngleZ);
            if (mOnScrollListener != null) {
                if (mScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                    mScrollState = OnScrollListener.SCROLL_STATE_TOUCH_SCROLL;
                    mOnScrollListener.onScrollStateChanged(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
                }
            }
            break;
        default:
            break;
        }
        return true;
    }
    
    @SuppressLint("NewApi")
    private void rotate(double angleX, double angleY, double angleZ) {
        sineCosine(angleX, angleY, angleZ);
        for (int i = 0; i < mcList.length; i++) {
            TagView child = (TagView) getChildAt(i);
            double rx1 = mcList[i].cx;
            double ry1 = mcList[i].cy * cos_mAngleX + mcList[i].cz * (-sin_mAngleX);
            double rz1 = mcList[i].cy * sin_mAngleX + mcList[i].cz * cos_mAngleX;
 
            double rx2 = rx1 * cos_mAngleY + rz1 * sin_mAngleY;
            double ry2 = ry1;
            double rz2 = rx1 * (-sin_mAngleY) + rz1 * cos_mAngleY;
 
            double rx3 = rx2 * cos_mAngleZ + ry2 * (-sin_mAngleZ);
            double ry3 = rx2 * sin_mAngleZ + ry2 * cos_mAngleZ;
            double rz3 = rz2;
 
            mcList[i].cx = rx3;
            mcList[i].cy = ry3;
            mcList[i].cz = rz3;
 
            // add perspective
            int diameter = 2 * radius;
            double per = diameter / (diameter + rz3);
 
            mcList[i].scale = per;
            mcList[i].alpha = per;
            mcList[i].alpha = (mcList[i].alpha - 0.6) * (10d / 6d);
            
            ((TextView) child.mTarget).setTextSize((float) Math.ceil(12 * mcList[i].scale / 2) + 8);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                ((TextView) child.mTarget).setAlpha((float) mcList[i].alpha);
            }
        }
        requestLayout();
        invalidate();
        depthSort();
    }
 
    private void sineCosine(double angleX, double angleY, double angleZ) {
        sin_mAngleX = Math.sin(angleX);
        cos_mAngleX = Math.cos(angleX);
        sin_mAngleY = Math.sin(angleY);
        cos_mAngleY = Math.cos(angleY);
        sin_mAngleZ = Math.sin(angleZ);
        cos_mAngleZ = Math.cos(angleZ);
    }
    
    public void depthSort() {
        Arrays.sort(mcList);
        for (TagView child : mcList) {
            bringChildToFront(child);
        }
    }
    
    /**
     * 计算z的坐标
     * 
     * @param x coordinate x
     * @param y coordinate y
     * @return the z coordinate
     */
    public double calculateZ(double x, double y) {
        return Math.sqrt(radius * radius - (x * x + y * y));
    }
    
    private class FlingRunnable implements Runnable {
 
        private double angleX;
        
        private double angleY;
        
        private double angleZ;
 
        public FlingRunnable(double angleX, double angleY, double angleZ) {
            this.angleX = angleX;
            this.angleY = angleY;
            this.angleZ = angleZ;
        }
 
        public void run() {
            if (!allowRotating
                    || Math.abs(angleX) <= 0.01 && Math.abs(angleY) <= 0.01 && Math.abs(angleZ) <= 0.01) {
                if (mOnScrollListener != null) {
                    if (mScrollState != OnScrollListener.SCROLL_STATE_IDLE) {
                        mScrollState = OnScrollListener.SCROLL_STATE_IDLE;
                        mOnScrollListener.onScrollStateChanged(OnScrollListener.SCROLL_STATE_IDLE);
                    }
                }
                return;
            } else {
                angleX *= 0.98d;
                angleY *= 0.98d;
                angleZ *= 0.98d;
                if (mOnScrollListener != null) {
                    if (mScrollState != OnScrollListener.SCROLL_STATE_FLING) {
                        mScrollState = OnScrollListener.SCROLL_STATE_FLING;
                        mOnScrollListener.onScrollStateChanged(OnScrollListener.SCROLL_STATE_FLING);
                    }
                }
                rotate(angleX, angleY, angleZ);
                TagCloudLayout.this.post(this);
            }
        }
    }
    
    public static class TagView extends FrameLayout implements Comparable<TagView>{
        
        public double cx;
        
        public double cy;
        
        public double cz;
        
        public double scale;
        
        public double alpha;
        
        public View mTarget;
        
        public TagView(Context context, View target) {
            super(context);
            this.mTarget = target;
            this.addView(mTarget);
        }
 
        @Override
        public int compareTo(TagView another) {
            if (cz > another.cz) {
                return -1;
            } else if (cz < another.cz) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    
    @Override
    public void addView(View child) {
        TagView tagView = new TagView(getContext(), child);
        tagView.setLayoutParams(new LayoutParams(child.getLayoutParams()));
        super.addView(tagView);
    }
}