RenderNodeAnimator.java revision 1c058e96b3fb5075c34b89cf22773373811abf7a
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 17package android.view; 18 19import android.graphics.Canvas; 20import android.graphics.CanvasProperty; 21import android.graphics.Paint; 22import android.util.SparseIntArray; 23 24import java.lang.ref.WeakReference; 25 26/** 27 * @hide 28 */ 29public final class RenderNodeAnimator { 30 31 // Keep in sync with enum RenderProperty in Animator.h 32 public static final int TRANSLATION_X = 0; 33 public static final int TRANSLATION_Y = 1; 34 public static final int TRANSLATION_Z = 2; 35 public static final int SCALE_X = 3; 36 public static final int SCALE_Y = 4; 37 public static final int ROTATION = 5; 38 public static final int ROTATION_X = 6; 39 public static final int ROTATION_Y = 7; 40 public static final int X = 8; 41 public static final int Y = 9; 42 public static final int Z = 10; 43 public static final int ALPHA = 11; 44 45 // Keep in sync with enum PaintFields in Animator.h 46 public static final int PAINT_STROKE_WIDTH = 0; 47 public static final int PAINT_ALPHA = 1; 48 49 // ViewPropertyAnimator uses a mask for its values, we need to remap them 50 // to the enum values here. RenderPropertyAnimator can't use the mask values 51 // directly as internally it uses a lookup table so it needs the values to 52 // be sequential starting from 0 53 private static final SparseIntArray sViewPropertyAnimatorMap = new SparseIntArray(15) {{ 54 put(ViewPropertyAnimator.TRANSLATION_X, TRANSLATION_X); 55 put(ViewPropertyAnimator.TRANSLATION_Y, TRANSLATION_Y); 56 put(ViewPropertyAnimator.TRANSLATION_Z, TRANSLATION_Z); 57 put(ViewPropertyAnimator.SCALE_X, SCALE_X); 58 put(ViewPropertyAnimator.SCALE_Y, SCALE_Y); 59 put(ViewPropertyAnimator.ROTATION, ROTATION); 60 put(ViewPropertyAnimator.ROTATION_X, ROTATION_X); 61 put(ViewPropertyAnimator.ROTATION_Y, ROTATION_Y); 62 put(ViewPropertyAnimator.X, X); 63 put(ViewPropertyAnimator.Y, Y); 64 put(ViewPropertyAnimator.Z, Z); 65 put(ViewPropertyAnimator.ALPHA, ALPHA); 66 }}; 67 68 // Keep in sync DeltaValueType in Animator.h 69 public static final int DELTA_TYPE_ABSOLUTE = 0; 70 public static final int DELTA_TYPE_DELTA = 1; 71 72 private RenderNode mTarget; 73 private long mNativePtr; 74 75 public int mapViewPropertyToRenderProperty(int viewProperty) { 76 return sViewPropertyAnimatorMap.get(viewProperty); 77 } 78 79 public RenderNodeAnimator(int property, int deltaType, float deltaValue) { 80 mNativePtr = nCreateAnimator(new WeakReference<RenderNodeAnimator>(this), 81 property, deltaType, deltaValue); 82 } 83 84 public RenderNodeAnimator(CanvasProperty<Float> property, int deltaType, float deltaValue) { 85 mNativePtr = nCreateCanvasPropertyFloatAnimator( 86 new WeakReference<RenderNodeAnimator>(this), 87 property.getNativeContainer(), deltaType, deltaValue); 88 } 89 90 public RenderNodeAnimator(CanvasProperty<Paint> property, int paintField, 91 int deltaType, float deltaValue) { 92 mNativePtr = nCreateCanvasPropertyPaintAnimator( 93 new WeakReference<RenderNodeAnimator>(this), 94 property.getNativeContainer(), paintField, deltaType, deltaValue); 95 } 96 97 public void start(View target) { 98 mTarget = target.mRenderNode; 99 mTarget.addAnimator(this); 100 // Kick off a frame to start the process 101 target.invalidateViewProperty(true, false); 102 } 103 104 public void start(Canvas canvas) { 105 if (!(canvas instanceof GLES20RecordingCanvas)) { 106 throw new IllegalArgumentException("Not a GLES20RecordingCanvas"); 107 } 108 GLES20RecordingCanvas recordingCanvas = (GLES20RecordingCanvas) canvas; 109 mTarget = recordingCanvas.mNode; 110 mTarget.addAnimator(this); 111 } 112 113 public void cancel() { 114 mTarget.removeAnimator(this); 115 } 116 117 public void setDuration(int duration) { 118 nSetDuration(mNativePtr, duration); 119 } 120 121 long getNativeAnimator() { 122 return mNativePtr; 123 } 124 125 private void onFinished() { 126 mTarget.removeAnimator(this); 127 } 128 129 // Called by native 130 private static void callOnFinished(WeakReference<RenderNodeAnimator> weakThis) { 131 RenderNodeAnimator animator = weakThis.get(); 132 if (animator != null) { 133 animator.onFinished(); 134 } 135 } 136 137 @Override 138 protected void finalize() throws Throwable { 139 try { 140 nUnref(mNativePtr); 141 mNativePtr = 0; 142 } finally { 143 super.finalize(); 144 } 145 } 146 147 private static native long nCreateAnimator(WeakReference<RenderNodeAnimator> weakThis, 148 int property, int deltaValueType, float deltaValue); 149 private static native long nCreateCanvasPropertyFloatAnimator(WeakReference<RenderNodeAnimator> weakThis, 150 long canvasProperty, int deltaValueType, float deltaValue); 151 private static native long nCreateCanvasPropertyPaintAnimator(WeakReference<RenderNodeAnimator> weakThis, 152 long canvasProperty, int paintField, int deltaValueType, float deltaValue); 153 private static native void nSetDuration(long nativePtr, int duration); 154 private static native void nUnref(long nativePtr); 155} 156