summaryrefslogtreecommitdiffstats
path: root/sparkbutton/src/main/java/com/varunest/sparkbutton/helpers/SparkAnimationView.java
blob: c7c5fb59f6de78c678719798d5d3a246c07c49b0 (plain)
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
package com.varunest.sparkbutton.helpers;

import android.animation.ArgbEvaluator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;


public class SparkAnimationView extends View {
    public static final Property<SparkAnimationView, Float> INNER_CIRCLE_RADIUS_PROGRESS =
            new Property<SparkAnimationView, Float>(Float.class, "innerCircleRadiusProgress") {
                @Override
                public Float get(SparkAnimationView object) {
                    return object.getInnerCircleRadiusProgress();
                }

                @Override
                public void set(SparkAnimationView object, Float value) {
                    object.setInnerCircleRadiusProgress(value);
                }
            };
    private static final int DOTS_COUNT = 12;
    private static final int OUTER_DOTS_POSITION_ANGLE = 360 / DOTS_COUNT;
    private static final ArgbEvaluator argbEvaluator = new ArgbEvaluator();
    public static final Property<SparkAnimationView, Float> DOTS_PROGRESS = new Property<SparkAnimationView, Float>(Float.class, "dotsProgress") {
        @Override
        public Float get(SparkAnimationView object) {
            return object.getCurrentProgress();
        }

        @Override
        public void set(SparkAnimationView object, Float value) {
            object.setCurrentProgress(value);
        }
    };
    public static final Property<SparkAnimationView, Float> OUTER_CIRCLE_RADIUS_PROGRESS =
            new Property<SparkAnimationView, Float>(Float.class, "outerCircleRadiusProgress") {
                @Override
                public Float get(SparkAnimationView object) {
                    return object.getOuterCircleRadiusProgress();
                }

                @Override
                public void set(SparkAnimationView object, Float value) {
                    object.setOuterCircleRadiusProgress(value);
                }
            };
    private final Paint[] dotsPaints = new Paint[4];
    private final Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int primaryColor = 0xFFFFC107;
    private int primaryColorDark = 0xFFFF9800;
    private int secondaryColor = 0xFFFF5722;
    private int secondaryColorDark = 0xFFF44336;
    private int centerX;
    private int centerY;
    private float maxOuterDotsRadius;
    private float maxInnerDotsRadius;
    private float maxDotSize;
    private float currentProgress = 0;
    private float currentRadius1 = 0;
    private float currentDotSize1 = 0;
    private float currentDotSize2 = 0;
    private float currentRadius2 = 0;
    private float outerCircleRadiusProgress = 0f;
    private float innerCircleRadiusProgress = 0f;
    private float maxCircleSize;

    public SparkAnimationView(Context context) {
        super(context);
        init();
    }

    public SparkAnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SparkAnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SparkAnimationView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setLayerType(View.LAYER_TYPE_HARDWARE, null);

        maxDotSize = Utils.dpToPx(getContext(), 4);
        for (int i = 0; i < dotsPaints.length; i++) {
            dotsPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
        }

        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        centerX = w / 2;
        centerY = h / 2;
        maxOuterDotsRadius = w / 2 - maxDotSize * 2;
        maxInnerDotsRadius = 0.8f * maxOuterDotsRadius;
        maxCircleSize = w / 4.3f;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawOuterDotsFrame(canvas);
        drawInnerDotsFrame(canvas);

        canvas.drawCircle(getWidth() / 2, getHeight() / 2, outerCircleRadiusProgress * maxCircleSize, circlePaint);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, innerCircleRadiusProgress * (maxCircleSize + 1), maskPaint);
    }

    public void setMaxDotSize(int pxUnits) {
        maxDotSize = pxUnits;
    }

    private void drawOuterDotsFrame(Canvas canvas) {
        for (int i = 0; i < DOTS_COUNT; i++) {
            int cX = (int) (centerX + currentRadius1 * Math.cos(i * OUTER_DOTS_POSITION_ANGLE * Math.PI / 180));
            int cY = (int) (centerY + currentRadius1 * Math.sin(i * OUTER_DOTS_POSITION_ANGLE * Math.PI / 180));
            canvas.drawCircle(cX, cY, currentDotSize1, dotsPaints[i % dotsPaints.length]);
        }
    }

    private void drawInnerDotsFrame(Canvas canvas) {
        for (int i = 0; i < DOTS_COUNT; i++) {
            int cX = (int) (centerX + currentRadius2 * Math.cos((i * OUTER_DOTS_POSITION_ANGLE - 10) * Math.PI / 180));
            int cY = (int) (centerY + currentRadius2 * Math.sin((i * OUTER_DOTS_POSITION_ANGLE - 10) * Math.PI / 180));
            canvas.drawCircle(cX, cY, currentDotSize2, dotsPaints[(i + 1) % dotsPaints.length]);
        }
    }

    public float getCurrentProgress() {
        return currentProgress;
    }

    public void setCurrentProgress(float currentProgress) {
        this.currentProgress = currentProgress;

        updateInnerDotsPosition();
        updateOuterDotsPosition();
        updateDotsPaints();
        updateDotsAlpha();

        postInvalidate();
    }

    private void updateInnerDotsPosition() {
        if (currentProgress < 0.3f) {
            this.currentRadius2 = (float) Utils.mapValueFromRangeToRange(currentProgress, 0, 0.3f, 0.f, maxInnerDotsRadius);
        } else {
            this.currentRadius2 = maxInnerDotsRadius;
        }

        if (currentProgress < 0.2) {
            this.currentDotSize2 = maxDotSize;
        } else if (currentProgress < 0.5) {
            this.currentDotSize2 = (float) Utils.mapValueFromRangeToRange(currentProgress, 0.2f, 0.5f, maxDotSize, 0.3 * maxDotSize);
        } else {
            this.currentDotSize2 = (float) Utils.mapValueFromRangeToRange(currentProgress, 0.5f, 1f, maxDotSize * 0.3f, 0);
        }

    }

    private void updateOuterDotsPosition() {
        if (currentProgress < 0.3f) {
            this.currentRadius1 = (float) Utils.mapValueFromRangeToRange(currentProgress, 0.0f, 0.3f, 0, maxOuterDotsRadius * 0.8f);
        } else {
            this.currentRadius1 = (float) Utils.mapValueFromRangeToRange(