blob: 29df38bae13648b0658353c392a679be5a521005 [file] [log] [blame]
Winson Chung150fbab2010-09-29 17:14:26 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung150fbab2010-09-29 17:14:26 -070018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Winson Chung150fbab2010-09-29 17:14:26 -070021import android.animation.ValueAnimator;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010022import android.view.View;
Winson Chung150fbab2010-09-29 17:14:26 -070023
Adam Cohen091440a2015-03-18 14:16:05 -070024import com.android.launcher3.util.Thunk;
25
Winson Chung150fbab2010-09-29 17:14:26 -070026/**
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 Haase472b2812010-10-14 07:02:04 -070033public class InterruptibleInOutAnimator {
Winson Chung150fbab2010-09-29 17:14:26 -070034 private long mOriginalDuration;
Chet Haase472b2812010-10-14 07:02:04 -070035 private float mOriginalFromValue;
36 private float mOriginalToValue;
37 private ValueAnimator mAnimator;
Winson Chung150fbab2010-09-29 17:14:26 -070038
Joe Onorato4be866d2010-10-10 11:26:02 -070039 private boolean mFirstRun = true;
40
41 private Object mTag = null;
42
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070043 private static final int STOPPED = 0;
44 private static final int IN = 1;
45 private static final int OUT = 2;
46
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070047 // TODO: This isn't really necessary, but is here to help diagnose a bug in the drag viz
Adam Cohen091440a2015-03-18 14:16:05 -070048 @Thunk int mDirection = STOPPED;
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070049
Michael Jurkaf1ad6082013-03-13 12:55:46 +010050 public InterruptibleInOutAnimator(View view, long duration, float fromValue, float toValue) {
51 mAnimator = LauncherAnimUtils.ofFloat(view, fromValue, toValue).setDuration(duration);
Winson Chung150fbab2010-09-29 17:14:26 -070052 mOriginalDuration = duration;
53 mOriginalFromValue = fromValue;
54 mOriginalToValue = toValue;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070055
Michael Jurka8edd75c2010-12-17 20:15:06 -080056 mAnimator.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -070057 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -080058 public void onAnimationEnd(Animator animation) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070059 mDirection = STOPPED;
60 }
61 });
Winson Chung150fbab2010-09-29 17:14:26 -070062 }
63
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070064 private void animate(int direction) {
Chet Haase472b2812010-10-14 07:02:04 -070065 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 Dubroyfe6bd872010-10-13 17:32:10 -070069
70 // Make sure it's stopped before we modify any values
Winson Chung150fbab2010-09-29 17:14:26 -070071 cancel();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070072
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070073 // 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 Chung150fbab2010-09-29 17:14:26 -070084 }
85
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070086 public void cancel() {
Chet Haase472b2812010-10-14 07:02:04 -070087 mAnimator.cancel();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070088 mDirection = STOPPED;
89 }
90
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070091 public void end() {
Chet Haase472b2812010-10-14 07:02:04 -070092 mAnimator.end();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070093 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 Chung150fbab2010-09-29 17:14:26 -0700103 /**
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 Dubroyfe6bd872010-10-13 17:32:10 -0700109 animate(IN);
Winson Chung150fbab2010-09-29 17:14:26 -0700110 }
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 Dubroyfe6bd872010-10-13 17:32:10 -0700119 animate(OUT);
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 }
121
122 public void setTag(Object tag) {
123 mTag = tag;
124 }
125
126 public Object getTag() {
127 return mTag;
Winson Chung150fbab2010-09-29 17:14:26 -0700128 }
Chet Haase472b2812010-10-14 07:02:04 -0700129
130 public ValueAnimator getAnimator() {
131 return mAnimator;
132 }
Winson Chung150fbab2010-09-29 17:14:26 -0700133}