Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.camera; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorSet; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.view.View; |
| 23 | |
| 24 | /** |
| 25 | * Class to handle animations. |
| 26 | */ |
| 27 | |
| 28 | public class AnimationManager { |
| 29 | |
| 30 | public static final float FLASH_ALPHA_START = 0.3f; |
| 31 | public static final float FLASH_ALPHA_END = 0f; |
| 32 | public static final int FLASH_DURATION = 300; |
| 33 | |
| 34 | public static final int SHRINK_DURATION = 400; |
| 35 | public static final int HOLD_DURATION = 2500; |
| 36 | public static final int SLIDE_DURATION = 1100; |
| 37 | |
| 38 | private ObjectAnimator mFlashAnim; |
| 39 | private AnimatorSet mCaptureAnimator; |
| 40 | |
| 41 | /** |
| 42 | * Starts capture animation. |
| 43 | * @param view a thumbnail view that shows a picture captured and gets animated |
| 44 | */ |
| 45 | public void startCaptureAnimation(final View view) { |
| 46 | if (mCaptureAnimator != null && mCaptureAnimator.isStarted()) { |
| 47 | mCaptureAnimator.cancel(); |
| 48 | } |
| 49 | View parentView = (View) view.getParent(); |
| 50 | float slideDistance = (float) (parentView.getWidth() - view.getLeft()); |
| 51 | |
| 52 | float scaleX = ((float) parentView.getWidth()) / ((float) view.getWidth()); |
| 53 | float scaleY = ((float) parentView.getHeight()) / ((float) view.getHeight()); |
| 54 | float scale = scaleX > scaleY ? scaleX : scaleY; |
| 55 | |
| 56 | int centerX = view.getLeft() + view.getWidth() / 2; |
| 57 | int centerY = view.getTop() + view.getHeight() / 2; |
| 58 | |
| 59 | ObjectAnimator slide = ObjectAnimator.ofFloat(view, "translationX", 0f, slideDistance) |
| 60 | .setDuration(AnimationManager.SLIDE_DURATION); |
| 61 | slide.setStartDelay(AnimationManager.SHRINK_DURATION + AnimationManager.HOLD_DURATION); |
| 62 | mCaptureAnimator = new AnimatorSet(); |
| 63 | mCaptureAnimator.playTogether( |
| 64 | ObjectAnimator.ofFloat(view, "scaleX", scale, 1f) |
| 65 | .setDuration(AnimationManager.SHRINK_DURATION), |
| 66 | ObjectAnimator.ofFloat(view, "scaleY", scale, 1f) |
| 67 | .setDuration(AnimationManager.SHRINK_DURATION), |
| 68 | ObjectAnimator.ofFloat(view, "translationX", |
| 69 | parentView.getWidth() / 2 - centerX, 0f) |
| 70 | .setDuration(AnimationManager.SHRINK_DURATION), |
| 71 | ObjectAnimator.ofFloat(view, "translationY", |
| 72 | parentView.getHeight() / 2 - centerY, 0f) |
| 73 | .setDuration(AnimationManager.SHRINK_DURATION), |
| 74 | slide); |
| 75 | mCaptureAnimator.addListener(new Animator.AnimatorListener() { |
| 76 | @Override |
| 77 | public void onAnimationStart(Animator animator) { |
| 78 | view.setVisibility(View.VISIBLE); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onAnimationEnd(Animator animator) { |
| 83 | view.setScaleX(1f); |
| 84 | view.setScaleX(1f); |
| 85 | view.setTranslationX(0f); |
| 86 | view.setTranslationY(0f); |
| 87 | view.setVisibility(View.INVISIBLE); |
| 88 | mCaptureAnimator.removeAllListeners(); |
| 89 | mCaptureAnimator = null; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onAnimationCancel(Animator animator) { |
| 94 | view.setVisibility(View.INVISIBLE); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onAnimationRepeat(Animator animator) { |
| 99 | // Do nothing. |
| 100 | } |
| 101 | }); |
| 102 | mCaptureAnimator.start(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Starts flash animation. |
| 107 | * @params flashOverlay the overlay that will animate on alpha to make the flash impression |
| 108 | */ |
| 109 | public void startFlashAnimation(final View flashOverlay) { |
| 110 | // End the previous animation if the previous one is still running |
| 111 | if (mFlashAnim != null && mFlashAnim.isRunning()) { |
| 112 | mFlashAnim.cancel(); |
| 113 | } |
| 114 | // Start new flash animation. |
| 115 | mFlashAnim = ObjectAnimator.ofFloat(flashOverlay, "alpha", |
| 116 | AnimationManager.FLASH_ALPHA_START, AnimationManager.FLASH_ALPHA_END); |
| 117 | mFlashAnim.setDuration(AnimationManager.FLASH_DURATION); |
| 118 | mFlashAnim.addListener(new Animator.AnimatorListener() { |
| 119 | @Override |
| 120 | public void onAnimationStart(Animator animator) { |
| 121 | flashOverlay.setVisibility(View.VISIBLE); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onAnimationEnd(Animator animator) { |
| 126 | flashOverlay.setAlpha(0f); |
| 127 | flashOverlay.setVisibility(View.GONE); |
| 128 | mFlashAnim.removeAllListeners(); |
| 129 | mFlashAnim = null; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onAnimationCancel(Animator animator) { |
| 134 | // Do nothing. |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onAnimationRepeat(Animator animator) { |
| 139 | // Do nothing. |
| 140 | } |
| 141 | }); |
| 142 | mFlashAnim.start(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Cancels on-going flash animation and capture animation, if any. |
| 147 | */ |
| 148 | public void cancelAnimations() { |
| 149 | // End the previous animation if the previous one is still running |
| 150 | if (mFlashAnim != null && mFlashAnim.isRunning()) { |
| 151 | mFlashAnim.cancel(); |
| 152 | } |
| 153 | if (mCaptureAnimator != null && mCaptureAnimator.isStarted()) { |
| 154 | mCaptureAnimator.cancel(); |
| 155 | } |
| 156 | } |
| 157 | } |