John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package android.view; |
| 18 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 20 | import android.animation.TimeInterpolator; |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
John Reck | 1c058e9 | 2014-05-02 14:20:53 -0700 | [diff] [blame] | 22 | import android.graphics.Canvas; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 23 | import android.graphics.CanvasProperty; |
| 24 | import android.graphics.Paint; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 25 | import android.util.SparseIntArray; |
| 26 | |
John Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 27 | import com.android.internal.util.VirtualRefBasePtr; |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 28 | import com.android.internal.view.animation.FallbackLUTInterpolator; |
| 29 | import com.android.internal.view.animation.HasNativeInterpolator; |
| 30 | import com.android.internal.view.animation.NativeInterpolatorFactory; |
John Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 31 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 32 | import java.util.ArrayList; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * @hide |
| 36 | */ |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 37 | public class RenderNodeAnimator extends Animator { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 38 | // Keep in sync with enum RenderProperty in Animator.h |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 39 | public static final int TRANSLATION_X = 0; |
| 40 | public static final int TRANSLATION_Y = 1; |
| 41 | public static final int TRANSLATION_Z = 2; |
| 42 | public static final int SCALE_X = 3; |
| 43 | public static final int SCALE_Y = 4; |
| 44 | public static final int ROTATION = 5; |
| 45 | public static final int ROTATION_X = 6; |
| 46 | public static final int ROTATION_Y = 7; |
| 47 | public static final int X = 8; |
| 48 | public static final int Y = 9; |
| 49 | public static final int Z = 10; |
| 50 | public static final int ALPHA = 11; |
John Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 51 | // The last value in the enum, used for array size initialization |
| 52 | public static final int LAST_VALUE = ALPHA; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 53 | |
| 54 | // Keep in sync with enum PaintFields in Animator.h |
| 55 | public static final int PAINT_STROKE_WIDTH = 0; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Field for the Paint alpha channel, which should be specified as a value |
| 59 | * between 0 and 255. |
| 60 | */ |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 61 | public static final int PAINT_ALPHA = 1; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 62 | |
| 63 | // ViewPropertyAnimator uses a mask for its values, we need to remap them |
| 64 | // to the enum values here. RenderPropertyAnimator can't use the mask values |
| 65 | // directly as internally it uses a lookup table so it needs the values to |
| 66 | // be sequential starting from 0 |
| 67 | private static final SparseIntArray sViewPropertyAnimatorMap = new SparseIntArray(15) {{ |
| 68 | put(ViewPropertyAnimator.TRANSLATION_X, TRANSLATION_X); |
| 69 | put(ViewPropertyAnimator.TRANSLATION_Y, TRANSLATION_Y); |
| 70 | put(ViewPropertyAnimator.TRANSLATION_Z, TRANSLATION_Z); |
| 71 | put(ViewPropertyAnimator.SCALE_X, SCALE_X); |
| 72 | put(ViewPropertyAnimator.SCALE_Y, SCALE_Y); |
| 73 | put(ViewPropertyAnimator.ROTATION, ROTATION); |
| 74 | put(ViewPropertyAnimator.ROTATION_X, ROTATION_X); |
| 75 | put(ViewPropertyAnimator.ROTATION_Y, ROTATION_Y); |
| 76 | put(ViewPropertyAnimator.X, X); |
| 77 | put(ViewPropertyAnimator.Y, Y); |
| 78 | put(ViewPropertyAnimator.Z, Z); |
| 79 | put(ViewPropertyAnimator.ALPHA, ALPHA); |
| 80 | }}; |
| 81 | |
John Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 82 | private VirtualRefBasePtr mNativePtr; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 83 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 84 | private RenderNode mTarget; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 85 | private View mViewTarget; |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 86 | private int mRenderProperty = -1; |
| 87 | private float mFinalValue; |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 88 | private TimeInterpolator mInterpolator; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 89 | |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 90 | private static final int STATE_PREPARE = 0; |
| 91 | private static final int STATE_DELAYED = 1; |
| 92 | private static final int STATE_RUNNING = 2; |
| 93 | private static final int STATE_FINISHED = 3; |
| 94 | private int mState = STATE_PREPARE; |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 95 | |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 96 | private long mUnscaledDuration = 300; |
| 97 | private long mUnscaledStartDelay = 0; |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 98 | // If this is true, we will run any start delays on the UI thread. This is |
| 99 | // the safe default, and is necessary to ensure start listeners fire at |
| 100 | // the correct time. Animators created by RippleDrawable (the |
| 101 | // CanvasProperty<> ones) do not have this expectation, and as such will |
| 102 | // set this to false so that the renderthread handles the startdelay instead |
| 103 | private final boolean mUiThreadHandlesDelay; |
| 104 | private long mStartDelay = 0; |
| 105 | private long mStartTime; |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 106 | |
John Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 107 | public static int mapViewPropertyToRenderProperty(int viewProperty) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 108 | return sViewPropertyAnimatorMap.get(viewProperty); |
| 109 | } |
| 110 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 111 | public RenderNodeAnimator(int property, float finalValue) { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 112 | mRenderProperty = property; |
| 113 | mFinalValue = finalValue; |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 114 | mUiThreadHandlesDelay = true; |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 115 | init(nCreateAnimator(property, finalValue)); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 116 | } |
| 117 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 118 | public RenderNodeAnimator(CanvasProperty<Float> property, float finalValue) { |
John Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 119 | init(nCreateCanvasPropertyFloatAnimator( |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 120 | property.getNativeContainer(), finalValue)); |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 121 | mUiThreadHandlesDelay = false; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Creates a new render node animator for a field on a Paint property. |
| 126 | * |
| 127 | * @param property The paint property to target |
| 128 | * @param paintField Paint field to animate, one of {@link #PAINT_ALPHA} or |
| 129 | * {@link #PAINT_STROKE_WIDTH} |
| 130 | * @param finalValue The target value for the property |
| 131 | */ |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 132 | public RenderNodeAnimator(CanvasProperty<Paint> property, int paintField, float finalValue) { |
John Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 133 | init(nCreateCanvasPropertyPaintAnimator( |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 134 | property.getNativeContainer(), paintField, finalValue)); |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 135 | mUiThreadHandlesDelay = false; |
John Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 138 | public RenderNodeAnimator(int x, int y, float startRadius, float endRadius) { |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 139 | init(nCreateRevealAnimator(x, y, startRadius, endRadius)); |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 140 | mUiThreadHandlesDelay = true; |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 141 | } |
| 142 | |
John Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 143 | private void init(long ptr) { |
| 144 | mNativePtr = new VirtualRefBasePtr(ptr); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 145 | } |
| 146 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 147 | private void checkMutable() { |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 148 | if (mState != STATE_PREPARE) { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 149 | throw new IllegalStateException("Animator has already started, cannot change it now!"); |
| 150 | } |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 151 | if (mNativePtr == null) { |
| 152 | throw new IllegalStateException("Animator's target has been destroyed " |
| 153 | + "(trying to modify an animation after activity destroy?)"); |
| 154 | } |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 155 | } |
| 156 | |
John Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 157 | static boolean isNativeInterpolator(TimeInterpolator interpolator) { |
| 158 | return interpolator.getClass().isAnnotationPresent(HasNativeInterpolator.class); |
| 159 | } |
| 160 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 161 | private void applyInterpolator() { |
| 162 | if (mInterpolator == null) return; |
| 163 | |
| 164 | long ni; |
John Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 165 | if (isNativeInterpolator(mInterpolator)) { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 166 | ni = ((NativeInterpolatorFactory)mInterpolator).createNativeInterpolator(); |
| 167 | } else { |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 168 | long duration = nGetDuration(mNativePtr.get()); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 169 | ni = FallbackLUTInterpolator.createNativeInterpolator(mInterpolator, duration); |
| 170 | } |
| 171 | nSetInterpolator(mNativePtr.get(), ni); |
| 172 | } |
| 173 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 174 | @Override |
| 175 | public void start() { |
| 176 | if (mTarget == null) { |
| 177 | throw new IllegalStateException("Missing target!"); |
| 178 | } |
| 179 | |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 180 | if (mState != STATE_PREPARE) { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 181 | throw new IllegalStateException("Already started!"); |
| 182 | } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 183 | |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 184 | mState = STATE_DELAYED; |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 185 | applyInterpolator(); |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 186 | |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 187 | if (mNativePtr == null) { |
| 188 | // It's dead, immediately cancel |
| 189 | cancel(); |
| 190 | } else if (mStartDelay <= 0 || !mUiThreadHandlesDelay) { |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 191 | nSetStartDelay(mNativePtr.get(), mStartDelay); |
| 192 | doStart(); |
| 193 | } else { |
| 194 | getHelper().addDelayedAnimation(this); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private void doStart() { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 199 | // Alpha is a special snowflake that has the canonical value stored |
| 200 | // in mTransformationInfo instead of in RenderNode, so we need to update |
| 201 | // it with the final value here. |
| 202 | if (mRenderProperty == RenderNodeAnimator.ALPHA) { |
| 203 | // Don't need null check because ViewPropertyAnimator's |
| 204 | // ctor calls ensureTransformationInfo() |
| 205 | mViewTarget.mTransformationInfo.mAlpha = mFinalValue; |
| 206 | } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 207 | |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 208 | moveToRunningState(); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 209 | |
| 210 | if (mViewTarget != null) { |
| 211 | // Kick off a frame to start the process |
| 212 | mViewTarget.invalidateViewProperty(true, false); |
| 213 | } |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 214 | } |
| 215 | |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 216 | private void moveToRunningState() { |
| 217 | mState = STATE_RUNNING; |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 218 | if (mNativePtr != null) { |
| 219 | nStart(mNativePtr.get()); |
| 220 | } |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 221 | notifyStartListeners(); |
| 222 | } |
| 223 | |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 224 | private void notifyStartListeners() { |
| 225 | final ArrayList<AnimatorListener> listeners = cloneListeners(); |
| 226 | final int numListeners = listeners == null ? 0 : listeners.size(); |
| 227 | for (int i = 0; i < numListeners; i++) { |
| 228 | listeners.get(i).onAnimationStart(this); |
| 229 | } |
| 230 | } |
| 231 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 232 | @Override |
| 233 | public void cancel() { |
John Reck | 55b46ef | 2014-11-03 10:00:33 -0800 | [diff] [blame] | 234 | if (mState != STATE_PREPARE && mState != STATE_FINISHED) { |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 235 | if (mState == STATE_DELAYED) { |
| 236 | getHelper().removeDelayedAnimation(this); |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 237 | moveToRunningState(); |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 238 | } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 239 | |
John Reck | 3b27e59 | 2014-08-21 12:37:48 -0700 | [diff] [blame] | 240 | final ArrayList<AnimatorListener> listeners = cloneListeners(); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 241 | final int numListeners = listeners == null ? 0 : listeners.size(); |
| 242 | for (int i = 0; i < numListeners; i++) { |
| 243 | listeners.get(i).onAnimationCancel(this); |
| 244 | } |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 245 | |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 246 | end(); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 247 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 250 | @Override |
| 251 | public void end() { |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 252 | if (mState != STATE_FINISHED) { |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 253 | if (mState < STATE_RUNNING) { |
| 254 | getHelper().removeDelayedAnimation(this); |
| 255 | doStart(); |
| 256 | } |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 257 | if (mNativePtr != null) { |
| 258 | nEnd(mNativePtr.get()); |
| 259 | if (mViewTarget != null) { |
| 260 | // Kick off a frame to flush the state change |
| 261 | mViewTarget.invalidateViewProperty(true, false); |
| 262 | } |
| 263 | } else { |
| 264 | // It's already dead, jump to onFinish |
| 265 | onFinished(); |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 266 | } |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 267 | } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void pause() { |
| 272 | throw new UnsupportedOperationException(); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void resume() { |
| 277 | throw new UnsupportedOperationException(); |
| 278 | } |
| 279 | |
| 280 | public void setTarget(View view) { |
| 281 | mViewTarget = view; |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 282 | setTarget(mViewTarget.mRenderNode); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | public void setTarget(Canvas canvas) { |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 286 | if (!(canvas instanceof DisplayListCanvas)) { |
John Reck | 1c058e9 | 2014-05-02 14:20:53 -0700 | [diff] [blame] | 287 | throw new IllegalArgumentException("Not a GLES20RecordingCanvas"); |
| 288 | } |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 289 | final DisplayListCanvas recordingCanvas = (DisplayListCanvas) canvas; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 290 | setTarget(recordingCanvas.mNode); |
John Reck | 1c058e9 | 2014-05-02 14:20:53 -0700 | [diff] [blame] | 291 | } |
| 292 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 293 | private void setTarget(RenderNode node) { |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 294 | checkMutable(); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 295 | if (mTarget != null) { |
| 296 | throw new IllegalStateException("Target already set!"); |
| 297 | } |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 298 | nSetListener(mNativePtr.get(), this); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 299 | mTarget = node; |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 300 | mTarget.addAnimator(this); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 301 | } |
| 302 | |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 303 | public void setStartValue(float startValue) { |
| 304 | checkMutable(); |
| 305 | nSetStartValue(mNativePtr.get(), startValue); |
| 306 | } |
| 307 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 308 | @Override |
| 309 | public void setStartDelay(long startDelay) { |
| 310 | checkMutable(); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 311 | if (startDelay < 0) { |
| 312 | throw new IllegalArgumentException("startDelay must be positive; " + startDelay); |
| 313 | } |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 314 | mUnscaledStartDelay = startDelay; |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 315 | mStartDelay = (long) (ValueAnimator.getDurationScale() * startDelay); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public long getStartDelay() { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 320 | return mUnscaledStartDelay; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public RenderNodeAnimator setDuration(long duration) { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 325 | checkMutable(); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 326 | if (duration < 0) { |
| 327 | throw new IllegalArgumentException("duration must be positive; " + duration); |
| 328 | } |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 329 | mUnscaledDuration = duration; |
| 330 | nSetDuration(mNativePtr.get(), (long) (duration * ValueAnimator.getDurationScale())); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 331 | return this; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 334 | @Override |
| 335 | public long getDuration() { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 336 | return mUnscaledDuration; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 339 | @Override |
| 340 | public long getTotalDuration() { |
| 341 | return mUnscaledDuration + mUnscaledStartDelay; |
| 342 | } |
| 343 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 344 | @Override |
| 345 | public boolean isRunning() { |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 346 | return mState == STATE_DELAYED || mState == STATE_RUNNING; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 350 | public boolean isStarted() { |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 351 | return mState != STATE_PREPARE; |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Override |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 355 | public void setInterpolator(TimeInterpolator interpolator) { |
| 356 | checkMutable(); |
| 357 | mInterpolator = interpolator; |
| 358 | } |
| 359 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 360 | @Override |
| 361 | public TimeInterpolator getInterpolator() { |
| 362 | return mInterpolator; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 363 | } |
| 364 | |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 365 | protected void onFinished() { |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 366 | if (mState == STATE_PREPARE) { |
| 367 | // Unlikely but possible, the native side has been destroyed |
| 368 | // before we have started. |
| 369 | releaseNativePtr(); |
| 370 | return; |
| 371 | } |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 372 | if (mState == STATE_DELAYED) { |
| 373 | getHelper().removeDelayedAnimation(this); |
| 374 | notifyStartListeners(); |
| 375 | } |
| 376 | mState = STATE_FINISHED; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 377 | |
John Reck | 3b27e59 | 2014-08-21 12:37:48 -0700 | [diff] [blame] | 378 | final ArrayList<AnimatorListener> listeners = cloneListeners(); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 379 | final int numListeners = listeners == null ? 0 : listeners.size(); |
| 380 | for (int i = 0; i < numListeners; i++) { |
| 381 | listeners.get(i).onAnimationEnd(this); |
| 382 | } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 383 | |
| 384 | // Release the native object, as it has a global reference to us. This |
| 385 | // breaks the cyclic reference chain, and allows this object to be |
| 386 | // GC'd |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 387 | releaseNativePtr(); |
| 388 | } |
| 389 | |
| 390 | private void releaseNativePtr() { |
| 391 | if (mNativePtr != null) { |
| 392 | mNativePtr.release(); |
| 393 | mNativePtr = null; |
| 394 | } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 395 | } |
| 396 | |
John Reck | 3b27e59 | 2014-08-21 12:37:48 -0700 | [diff] [blame] | 397 | @SuppressWarnings("unchecked") |
| 398 | private ArrayList<AnimatorListener> cloneListeners() { |
| 399 | ArrayList<AnimatorListener> listeners = getListeners(); |
| 400 | if (listeners != null) { |
| 401 | listeners = (ArrayList<AnimatorListener>) listeners.clone(); |
| 402 | } |
| 403 | return listeners; |
| 404 | } |
| 405 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 406 | long getNativeAnimator() { |
| 407 | return mNativePtr.get(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 408 | } |
| 409 | |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 410 | /** |
| 411 | * @return true if the animator was started, false if still delayed |
| 412 | */ |
| 413 | private boolean processDelayed(long frameTimeMs) { |
| 414 | if (mStartTime == 0) { |
| 415 | mStartTime = frameTimeMs; |
| 416 | } else if ((frameTimeMs - mStartTime) >= mStartDelay) { |
| 417 | doStart(); |
| 418 | return true; |
| 419 | } |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | private static DelayedAnimationHelper getHelper() { |
| 424 | DelayedAnimationHelper helper = sAnimationHelper.get(); |
| 425 | if (helper == null) { |
| 426 | helper = new DelayedAnimationHelper(); |
| 427 | sAnimationHelper.set(helper); |
| 428 | } |
| 429 | return helper; |
| 430 | } |
| 431 | |
| 432 | private static ThreadLocal<DelayedAnimationHelper> sAnimationHelper = |
| 433 | new ThreadLocal<DelayedAnimationHelper>(); |
| 434 | |
| 435 | private static class DelayedAnimationHelper implements Runnable { |
| 436 | |
| 437 | private ArrayList<RenderNodeAnimator> mDelayedAnims = new ArrayList<RenderNodeAnimator>(); |
| 438 | private final Choreographer mChoreographer; |
| 439 | private boolean mCallbackScheduled; |
| 440 | |
| 441 | public DelayedAnimationHelper() { |
| 442 | mChoreographer = Choreographer.getInstance(); |
| 443 | } |
| 444 | |
| 445 | public void addDelayedAnimation(RenderNodeAnimator animator) { |
| 446 | mDelayedAnims.add(animator); |
| 447 | scheduleCallback(); |
| 448 | } |
| 449 | |
| 450 | public void removeDelayedAnimation(RenderNodeAnimator animator) { |
| 451 | mDelayedAnims.remove(animator); |
| 452 | } |
| 453 | |
| 454 | private void scheduleCallback() { |
| 455 | if (!mCallbackScheduled) { |
| 456 | mCallbackScheduled = true; |
| 457 | mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public void run() { |
| 463 | long frameTimeMs = mChoreographer.getFrameTime(); |
| 464 | mCallbackScheduled = false; |
| 465 | |
| 466 | int end = 0; |
| 467 | for (int i = 0; i < mDelayedAnims.size(); i++) { |
| 468 | RenderNodeAnimator animator = mDelayedAnims.get(i); |
| 469 | if (!animator.processDelayed(frameTimeMs)) { |
| 470 | if (end != i) { |
| 471 | mDelayedAnims.set(end, animator); |
| 472 | } |
| 473 | end++; |
| 474 | } |
| 475 | } |
| 476 | while (mDelayedAnims.size() > end) { |
| 477 | mDelayedAnims.remove(mDelayedAnims.size() - 1); |
| 478 | } |
| 479 | |
| 480 | if (mDelayedAnims.size() > 0) { |
| 481 | scheduleCallback(); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 486 | // Called by native |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 487 | private static void callOnFinished(RenderNodeAnimator animator) { |
| 488 | animator.onFinished(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 489 | } |
| 490 | |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 491 | @Override |
| 492 | public Animator clone() { |
| 493 | throw new IllegalStateException("Cannot clone this animator"); |
| 494 | } |
| 495 | |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 496 | @Override |
| 497 | public void setAllowRunningAsynchronously(boolean mayRunAsync) { |
| 498 | checkMutable(); |
| 499 | nSetAllowRunningAsync(mNativePtr.get(), mayRunAsync); |
| 500 | } |
| 501 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 502 | private static native long nCreateAnimator(int property, float finalValue); |
| 503 | private static native long nCreateCanvasPropertyFloatAnimator( |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 504 | long canvasProperty, float finalValue); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 505 | private static native long nCreateCanvasPropertyPaintAnimator( |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 506 | long canvasProperty, int paintField, float finalValue); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 507 | private static native long nCreateRevealAnimator( |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 508 | int x, int y, float startRadius, float endRadius); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 509 | |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 510 | private static native void nSetStartValue(long nativePtr, float startValue); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 511 | private static native void nSetDuration(long nativePtr, long duration); |
| 512 | private static native long nGetDuration(long nativePtr); |
| 513 | private static native void nSetStartDelay(long nativePtr, long startDelay); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 514 | private static native void nSetInterpolator(long animPtr, long interpolatorPtr); |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 515 | private static native void nSetAllowRunningAsync(long animPtr, boolean mayRunAsync); |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 516 | private static native void nSetListener(long animPtr, RenderNodeAnimator listener); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 517 | |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 518 | private static native void nStart(long animPtr); |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 519 | private static native void nEnd(long animPtr); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 520 | } |