blob: 135fa39960b05f9a8dd81b8fe7eca86ef2b4f05e [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
17package com.android.launcher2;
18
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;
Winson Chung150fbab2010-09-29 17:14:26 -070022
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 Haase472b2812010-10-14 07:02:04 -070030public class InterruptibleInOutAnimator {
Winson Chung150fbab2010-09-29 17:14:26 -070031 private long mOriginalDuration;
Chet Haase472b2812010-10-14 07:02:04 -070032 private float mOriginalFromValue;
33 private float mOriginalToValue;
34 private ValueAnimator mAnimator;
Winson Chung150fbab2010-09-29 17:14:26 -070035
Joe Onorato4be866d2010-10-10 11:26:02 -070036 private boolean mFirstRun = true;
37
38 private Object mTag = null;
39
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070040 private static final int STOPPED = 0;
41 private static final int IN = 1;
42 private static final int OUT = 2;
43
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070044 // TODO: This isn't really necessary, but is here to help diagnose a bug in the drag viz
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070045 private int mDirection = STOPPED;
46
Chet Haase472b2812010-10-14 07:02:04 -070047 public InterruptibleInOutAnimator(long duration, float fromValue, float toValue) {
48 mAnimator = ValueAnimator.ofFloat(fromValue, toValue).setDuration(duration);
Winson Chung150fbab2010-09-29 17:14:26 -070049 mOriginalDuration = duration;
50 mOriginalFromValue = fromValue;
51 mOriginalToValue = toValue;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070052
Michael Jurka8edd75c2010-12-17 20:15:06 -080053 mAnimator.addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -070054 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -080055 public void onAnimationEnd(Animator animation) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070056 mDirection = STOPPED;
57 }
58 });
Winson Chung150fbab2010-09-29 17:14:26 -070059 }
60
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070061 private void animate(int direction) {
Chet Haase472b2812010-10-14 07:02:04 -070062 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 Dubroyfe6bd872010-10-13 17:32:10 -070066
67 // Make sure it's stopped before we modify any values
Winson Chung150fbab2010-09-29 17:14:26 -070068 cancel();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070069
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -070070 // 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 Chung150fbab2010-09-29 17:14:26 -070081 }
82
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070083 public void cancel() {
Chet Haase472b2812010-10-14 07:02:04 -070084 mAnimator.cancel();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070085 mDirection = STOPPED;
86 }
87
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070088 public void end() {
Chet Haase472b2812010-10-14 07:02:04 -070089 mAnimator.end();
Patrick Dubroyfe6bd872010-10-13 17:32:10 -070090 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 Chung150fbab2010-09-29 17:14:26 -0700100 /**
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 Dubroyfe6bd872010-10-13 17:32:10 -0700106 animate(IN);
Winson Chung150fbab2010-09-29 17:14:26 -0700107 }
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 Dubroyfe6bd872010-10-13 17:32:10 -0700116 animate(OUT);
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 }
118
119 public void setTag(Object tag) {
120 mTag = tag;
121 }
122
123 public Object getTag() {
124 return mTag;
Winson Chung150fbab2010-09-29 17:14:26 -0700125 }
Chet Haase472b2812010-10-14 07:02:04 -0700126
127 public ValueAnimator getAnimator() {
128 return mAnimator;
129 }
Winson Chung150fbab2010-09-29 17:14:26 -0700130}