summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ui/ModeTransitionView.java
blob: 0d0f46520158158d8a60f0d8be6e08ca9702e629 (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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.ui;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.android.camera.app.CameraAppUI;
import com.android.camera.util.Gusterpolator;
import com.android.camera2.R;

/**
 * This view is designed to handle all the animations during camera mode transition.
 * It should only be visible during mode switch.
 */
public class ModeTransitionView extends View {
    private static final String TAG = "ModeTransitionView";

    private static final int PEEP_HOLE_ANIMATION_DURATION_MS = 300;
    private static final int ICON_FADE_OUT_DURATION_MS = 850;
    private static final int FADE_OUT_DURATION_MS = 100;

    private static final int IDLE = 0;
    private static final int PULL_UP_SHADE = 1;
    private static final int PULL_DOWN_SHADE = 2;
    private static final int PEEP_HOLE_ANIMATION = 3;
    private static final int FADE_OUT = 4;

    private static final float SCROLL_DISTANCE_MULTIPLY_FACTOR = 2f;
    private static final int ALPHA_FULLY_TRANSPARENT = 0;
    private static final int ALPHA_FULLY_OPAQUE = 255;
    private static final int ALPHA_HALF_TRANSPARENT = 127;

    private final GestureDetector mGestureDetector;
    private final Paint mMaskPaint = new Paint();
    private final Rect mIconRect = new Rect();
    /** An empty drawable to fall back to when mIconDrawable set to null. */
    private final Drawable mDefaultDrawable = new ColorDrawable();

    private Drawable mIconDrawable;
    private int mBackgroundColor;
    private int mWidth = 0;
    private int mHeight = 0;
    private int mPeepHoleCenterX = 0;
    private int mPeepHoleCenterY = 0;
    private float mRadius = 0f;
    private int mIconSize;
    private AnimatorSet mPeepHoleAnimator;
    private int mAnimationType = PEEP_HOLE_ANIMATION;
    private float mScrollDistance = 0;
    private final Path mShadePath = new Path();
    private final Paint mShadePaint = new Paint();
    private CameraAppUI.AnimationFinishedListener mAnimationFinishedListener;
    private float mScrollTrend;

    public ModeTransitionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMaskPaint.setAlpha(0);
        mMaskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mBackgroundColor = getResources().getColor(R.color.video_mode_color);
        mGestureDetector = new GestureDetector(getContext(),
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onDown(MotionEvent ev) {
                        setScrollDistance(0f);
                        mScrollTrend = 0f;
                        return true;
                    }

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                            float distanceX, float distanceY) {
                        setScrollDistance(getScrollDistance()
                                + SCROLL_DISTANCE_MULTIPLY_FACTOR * distanceY);
                        mScrollTrend = 0.3f * mScrollTrend + 0.7f * distanceY;
                        return false;
                    }
                });
        mIconSize = getResources().getDimensionPixelSize(R.dimen.mode_transition_view_icon_size);
        setIconDrawable(mDefaultDrawable);
    }

    /**
     * Updates the size and shape of the shade
     */
    private void updateShade() {
        if (mAnimationType == PULL_UP_SHADE || mAnimationType == PULL_DOWN_SHADE) {
            mShadePath.reset();
            float shadeHeight;
            if (mAnimationType == PULL_UP_SHADE) {
                // Scroll distance > 0.
                mShadePath.addRect(0, mHeight - getScrollDistance(), mWidth, mHeight,
                        Path.Direction.CW);
                shadeHeight = getScrollDistance();
            } else {
                // Scroll distance < 0.
                mShadePath.addRect(0, 0, mWidth, - getScrollDistance(), Path.Direction.CW);
                shadeHeight = getScrollDistance() * (-1);
            }

            if (mIconDrawable != null) {
                if (shadeHeight < mHeight / 2 || mHeight == 0) {
                    mIconDrawable.setAlpha(ALPHA_FULLY_TRANSPARENT);
                } else {
                    int alpha  = ((int) shadeHeight - mHeight / 2)  * ALPHA_FULLY_OPAQUE
                            / (mHeight / 2);
                    mIconDrawable.setAlpha(alpha);
                }
            }
            invalidate();
        }
    }

    /**
     * Sets the scroll distance. Note this function gets called in every
     * frame during animation. It should be very light weight.
     *
     * @param scrollDistance the scaled distance that user has scrolled
     */
    public void setScrollDistance(float scrollDistance) {
        // First make sure scroll distance is clamped to the valid range.
        if (mAnimationType == PULL_UP_SHADE) {
            scrollDistance = Math.min(scrollDistance, mHeight);
            scrollDistance = Math.max(scrollDistance, 0);
        } else if (mAnimationType == PULL_DOWN_SHADE) {
            scrollDistance = Math.min(scrollDistance, 0);
            scrollDistance = Math.max(scrollDistance, -mHeight);
        }
        mScrollDistance = scrollDistance;
        updateShade();
    }

    public float getScrollDistance() {
        return mScrollDistance;
    }

    @Override
    public void onDraw(Canvas canvas) {
        if (mAnimationType == PEEP_HOLE_ANIMATION) {
            canvas.drawColor(mBackgroundColor);
            if (mPeepHoleAnimator != null) {
                // Draw a transparent circle using clear mode
                canvas.drawCircle(mPeepHoleCenterX, mPeepHoleCenterY, mRadius, mMaskPaint);
            }
        } else if (mAnimationType == PULL_UP_SHADE || mAnimationType == PULL_DOWN_SHADE) {
            canvas.drawPath(mShadePath, mShadePaint);
        } else if (mAnimationType == IDLE || mAnimationType == FADE_OUT) {
            canvas.drawColor(mBackgroundColor);
        }
        super.onDraw(canvas);
        mIconDrawable.draw(canvas);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        mWidth = right - left;
        mHeight = bottom - top;
        // Center the icon in the view.
        mIconRect.set(mWidth / 2 - mIconSize / 2, mHeight / 2 - mIconSize / 2,
                mWidth / 2 + mIconSize / 2, mHeight / 2 + mIconSize / 2);
        mIconDrawable.setBounds(mIconRect);
    }

    /**
     * This is an overloaded function. When no position is provided for the animation,
     * the peep hole will start at the default position (i.e. center of the view).
     */
    public void startPeepHoleAnimation() {
        float x = mWidth / 2;
        float y = mHeight / 2;
        startPeepHoleAnimation(x, y);
    }

    /**
     * Starts the peep hole animation where the circle is centered at position (x, y).
     */
    private void startPeepHoleAnimation(float x, float y) {
        if (mPeepHoleAnimator != null && mPeepHoleAnimator.isRunning()) {
            return;
        }
        mAnimationType = PEEP_HOLE_ANIMATION;
        mPeepHoleCenterX = (int) x;
        mPeepHoleCenterY = (int) y;

        int horizontalDistanceToFarEdge = Math.max(mPeepHoleCenterX, mWidth - mPeepHoleCenterX);
        int verticalDistanceToFarEdge = Math.max(mPeepHoleCenterY, mHeight - mPeepHoleCenterY);
        int endRadius = (int) (Math.sqrt(horizontalDistanceToFarEdge * horizontalDistanceToFarEdge
                + verticalDistanceToFarEdge * verticalDistanceToFarEdge));

        final ValueAnimator radiusAnimator = ValueAnimator.ofFloat(0, endRadius);
        radiusAnimator.setDuration(PEEP_HOLE_ANIMATION_DURATION_MS);

        final ValueAnimator  iconScaleAnimator = ValueAnimator.ofFloat(1f, 0.5f);
        iconScaleAnimator.setDuration(ICON_FADE_OUT_DURATION_MS);

        final ValueAnimator  iconAlphaAnimator = ValueAnimator.ofInt(ALPHA_HALF_TRANSPARENT,
                ALPHA_FULLY_TRANSPARENT);
        iconAlphaAnimator.setDuration(ICON_FADE_OUT_DURATION_MS);

        mPeepHoleAnimator = new AnimatorSet();
        mPeepHoleAnimator.playTogether(radiusAnimator, iconAlphaAnimator, iconScaleAnimator);
        mPeepHoleAnimator.setInterpolator(Gusterpolator.INSTANCE);

        iconAlphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // Modify mask by enlarging the hole
                mRadius = (Float) radiusAnimator.getAnimatedValue();

                mIconDrawable.setAlpha((Integer) iconAlphaAnimator.getAnimatedValue());
                float scale = (Float) iconScaleAnimator.getAnimatedValue();
                int size = (int) (scale * (float) mIconSize);

                mIconDrawable.setBounds(mPeepHoleCenterX - size / 2,
                        mPeepHoleCenterY - size / 2,
                        mPeepHoleCenterX + size / 2,
                        mPeepHoleCenterY + size / 2);

                invalidate();
            }
        });

        mPeepHoleAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mPeepHoleAnimator = null;
                mRadius = 0;
                mIconDrawable.setAlpha(ALPHA_FULLY_OPAQUE);
                mIconDrawable.setBounds(mIconRect);
                setVisibility(GONE);
                mAnimationType = IDLE;
                if (mAnimationFinishedListener != null) {
                    mAnimationFinishedListener.onAnimationFinished(true);
                    mAnimationFinishedListener = null;
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        mPeepHoleAnimator.start();

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean touchHandled = mGestureDetector.onTouchEvent(ev);
        if (ev.getActionMasked() == MotionEvent.ACTION_UP) {
            // TODO: Take into account fling
            snap();
        }
        return touchHandled;
    }

    /**
     * Snaps the shade to position at the end of a gesture.
     */
    private void snap() {
        if (mScrollTrend >= 0 && mAnimationType == PULL_UP_SHADE) {
            // Snap to full screen.
            snapShadeTo(mHeight, ALPHA_FULLY_OPAQUE);
        } else if (mScrollTrend <= 0 && mAnimationType == PULL_DOWN_SHADE) {
            // Snap to full screen.
            snapShadeTo(-mHeight, ALPHA_FULLY_OPAQUE);
        } else if (mScrollTrend < 0 && mAnimationType == PULL_UP_SHADE) {
            // Snap back.
            snapShadeTo(0, ALPHA_FULLY_TRANSPARENT, false);
        } else if (mScrollTrend > 0 && mAnimationType == PULL_DOWN_SHADE) {
            // Snap back.
            snapShadeTo(0, ALPHA_FULLY_TRANSPARENT, false);
        }
    }

    private void snapShadeTo(int scrollDistance, int alpha) {
        snapShadeTo(scrollDistance, alpha, true);
    }

    /**
     * Snaps the shade to a given scroll distance and sets the icon alpha. If the shade
     * is to snap back out, then hide the view after the animation.
     *
     * @param scrollDistance scaled user scroll distance
     * @param alpha ending alpha of the icon drawable
     * @param snapToFullScreen whether this snap animation snaps the shade to full screen
     */
    private void snapShadeTo(final int scrollDistance, final int alpha,
                             final boolean snapToFullScreen) {
        if (mAnimationType == PULL_UP_SHADE || mAnimationType == PULL_DOWN_SHADE) {
            ObjectAnimator scrollAnimator = ObjectAnimator.ofFloat(this, "scrollDistance",
                    scrollDistance);
            scrollAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    setScrollDistance(scrollDistance);
                    mIconDrawable.setAlpha(alpha);
                    mAnimationType = IDLE;
                    if (!snapToFullScreen) {
                        setVisibility(GONE);
                    }
                    if (mAnimationFinishedListener != null) {
                        mAnimationFinishedListener.onAnimationFinished(snapToFullScreen);
                        mAnimationFinishedListener = null;
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            scrollAnimator.setInterpolator(Gusterpolator.INSTANCE);
            scrollAnimator.start();
        }
    }


    /**
     * Set the states for the animation that pulls up a shade with given shade color.
     *
     * @param shadeColorId color id of the shade that will be pulled up
     * @param iconId id of the icon that will appear on top the shade
     * @param listener a listener that will get notified when the animation
     *        is finished. Could be <code>null</code>.
     */
    public void prepareToPullUpShade(int shadeColorId, int iconId,
                                     CameraAppUI.AnimationFinishedListener listener) {
        prepareShadeAnimation(PULL_UP_SHADE, shadeColorId, iconId, listener);
    }

    /**
     * Set the states for the animation that pulls down a shade with given shade color.
     *
     * @param shadeColorId color id of the shade that will be pulled down
     * @param modeIconResourceId id of the icon that will appear on top the shade
     * @param listener a listener that will get notified when the animation
     *        is finished. Could be <code>null</code>.
     */
    public void prepareToPullDownShade(int shadeColorId, int modeIconResourceId,
                                       CameraAppUI.AnimationFinishedListener listener) {;
        prepareShadeAnimation(PULL_DOWN_SHADE, shadeColorId, modeIconResourceId, listener);
    }

    /**
     * Set the states for the animation that involves a shade.
     *
     * @param animationType type of animation that will happen to the shade
     * @param shadeColorId color id of the shade that will be animated
     * @param iconResId id of the icon that will appear on top the shade
     * @param listener a listener that will get notified when the animation
     *        is finished. Could be <code>null</code>.
     */
    private void prepareShadeAnimation(int animationType, int shadeColorId, int iconResId,
                                       CameraAppUI.AnimationFinishedListener listener) {
        mAnimationFinishedListener = listener;
        if (mPeepHoleAnimator != null && mPeepHoleAnimator.isRunning()) {
            mPeepHoleAnimator.end();
        }
        mAnimationType = animationType;
        resetShade(shadeColorId, iconResId);
    }

    /**
     * Reset the shade with the given shade color and icon drawable.
     *
     * @param shadeColorId id of the shade color
     * @param modeIconResourceId resource id of the icon drawable
     */
    private void resetShade(int shadeColorId, int modeIconResourceId) {
        // Sets color for the shade.
        int shadeColor = getResources().getColor(shadeColorId);
        mBackgroundColor = shadeColor;
        mShadePaint.setColor(shadeColor);
        // Reset scroll distance.
        setScrollDistance(0f);
        // Sets new drawable.
        updateIconDrawableByResourceId(modeIconResourceId);
        mIconDrawable.setAlpha(0);
        setVisibility(VISIBLE);
    }

    /**
     * By default, all drawables instances loaded from the same resource share a
     * common state; if you modify the state of one instance, all the other
     * instances will receive the same modification. So here we need to make sure
     * we mutate the drawable loaded from resource.
     *
     * @param modeIconResourceId resource id of the icon drawable
     */
    private void updateIconDrawableByResourceId(int modeIconResourceId) {
        Drawable iconDrawable = getResources().getDrawable(modeIconResourceId);
        if (iconDrawable == null) {
            // Resource id not found
            Log.e(TAG, "Invalid resource id for icon drawable. Setting icon drawable to null.");
            setIconDrawable(null);
            return;
        }
        // Mutate the drawable loaded from resource so modifying its states does
        // not affect other drawable instances loaded from the same resource.
        setIconDrawable(iconDrawable.mutate());
    }

    /**
     * In order to make sure icon drawable is never set to null. Fall back to an
     * empty drawable when icon needs to get reset.
     *
     * @param iconDrawable new drawable for icon. A value of <code>null</code> sets
     *        the icon drawable to the default drawable.
     */
    private void setIconDrawable(Drawable iconDrawable) {
        if (iconDrawable == null) {
            mIconDrawable = mDefaultDrawable;
        } else {
            mIconDrawable = iconDrawable;
        }
    }

    /**
     * Initialize the mode cover with a mode theme color and a mode icon.
     *
     * @param colorId resource id of the mode theme color
     * @param modeIconResourceId resource id of the icon drawable
     */
    public void setupModeCover(int colorId, int modeIconResourceId) {
        // Stop ongoing animation.
        if (mPeepHoleAnimator != null && mPeepHoleAnimator.isRunning()) {
            mPeepHoleAnimator.cancel();
        }
        mAnimationType = IDLE;
        mBackgroundColor = getResources().getColor(colorId);
        // Sets new drawable.
        updateIconDrawableByResourceId(modeIconResourceId);
        mIconDrawable.setAlpha(ALPHA_FULLY_OPAQUE);
        setVisibility(VISIBLE);
    }

    /**
     * Hides the cover view and notifies the
     * {@link com.android.camera.app.CameraAppUI.AnimationFinishedListener} of whether
     * the hide animation is successfully finished.
     *
     * @param animationFinishedListener a listener that will get notified when the
     *        animation is finished. Could be <code>null</code>.
     */
    public void hideModeCover(
            final CameraAppUI.AnimationFinishedListener animationFinishedListener) {
        if (mAnimationType != IDLE) {
            // Nothing to hide.
            if (animationFinishedListener != null) {
                // Animation not successful.
                animationFinishedListener.onAnimationFinished(false);
            }
        } else {
            // Start fade out animation.
            mAnimationType = FADE_OUT;
            ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(this, "alpha", 1f, 0f);
            alphaAnimator.setDuration(FADE_OUT_DURATION_MS);
            // Linear interpolation.
            alphaAnimator.setInterpolator(null);
            alphaAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    setVisibility(GONE);
                    setAlpha(1f);
                    animationFinishedListener.onAnimationFinished(true);
                    mAnimationType = IDLE;
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            alphaAnimator.start();
        }
    }

    @Override
    public void setAlpha(float alpha) {
        super.setAlpha(alpha);
        int alphaScaled = (int) (255f * getAlpha());
        mBackgroundColor = (mBackgroundColor & 0xFFFFFF) | (alphaScaled << 24);
        mIconDrawable.setAlpha(alphaScaled);
    }
}