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