blob: d71269f9eaeb105f82ec53849fd69fe1579d28d3 [file] [log] [blame]
Jeff Brown96307042012-07-27 15:51:34 -07001/*
2 * Copyright (C) 2012 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
Jeff Brownad9ef192014-04-08 17:26:30 -070017package com.android.server.display;
Jeff Brown96307042012-07-27 15:51:34 -070018
19import android.animation.ValueAnimator;
20import android.util.IntProperty;
21import android.view.Choreographer;
22
23/**
24 * A custom animator that progressively updates a property value at
25 * a given variable rate until it reaches a particular target value.
26 */
27final class RampAnimator<T> {
28 private final T mObject;
29 private final IntProperty<T> mProperty;
30 private final Choreographer mChoreographer;
31
32 private int mCurrentValue;
33 private int mTargetValue;
34 private int mRate;
35
36 private boolean mAnimating;
37 private float mAnimatedValue; // higher precision copy of mCurrentValue
38 private long mLastFrameTimeNanos;
39
40 private boolean mFirstTime = true;
41
Jeff Brown42558692014-05-20 22:02:46 -070042 private Listener mListener;
43
Jeff Brown96307042012-07-27 15:51:34 -070044 public RampAnimator(T object, IntProperty<T> property) {
45 mObject = object;
46 mProperty = property;
47 mChoreographer = Choreographer.getInstance();
48 }
49
50 /**
51 * Starts animating towards the specified value.
52 *
Jeff Brown0a434772014-09-30 14:42:27 -070053 * If this is the first time the property is being set or if the rate is 0,
54 * the value jumps directly to the target.
Jeff Brown96307042012-07-27 15:51:34 -070055 *
56 * @param target The target value.
Jeff Brown0a434772014-09-30 14:42:27 -070057 * @param rate The convergence rate in units per second, or 0 to set the value immediately.
Jeff Brown96307042012-07-27 15:51:34 -070058 * @return True if the target differs from the previous target.
59 */
60 public boolean animateTo(int target, int rate) {
61 // Immediately jump to the target the first time.
Jeff Brown0a434772014-09-30 14:42:27 -070062 if (mFirstTime || rate <= 0) {
63 if (mFirstTime || target != mCurrentValue) {
64 mFirstTime = false;
65 mRate = 0;
66 mTargetValue = target;
67 mCurrentValue = target;
68 mProperty.setValue(mObject, target);
69 if (mAnimating) {
70 mAnimating = false;
71 cancelAnimationCallback();
72 }
73 if (mListener != null) {
74 mListener.onAnimationEnd();
75 }
76 return true;
77 }
78 return false;
Jeff Brown96307042012-07-27 15:51:34 -070079 }
80
81 // Adjust the rate based on the closest target.
82 // If a faster rate is specified, then use the new rate so that we converge
83 // more rapidly based on the new request.
84 // If a slower rate is specified, then use the new rate only if the current
85 // value is somewhere in between the new and the old target meaning that
86 // we will be ramping in a different direction to get there.
87 // Otherwise, continue at the previous rate.
88 if (!mAnimating
89 || rate > mRate
90 || (target <= mCurrentValue && mCurrentValue <= mTargetValue)
91 || (mTargetValue <= mCurrentValue && mCurrentValue <= target)) {
92 mRate = rate;
93 }
94
95 final boolean changed = (mTargetValue != target);
96 mTargetValue = target;
97
98 // Start animating.
99 if (!mAnimating && target != mCurrentValue) {
100 mAnimating = true;
101 mAnimatedValue = mCurrentValue;
102 mLastFrameTimeNanos = System.nanoTime();
Jeff Brown0a434772014-09-30 14:42:27 -0700103 postAnimationCallback();
Jeff Brown96307042012-07-27 15:51:34 -0700104 }
105
106 return changed;
107 }
108
Jeff Brown42558692014-05-20 22:02:46 -0700109 /**
110 * Returns true if the animation is running.
111 */
112 public boolean isAnimating() {
113 return mAnimating;
114 }
115
116 /**
117 * Sets a listener to watch for animation events.
118 */
119 public void setListener(Listener listener) {
120 mListener = listener;
121 }
122
Jeff Brown0a434772014-09-30 14:42:27 -0700123 private void postAnimationCallback() {
124 mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, mAnimationCallback, null);
Jeff Brown96307042012-07-27 15:51:34 -0700125 }
126
Jeff Brown0a434772014-09-30 14:42:27 -0700127 private void cancelAnimationCallback() {
128 mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION, mAnimationCallback, null);
129 }
130
131 private final Runnable mAnimationCallback = new Runnable() {
Jeff Brown96307042012-07-27 15:51:34 -0700132 @Override // Choreographer callback
133 public void run() {
134 final long frameTimeNanos = mChoreographer.getFrameTimeNanos();
135 final float timeDelta = (frameTimeNanos - mLastFrameTimeNanos)
136 * 0.000000001f;
Jeff Brown96307042012-07-27 15:51:34 -0700137 mLastFrameTimeNanos = frameTimeNanos;
138
139 // Advance the animated value towards the target at the specified rate
140 // and clamp to the target. This gives us the new current value but
141 // we keep the animated value around to allow for fractional increments
142 // towards the target.
Craig Mautnera7233fe32012-11-15 14:25:14 -0800143 final float scale = ValueAnimator.getDurationScale();
144 if (scale == 0) {
145 // Animation off.
146 mAnimatedValue = mTargetValue;
Jeff Brown96307042012-07-27 15:51:34 -0700147 } else {
Craig Mautnera7233fe32012-11-15 14:25:14 -0800148 final float amount = timeDelta * mRate / scale;
149 if (mTargetValue > mCurrentValue) {
150 mAnimatedValue = Math.min(mAnimatedValue + amount, mTargetValue);
151 } else {
152 mAnimatedValue = Math.max(mAnimatedValue - amount, mTargetValue);
153 }
Jeff Brown96307042012-07-27 15:51:34 -0700154 }
Craig Mautnera7233fe32012-11-15 14:25:14 -0800155 final int oldCurrentValue = mCurrentValue;
156 mCurrentValue = Math.round(mAnimatedValue);
Jeff Brown96307042012-07-27 15:51:34 -0700157
158 if (oldCurrentValue != mCurrentValue) {
159 mProperty.setValue(mObject, mCurrentValue);
160 }
161
162 if (mTargetValue != mCurrentValue) {
Jeff Brown0a434772014-09-30 14:42:27 -0700163 postAnimationCallback();
Jeff Brown96307042012-07-27 15:51:34 -0700164 } else {
165 mAnimating = false;
Jeff Brown42558692014-05-20 22:02:46 -0700166 if (mListener != null) {
167 mListener.onAnimationEnd();
168 }
Jeff Brown96307042012-07-27 15:51:34 -0700169 }
170 }
171 };
Jeff Brown42558692014-05-20 22:02:46 -0700172
173 public interface Listener {
174 void onAnimationEnd();
175 }
Jeff Brown96307042012-07-27 15:51:34 -0700176}