summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/common/widget/CheckableFlipDrawable.java
blob: 329b665c7d2499dfbe06cb4be1548f6cf6131d18 (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
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
package com.android.contacts.common.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.QuickContactBadge;

import com.android.contacts.common.R;

public class CheckableFlipDrawable extends FlipDrawable implements
        ValueAnimator.AnimatorUpdateListener {

    private final FrontDrawable mFrontDrawable;
    private final CheckmarkDrawable mCheckmarkDrawable;

    private final ValueAnimator mCheckmarkScaleAnimator;
    private final ValueAnimator mCheckmarkAlphaAnimator;

    private static final int POST_FLIP_DURATION_MS = 150;

    private static final float CHECKMARK_SCALE_BEGIN_VALUE = 0.2f;
    private static final float CHECKMARK_ALPHA_BEGIN_VALUE = 0f;

    /** Must be <= 1f since the animation value is used as a percentage. */
    private static final float END_VALUE = 1f;

    public CheckableFlipDrawable(Drawable front, final Resources res,
            final int checkBackgroundColor, final int flipDurationMs) {
        super(new FrontDrawable(front), new CheckmarkDrawable(res, checkBackgroundColor),
                flipDurationMs, 0 /* preFlipDurationMs */, POST_FLIP_DURATION_MS);

        mFrontDrawable = (FrontDrawable) mFront;
        mCheckmarkDrawable = (CheckmarkDrawable) mBack;

        // We will create checkmark animations that are synchronized with the
        // flipping animation. The entire delay + duration of the checkmark animation
        // needs to equal the entire duration of the flip animation (where delay is 0).

        // The checkmark animation is in effect only when the back drawable is being shown.
        // For the flip animation duration    <pre>[_][]|[][_]<post>
        // The checkmark animation will be    |--delay--|-duration-|

        // Need delay to skip the first half of the flip duration.
        final long animationDelay = mPreFlipDurationMs + mFlipDurationMs / 2;
        // Actual duration is the second half of the flip duration.
        final long animationDuration = mFlipDurationMs / 2 + mPostFlipDurationMs;

        mCheckmarkScaleAnimator = ValueAnimator.ofFloat(CHECKMARK_SCALE_BEGIN_VALUE, END_VALUE)
                .setDuration(animationDuration);
        mCheckmarkScaleAnimator.setStartDelay(animationDelay);
        mCheckmarkScaleAnimator.addUpdateListener(this);

        mCheckmarkAlphaAnimator = ValueAnimator.ofFloat(CHECKMARK_ALPHA_BEGIN_VALUE, END_VALUE)
                .setDuration(animationDuration);
        mCheckmarkAlphaAnimator.setStartDelay(animationDelay);
        mCheckmarkAlphaAnimator.addUpdateListener(this);
    }

    public void setFront(Drawable front) {
        mFrontDrawable.setInnerDrawable(front);
        invalidateSelf();
    }

    public void setCheckMarkBackgroundColor(int color) {
        mCheckmarkDrawable.setBackgroundColor(color);
        invalidateSelf();
    }

    @Override
    public void reset() {
        super.reset();
        if (mCheckmarkScaleAnimator == null) {
            // Call from super's constructor. Not yet initialized.
            return;
        }
        mCheckmarkScaleAnimator.cancel();
        mCheckmarkAlphaAnimator.cancel();
        boolean side = getSideFlippingTowards();
        mCheckmarkDrawable.setScaleAnimatorValue(side ? CHECKMARK_SCALE_BEGIN_VALUE : END_VALUE);
        mCheckmarkDrawable.setAlphaAnimatorValue(side ? CHECKMARK_ALPHA_BEGIN_VALUE : END_VALUE);
    }

    @Override
    public void flip() {
        super.flip();
        // Keep the checkmark animators in sync with the flip animator.
        if (mCheckmarkScaleAnimator.isStarted()) {
            mCheckmarkScaleAnimator.reverse();
            mCheckmarkAlphaAnimator.reverse();
        } else {
            if (!getSideFlippingTowards() /* front to back */) {
                mCheckmarkScaleAnimator.start();
                mCheckmarkAlphaAnimator.start();
            } else /* back to front */ {
                mCheckmarkScaleAnimator.reverse();
                mCheckmarkAlphaAnimator.reverse();
            }
        }
    }

    @Override
    public void onAnimationUpdate(final ValueAnimator animation) {
        //noinspection ConstantConditions
        final float value = (Float) animation.getAnimatedValue();

        if (animation == mCheckmarkScaleAnimator) {
            mCheckmarkDrawable.setScaleAnimatorValue(value);
        } else if (animation == mCheckmarkAlphaAnimator) {
            mCheckmarkDrawable.setAlphaAnimatorValue(value);
        }
    }

    private static class FrontDrawable extends Drawable implements Drawable.Callback {
        private Drawable mDrawable;

        public FrontDrawable(Drawable d) {
            mDrawable = d;
            mDrawable.setCallback(this);
        }

        public void setInnerDrawable(Drawable d) {
            mDrawable.setCallback(null);
            mDrawable = d;
            mDrawable.setCallback(this);
            assignDrawableBounds(getBounds());
            invalidateSelf();
        }

        @Override
        protected void onBoundsChange(final Rect bounds) {
            super.onBoundsChange(bounds);
            assignDrawableBounds(bounds);
        }

        private void assignDrawableBounds(Rect bounds) {
            int w = mDrawable.getIntrinsicWidth();
            int h = mDrawable.getIntrinsicHeight();
            if (w > 0 && h > 0) {
                mDrawable.setBounds(0, 0, w, h);
            } else {
                mDrawable.setBounds(bounds);
            }
        }

        @Override
        public void draw(final Canvas canvas) {
            final Rect bounds = getBounds();
            if (!isVisible() || bounds.isEmpty()) {
                return;
            }

            int w = mDrawable.getIntrinsicWidth();
            int h = mDrawable.getIntrinsicHeight();

            if (w <= 0 || h <= 0) {
                mDrawable.draw(canvas);
            } else {
                final float widthScale = (float) bounds.width() / (float) w;
                final float heightScale = (float) bounds.height() / (float) h;
                final float scale = Math.max(widthScale, heightScale);

                canvas.save();
                canvas.scale(scale, scale);
                canvas.translate(bounds.left, bounds.top);
                mDrawable.draw(canvas);
                canvas.restore();
            }
        }

        @Override
        public void setAlpha(final int alpha) {
            mDrawable.setAlpha(alpha);
        }

        @Override
        public void setColorFilter(final ColorFilter cf) {
            mDrawable.setColorFilter(cf);
        }

        @Override
        public int getOpacity() {
            return mDrawable.getOpacity();
        }

        @Override
        public void invalidateDrawable(final Drawable who) {
            invalidateSelf();
        }

        @Override
        public void scheduleDrawable(final Drawable who, final Runnable what, final long when) {
            scheduleSelf(what, when);
        }

        @Override
        public void unscheduleDrawable(final Drawable who, final Runnable what) {
            unscheduleSelf(what);
        }
    }

    private static class CheckmarkDrawable extends Drawable {
        private static Bitmap sCheckMark;

        private final Paint mPaint;

        private float mScaleFraction;
        private float mAlphaFraction;

        private static final Matrix sMatrix = new Matrix();

        public CheckmarkDrawable(final Resources res, int backgroundColor) {
            if (sCheckMark == null) {
                sCheckMark = BitmapFactory.decodeResource(res, R.drawable.ic_check_wht_24dp);
            }
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setFilterBitmap(true);
            mPaint.setColor(backgroundColor);
        }

        public void setBackgroundColor(int color) {
            mPaint.setColor(color);
        }

        @Override
        public void draw(final Canvas canvas) {
            final Rect bounds = getBounds();
            if (!isVisible() || bounds.isEmpty()) {
                return;
            }

            canvas.drawCircle(bounds.centerX(), bounds.centerY(), bounds.width() / 2, mPaint);

            // Scale the checkmark.
            sMatrix.reset();
            sMatrix.setScale(mScaleFraction, mScaleFraction, sCheckMark.getWidth() / 2,
                    sCheckMark.getHeight() / 2);
            sMatrix.postTranslate(bounds.centerX() - sCheckMark.getWidth() / 2,
                    bounds.centerY() - sCheckMark.getHeight() / 2);

            // Fade the checkmark.
            final int oldAlpha = mPaint.getAlpha();
            // Interpolate the alpha.
            mPaint.setAlpha((int) (oldAlpha * mAlphaFraction));
            canvas.drawBitmap(sCheckMark, sMatrix, mPaint);
            // Restore the alpha.
            mPaint.setAlpha(oldAlpha);
        }

        @Override
        public void setAlpha(final int alpha) {
            mPaint.setAlpha(alpha);
        }

        @Override
        public void setColorFilter(final ColorFilter cf) {
            mPaint.setColorFilter(cf);
        }

        @Override
        public int getOpacity() {
            // Always a gray background.
            return PixelFormat.OPAQUE;
        }

        /**
         * Set value as a fraction from 0f to 1f.
         */
        public void setScaleAnimatorValue(final float value) {
            final float old = mScaleFraction;
            mScaleFraction = value;
            if (old != mScaleFraction) {
                invalidateSelf();
            }
        }

        /**
         * Set value as a fraction from 0f to 1f.
         */
        public void setAlphaAnimatorValue(final float value) {
            final float old = mAlphaFraction;
            mAlphaFraction = value;
            if (old != mAlphaFraction) {
                invalidateSelf();
            }
        }
    }
}