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