admin
2024-10-30 4c44b1036ff555e63a9a8b027dbe89d0b08a430b
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
package com.lcjian.library.widget;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
 
import com.lcjian.lcjianlibrary.R;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.PropertyValuesHolder;
 
public class ArcMeun extends FrameLayout {
 
    private float mFromDegrees;
    private float mToDegrees;
    private float mRadius;
    private int mDuration;
    private boolean closeOnClick;
    private int mainImage;
    
    private ImageView mainbtn;
    
    private float[] translationXs;
    private float[] translationYs;
    
    public ArcMeun(Context context) {
        this(context, null);
    }
 
    public ArcMeun(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public ArcMeun(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ArcMenu, defStyle, 0);
        
        mFromDegrees = a.getFloat(R.styleable.ArcMenu_fromDegrees, 180);
        mToDegrees = a.getFloat(R.styleable.ArcMenu_toDegrees, 90);
        mRadius = a.getDimensionPixelSize(R.styleable.ArcMenu_arcRadius, 100);
        mDuration = a.getInteger(R.styleable.ArcMenu_duration, 300);
        closeOnClick = a.getBoolean(R.styleable.ArcMenu_closeOnClick, true);
        mainImage = a.getResourceId(R.styleable.ArcMenu_mainImage, 0);
 
        a.recycle();
        
        mainbtn = new ImageView(context);
        mainbtn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mainbtn.setImageResource(mainImage);
        addView(mainbtn);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (Math.abs(mToDegrees - mFromDegrees) > 360) {
            throw new IllegalStateException("angle must less than 360");
        }
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        translationXs = new float[getChildCount()];
        translationYs = new float[getChildCount()];
        for (int i = 0; i < getChildCount(); i++) {
            translationXs[i] = (float) Math.sin(Math.toRadians(mFromDegrees + (mFromDegrees - mToDegrees) / (getChildCount() - 1) * i)) * mRadius;
            translationYs[i] = (float) Math.cos(Math.toRadians(mFromDegrees + (mFromDegrees - mToDegrees) / (getChildCount() - 1) * i)) * mRadius;
        }
        float minTranslationX = getMin(translationXs);
        float maxTranslationX = getMax(translationXs);
        float minTranslationY = getMin(translationYs);
        float maxTranslationY = getMax(translationYs);
        
        setMeasuredDimension((int) (maxTranslationX - minTranslationX), (int) (maxTranslationY - minTranslationY));
    }
    
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }
    
    private float getMax(float[] arryas) {
        if (arryas == null || arryas.length <= 0) {
            return 0;
        }
        float max = arryas[0];
        for (int i = 0; i < arryas.length; i++) {
            if (max < arryas[i]) {
                max = arryas[i];
            }
        }
        return max;
    }
    
    private float getMin(float[] arryas) {
        if (arryas == null || arryas.length <= 0) {
            return 0;
        }
        float min = arryas[0];
        for (int i = 0; i < arryas.length; i++) {
            if (min > arryas[i]) {
                min = arryas[i];
            }
        }
        return min;
    }
    
    public void addMenuItem(View item, OnClickListener l) {
        item.setOnClickListener(l);
        addView(item);
        mainbtn.bringToFront();
    }
    
    public void closeMeun() {
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            PropertyValuesHolder propertyValuesHolderX = PropertyValuesHolder.ofFloat("translationX",
                    ((float) Math.sin(Math.toRadians(mFromDegrees + (mFromDegrees - mToDegrees) / (getChildCount() - 1) * i)) * mRadius), 0);
            PropertyValuesHolder propertyValuesHolderY = PropertyValuesHolder.ofFloat("translationY",
                    ((float) Math.cos(Math.toRadians(mFromDegrees + (mFromDegrees - mToDegrees) / (getChildCount() - 1) * i)) * mRadius), 0);
            PropertyValuesHolder propertyValuesHolderrotation = PropertyValuesHolder.ofFloat("rotation", 720, 0);
            
            ObjectAnimator item = ObjectAnimator.ofPropertyValuesHolder(child,
                    propertyValuesHolderX, propertyValuesHolderY,
                    propertyValuesHolderrotation).setDuration(mDuration);
            item.setStartDelay((getChildCount() - 1 - i) * 30);
            item.setInterpolator(new AnticipateInterpolator(2));
            item.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    child.setVisibility(View.VISIBLE);
                }
                @Override
                public void onAnimationEnd(Animator animation) {
                    child.setVisibility(View.INVISIBLE);
                }
            });
            item.start();
        }
        ObjectAnimator rotation = ObjectAnimator.ofFloat(mainbtn, "rotation", 135, 0);
        rotation.start();
    }
    
    public void openMeun() {
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            PropertyValuesHolder propertyValuesHolderX = PropertyValuesHolder.ofFloat("translationX", 0,
                    ((float) Math.sin(Math.toRadians(mFromDegrees + (mFromDegrees - mToDegrees) / (getChildCount() - 1) * i)) * mRadius));
            PropertyValuesHolder propertyValuesHolderY = PropertyValuesHolder.ofFloat("translationY", 0,
                    ((float) Math.cos(Math.toRadians(mFromDegrees + (mFromDegrees - mToDegrees) / (getChildCount() - 1) * i)) * mRadius));
            PropertyValuesHolder propertyValuesHolderrotation = PropertyValuesHolder.ofFloat("rotation", 0, 720);
            
            ObjectAnimator item = ObjectAnimator.ofPropertyValuesHolder(child,
                    propertyValuesHolderX, propertyValuesHolderY,
                    propertyValuesHolderrotation).setDuration(mDuration);
            item.setStartDelay(i * 30);
            item.setInterpolator(new OvershootInterpolator(2));
            item.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    child.setVisibility(View.VISIBLE);
                }
            });
            item.start();
        }
        ObjectAnimator rotation = ObjectAnimator.ofFloat(mainbtn, "rotation", 0, 135);
        rotation.start();
    }
}