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