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.transition; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorSet; 21 import android.animation.TypeEvaluator; 22 import android.graphics.Bitmap; 23 import android.graphics.Matrix; 24 import android.graphics.Rect; 25 import android.graphics.RectF; 26 import android.graphics.drawable.BitmapDrawable; 27 import android.graphics.drawable.Drawable; 28 import android.view.DisplayListCanvas; 29 import android.view.RenderNode; 30 import android.view.ThreadedRenderer; 31 import android.view.View; 32 import android.view.ViewGroup; 33 import android.widget.ImageView; 34 35 /** 36 * Static utility methods for Transitions. 37 * 38 * @hide 39 */ 40 public class TransitionUtils { 41 private static int MAX_IMAGE_SIZE = (1024 * 1024); 42 mergeAnimators(Animator animator1, Animator animator2)43 static Animator mergeAnimators(Animator animator1, Animator animator2) { 44 if (animator1 == null) { 45 return animator2; 46 } else if (animator2 == null) { 47 return animator1; 48 } else { 49 AnimatorSet animatorSet = new AnimatorSet(); 50 animatorSet.playTogether(animator1, animator2); 51 return animatorSet; 52 } 53 } 54 mergeTransitions(Transition... transitions)55 public static Transition mergeTransitions(Transition... transitions) { 56 int count = 0; 57 int nonNullIndex = -1; 58 for (int i = 0; i < transitions.length; i++) { 59 if (transitions[i] != null) { 60 count++; 61 nonNullIndex = i; 62 } 63 } 64 65 if (count == 0) { 66 return null; 67 } 68 69 if (count == 1) { 70 return transitions[nonNullIndex]; 71 } 72 73 TransitionSet transitionSet = new TransitionSet(); 74 for (int i = 0; i < transitions.length; i++) { 75 if (transitions[i] != null) { 76 transitionSet.addTransition(transitions[i]); 77 } 78 } 79 return transitionSet; 80 } 81 82 /** 83 * Creates a View using the bitmap copy of <code>view</code>. If <code>view</code> is large, 84 * the copy will use a scaled bitmap of the given view. 85 * 86 * @param sceneRoot The ViewGroup in which the view copy will be displayed. 87 * @param view The view to create a copy of. 88 * @param parent The parent of view. 89 */ copyViewImage(ViewGroup sceneRoot, View view, View parent)90 public static View copyViewImage(ViewGroup sceneRoot, View view, View parent) { 91 Matrix matrix = new Matrix(); 92 matrix.setTranslate(-parent.getScrollX(), -parent.getScrollY()); 93 view.transformMatrixToGlobal(matrix); 94 sceneRoot.transformMatrixToLocal(matrix); 95 RectF bounds = new RectF(0, 0, view.getWidth(), view.getHeight()); 96 matrix.mapRect(bounds); 97 int left = Math.round(bounds.left); 98 int top = Math.round(bounds.top); 99 int right = Math.round(bounds.right); 100 int bottom = Math.round(bounds.bottom); 101 102 ImageView copy = new ImageView(view.getContext()); 103 copy.setScaleType(ImageView.ScaleType.CENTER_CROP); 104 Bitmap bitmap = createViewBitmap(view, matrix, bounds, sceneRoot); 105 if (bitmap != null) { 106 copy.setImageBitmap(bitmap); 107 } 108 int widthSpec = View.MeasureSpec.makeMeasureSpec(right - left, View.MeasureSpec.EXACTLY); 109 int heightSpec = View.MeasureSpec.makeMeasureSpec(bottom - top, View.MeasureSpec.EXACTLY); 110 copy.measure(widthSpec, heightSpec); 111 copy.layout(left, top, right, bottom); 112 return copy; 113 } 114 115 /** 116 * Get a copy of bitmap of given drawable, return null if intrinsic size is zero 117 */ createDrawableBitmap(Drawable drawable, View hostView)118 public static Bitmap createDrawableBitmap(Drawable drawable, View hostView) { 119 int width = drawable.getIntrinsicWidth(); 120 int height = drawable.getIntrinsicHeight(); 121 if (width <= 0 || height <= 0) { 122 return null; 123 } 124 float scale = Math.min(1f, ((float)MAX_IMAGE_SIZE) / (width * height)); 125 if (drawable instanceof BitmapDrawable && scale == 1f) { 126 // return same bitmap if scale down not needed 127 return ((BitmapDrawable) drawable).getBitmap(); 128 } 129 int bitmapWidth = (int) (width * scale); 130 int bitmapHeight = (int) (height * scale); 131 final RenderNode node = RenderNode.create("TransitionUtils", hostView); 132 node.setLeftTopRightBottom(0, 0, width, height); 133 node.setClipToBounds(false); 134 final DisplayListCanvas canvas = node.start(width, height); 135 // Do stuff with the canvas 136 Rect existingBounds = drawable.getBounds(); 137 int left = existingBounds.left; 138 int top = existingBounds.top; 139 int right = existingBounds.right; 140 int bottom = existingBounds.bottom; 141 drawable.setBounds(0, 0, bitmapWidth, bitmapHeight); 142 drawable.draw(canvas); 143 drawable.setBounds(left, top, right, bottom); 144 node.end(canvas); 145 return ThreadedRenderer.createHardwareBitmap(node, width, height); 146 } 147 148 /** 149 * Creates a Bitmap of the given view, using the Matrix matrix to transform to the local 150 * coordinates. <code>matrix</code> will be modified during the bitmap creation. 151 * 152 * <p>If the bitmap is large, it will be scaled uniformly down to at most 1MB size.</p> 153 * @param view The view to create a bitmap for. 154 * @param matrix The matrix converting the view local coordinates to the coordinates that 155 * the bitmap will be displayed in. <code>matrix</code> will be modified before 156 * returning. 157 * @param bounds The bounds of the bitmap in the destination coordinate system (where the 158 * view should be presented. Typically, this is matrix.mapRect(viewBounds); 159 * @param sceneRoot A ViewGroup that is attached to the window to temporarily contain the view 160 * if it isn't attached to the window. 161 * @return A bitmap of the given view or null if bounds has no width or height. 162 */ createViewBitmap(View view, Matrix matrix, RectF bounds, ViewGroup sceneRoot)163 public static Bitmap createViewBitmap(View view, Matrix matrix, RectF bounds, 164 ViewGroup sceneRoot) { 165 final boolean addToOverlay = !view.isAttachedToWindow(); 166 if (addToOverlay) { 167 if (sceneRoot == null || !sceneRoot.isAttachedToWindow()) { 168 return null; 169 } 170 sceneRoot.getOverlay().add(view); 171 } 172 Bitmap bitmap = null; 173 int bitmapWidth = Math.round(bounds.width()); 174 int bitmapHeight = Math.round(bounds.height()); 175 if (bitmapWidth > 0 && bitmapHeight > 0) { 176 float scale = Math.min(1f, ((float) MAX_IMAGE_SIZE) / (bitmapWidth * bitmapHeight)); 177 bitmapWidth *= scale; 178 bitmapHeight *= scale; 179 matrix.postTranslate(-bounds.left, -bounds.top); 180 matrix.postScale(scale, scale); 181 182 final RenderNode node = RenderNode.create("TransitionUtils", view); 183 node.setLeftTopRightBottom(0, 0, bitmapWidth, bitmapHeight); 184 node.setClipToBounds(false); 185 final DisplayListCanvas canvas = node.start(bitmapWidth, bitmapHeight); 186 canvas.concat(matrix); 187 view.draw(canvas); 188 node.end(canvas); 189 bitmap = ThreadedRenderer.createHardwareBitmap(node, bitmapWidth, bitmapHeight); 190 } 191 if (addToOverlay) { 192 sceneRoot.getOverlay().remove(view); 193 } 194 return bitmap; 195 } 196 197 public static class MatrixEvaluator implements TypeEvaluator<Matrix> { 198 199 float[] mTempStartValues = new float[9]; 200 201 float[] mTempEndValues = new float[9]; 202 203 Matrix mTempMatrix = new Matrix(); 204 205 @Override evaluate(float fraction, Matrix startValue, Matrix endValue)206 public Matrix evaluate(float fraction, Matrix startValue, Matrix endValue) { 207 startValue.getValues(mTempStartValues); 208 endValue.getValues(mTempEndValues); 209 for (int i = 0; i < 9; i++) { 210 float diff = mTempEndValues[i] - mTempStartValues[i]; 211 mTempEndValues[i] = mTempStartValues[i] + (fraction * diff); 212 } 213 mTempMatrix.setValues(mTempEndValues); 214 return mTempMatrix; 215 } 216 } 217 } 218