blob: f4395ca63fdcb4f685954cfd1a61d0fb1797de1a [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;
Sunny Goyal849c6a22018-08-08 16:33:46 -070021import android.animation.ObjectAnimator;
Winson Chung150fbab2010-09-29 17:14:26 -070022import android.animation.ValueAnimator;
Sunny Goyal849c6a22018-08-08 16:33:46 -070023import android.util.Property;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010024import android.view.View;
Winson Chung150fbab2010-09-29 17:14:26 -070025
Adam Cohen091440a2015-03-18 14:16:05 -070026import com.android.launcher3.util.Thunk;
27
Winson Chung150fbab2010-09-29 17:14:26 -070028/**
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 Haase472b2812010-10-14 07:02:04 -070035public class InterruptibleInOutAnimator {
Sunny Goyal849c6a22018-08-08 16:33:46 -070036
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 Chung150fbab2010-09-29 17:14:26 -070050 private long mOriginalDuration;
Chet Haase472b2812010-10-14 07:02:04 -070051 private float mOriginalFromValue;
52 private float mOriginalToValue;
53 private ValueAnimator mAnimator;
Winson Chung150fbab2010-09-29 17:14:26 -070054
Sunny Goyal849c6a22018-08-08 16:33:46 -070055 private float mValue;
56
Joe Onorato4be866d2010-10-10 11:26:02 -070057 private boolean mFirstRun = true;
58
59 private Object mTag = null;
60
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070061 private static final int STOPPED = 0;
62 private static final int IN = 1;
63 private static final int OUT = 2;
64
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070065 // 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 -070066 @Thunk int mDirection = STOPPED;
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070067
Sunny Goyal849c6a22018-08-08 16:33:46 -070068 public InterruptibleInOutAnimator(long duration, float fromValue, float toValue) {
69 mAnimator = ObjectAnimator.ofFloat(this, VALUE, fromValue, toValue).setDuration(duration);
Winson Chung150fbab2010-09-29 17:14:26 -070070 mOriginalDuration = duration;
71 mOriginalFromValue = fromValue;
72 mOriginalToValue = toValue;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070073
Michael Jurka8edd75c2010-12-17 20:15:06 -080074 mAnimator.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -070075 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -080076 public void onAnimationEnd(Animator animation) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070077 mDirection = STOPPED;
78 }
79 });
Winson Chung150fbab2010-09-29 17:14:26 -070080 }
81
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070082 private void animate(int direction) {
Chet Haase472b2812010-10-14 07:02:04 -070083 final long currentPlayTime = mAnimator.getCurrentPlayTime();
84 final float toValue = (direction == IN) ? mOriginalToValue : mOriginalFromValue;
Sunny Goyal849c6a22018-08-08 16:33:46 -070085 final float startValue = mFirstRun ? mOriginalFromValue : mValue;
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070086
87 // Make sure it's stopped before we modify any values
Winson Chung150fbab2010-09-29 17:14:26 -070088 cancel();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070089
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070090 // 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 Chung150fbab2010-09-29 17:14:26 -0700101 }
102
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700103 public void cancel() {
Chet Haase472b2812010-10-14 07:02:04 -0700104 mAnimator.cancel();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700105 mDirection = STOPPED;
106 }
107
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700108 public void end() {
Chet Haase472b2812010-10-14 07:02:04 -0700109 mAnimator.end();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700110 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 Chung150fbab2010-09-29 17:14:26 -0700120 /**
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 Dubroyfe6bd872010-10-13 17:32:10 -0700126 animate(IN);
Winson Chung150fbab2010-09-29 17:14:26 -0700127 }
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 Dubroyfe6bd872010-10-13 17:32:10 -0700136 animate(OUT);
Joe Onorato4be866d2010-10-10 11:26:02 -0700137 }
138
139 public void setTag(Object tag) {
140 mTag = tag;
141 }
142
143 public Object getTag() {
144 return mTag;
Winson Chung150fbab2010-09-29 17:14:26 -0700145 }
Chet Haase472b2812010-10-14 07:02:04 -0700146
147 public ValueAnimator getAnimator() {
148 return mAnimator;
149 }
Winson Chung150fbab2010-09-29 17:14:26 -0700150}