Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.launcher2; |
| 18 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * A convenience class for two-way animations, e.g. a fadeIn/fadeOut animation. |
| 25 | * With a regular ValueAnimator, if you call reverse to show the 'out' animation, you'll get |
| 26 | * a frame-by-frame mirror of the 'in' animation -- i.e., the interpolated values will |
| 27 | * be exactly reversed. Using this class, both the 'in' and the 'out' animation use the |
| 28 | * interpolator in the same direction. |
| 29 | */ |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 30 | public class InterruptibleInOutAnimator { |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 31 | private long mOriginalDuration; |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 32 | private float mOriginalFromValue; |
| 33 | private float mOriginalToValue; |
| 34 | private ValueAnimator mAnimator; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 35 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 36 | private boolean mFirstRun = true; |
| 37 | |
| 38 | private Object mTag = null; |
| 39 | |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 40 | private static final int STOPPED = 0; |
| 41 | private static final int IN = 1; |
| 42 | private static final int OUT = 2; |
| 43 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 44 | // TODO: This isn't really necessary, but is here to help diagnose a bug in the drag viz |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 45 | private int mDirection = STOPPED; |
| 46 | |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 47 | public InterruptibleInOutAnimator(long duration, float fromValue, float toValue) { |
| 48 | mAnimator = ValueAnimator.ofFloat(fromValue, toValue).setDuration(duration); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 49 | mOriginalDuration = duration; |
| 50 | mOriginalFromValue = fromValue; |
| 51 | mOriginalToValue = toValue; |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 52 | |
Michael Jurka | 8edd75c | 2010-12-17 20:15:06 -0800 | [diff] [blame] | 53 | mAnimator.addListener(new AnimatorListenerAdapter() { |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 54 | @Override |
Michael Jurka | 8edd75c | 2010-12-17 20:15:06 -0800 | [diff] [blame] | 55 | public void onAnimationEnd(Animator animation) { |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 56 | mDirection = STOPPED; |
| 57 | } |
| 58 | }); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 61 | private void animate(int direction) { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 62 | final long currentPlayTime = mAnimator.getCurrentPlayTime(); |
| 63 | final float toValue = (direction == IN) ? mOriginalToValue : mOriginalFromValue; |
| 64 | final float startValue = mFirstRun ? mOriginalFromValue : |
| 65 | ((Float) mAnimator.getAnimatedValue()).floatValue(); |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 66 | |
| 67 | // Make sure it's stopped before we modify any values |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 68 | cancel(); |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 69 | |
Patrick Dubroy | 08ae2ec | 2010-10-14 23:54:22 -0700 | [diff] [blame] | 70 | // TODO: We don't really need to do the animation if startValue == toValue, but |
| 71 | // somehow that doesn't seem to work, possibly a quirk of the animation framework |
| 72 | mDirection = direction; |
| 73 | |
| 74 | // Ensure we don't calculate a non-sensical duration |
| 75 | long duration = mOriginalDuration - currentPlayTime; |
| 76 | mAnimator.setDuration(Math.max(0, Math.min(duration, mOriginalDuration))); |
| 77 | |
| 78 | mAnimator.setFloatValues(startValue, toValue); |
| 79 | mAnimator.start(); |
| 80 | mFirstRun = false; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 83 | public void cancel() { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 84 | mAnimator.cancel(); |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 85 | mDirection = STOPPED; |
| 86 | } |
| 87 | |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 88 | public void end() { |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 89 | mAnimator.end(); |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 90 | mDirection = STOPPED; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return true when the animation is not running and it hasn't even been started. |
| 95 | */ |
| 96 | public boolean isStopped() { |
| 97 | return mDirection == STOPPED; |
| 98 | } |
| 99 | |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 100 | /** |
| 101 | * This is the equivalent of calling Animator.start(), except that it can be called when |
| 102 | * the animation is running in the opposite direction, in which case we reverse |
| 103 | * direction and animate for a correspondingly shorter duration. |
| 104 | */ |
| 105 | public void animateIn() { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 106 | animate(IN); |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * This is the roughly the equivalent of calling Animator.reverse(), except that it uses the |
| 111 | * same interpolation curve as animateIn(), rather than mirroring it. Also, like animateIn(), |
| 112 | * if the animation is currently running in the opposite direction, we reverse |
| 113 | * direction and animate for a correspondingly shorter duration. |
| 114 | */ |
| 115 | public void animateOut() { |
Patrick Dubroy | fe6bd87 | 2010-10-13 17:32:10 -0700 | [diff] [blame] | 116 | animate(OUT); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public void setTag(Object tag) { |
| 120 | mTag = tag; |
| 121 | } |
| 122 | |
| 123 | public Object getTag() { |
| 124 | return mTag; |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 125 | } |
Chet Haase | 472b281 | 2010-10-14 07:02:04 -0700 | [diff] [blame] | 126 | |
| 127 | public ValueAnimator getAnimator() { |
| 128 | return mAnimator; |
| 129 | } |
Winson Chung | 150fbab | 2010-09-29 17:14:26 -0700 | [diff] [blame] | 130 | } |