aboutsummaryrefslogtreecommitdiff
path: root/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/RippleToggleButton.java
blob: fd2ac6110142479af945f3db84f3cd7aa3387b1e (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
package org.wordpress.android.editor;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ToggleButton;

public class RippleToggleButton extends ToggleButton {
    private static final int FRAME_RATE = 10;
    private static final int DURATION = 250;
    private static final int FILL_INITIAL_OPACITY = 200;
    private static final int STROKE_INITIAL_OPACITY = 255;

    private float mHalfWidth;
    private boolean mAnimationIsRunning = false;
    private int mTimer = 0;
    private Paint mFillPaint;
    private Paint mStrokePaint;

    public RippleToggleButton(Context context) {
        this(context, null);
   }

    public RippleToggleButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RippleToggleButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        if (isInEditMode()) {
            return;
        }

        int rippleColor = getResources().getColor(R.color.format_bar_ripple_animation);

        mFillPaint = new Paint();
        mFillPaint.setAntiAlias(true);
        mFillPaint.setColor(rippleColor);
        mFillPaint.setStyle(Paint.Style.FILL);
        mFillPaint.setAlpha(FILL_INITIAL_OPACITY);

        mStrokePaint = new Paint();
        mStrokePaint.setAntiAlias(true);
        mStrokePaint.setColor(rippleColor);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(2);
        mStrokePaint.setAlpha(STROKE_INITIAL_OPACITY);

        setWillNotDraw(false);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        super.draw(canvas);
        if (mAnimationIsRunning) {
            if (DURATION <= mTimer * FRAME_RATE) {
                mAnimationIsRunning = false;
                mTimer = 0;
            } else {
                float progressFraction = ((float) mTimer * FRAME_RATE) / DURATION;

                mFillPaint.setAlpha((int) (FILL_INITIAL_OPACITY * (1 - progressFraction)));
                mStrokePaint.setAlpha((int) (STROKE_INITIAL_OPACITY * (1 - progressFraction)));

                canvas.drawCircle(mHalfWidth, mHalfWidth, mHalfWidth * progressFraction, mFillPaint);
                canvas.drawCircle(mHalfWidth, mHalfWidth, mHalfWidth * progressFraction, mStrokePaint);

                mTimer++;
            }

            invalidate();
        }
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        startRippleAnimation();
        return super.onTouchEvent(event);
    }

    private void startRippleAnimation() {
        if (this.isEnabled() && !mAnimationIsRunning) {
            mHalfWidth = getMeasuredWidth() / 2;
            mAnimationIsRunning = true;
            invalidate();
        }
    }
}