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.Canvas; 24 import android.graphics.Matrix; 25 import android.graphics.RectF; 26 import android.graphics.drawable.BitmapDrawable; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.ImageView; 30 31 /** 32 * Static utility methods for Transitions. 33 * 34 * @hide 35 */ 36 public class TransitionUtils { 37 private static int MAX_IMAGE_SIZE = (1024 * 1024); 38 39 static Animator mergeAnimators(Animator animator1, Animator animator2) { 40 if (animator1 == null) { 41 return animator2; 42 } else if (animator2 == null) { 43 return animator1; 44 } else { 45 AnimatorSet animatorSet = new AnimatorSet(); 46 animatorSet.playTogether(animator1, animator2); 47 return animatorSet; 48 } 49 } 50 51 public static Transition mergeTransitions(Transition... transitions) { 52 int count = 0; 53 int nonNullIndex = -1; 54 for (int i = 0; i < transitions.length; i++) { 55 if (transitions[i] != null) { 56 count++; 57 nonNullIndex = i; 58 } 59 } 60 61 if (count == 0) { 62 return null; 63 } 64 65 if (count == 1) { 66 return transitions[nonNullIndex]; 67 } 68 69 TransitionSet transitionSet = new TransitionSet(); 70 for (int i = 0; i < transitions.length; i++) { 71 if (transitions[i] != null) { 72 transitionSet.addTransition(transitions[i]); 73 } 74 } 75 return transitionSet; 76 } 77 78 /** 79 * Creates a View using the bitmap copy of <code>view</code>. If <code>view</code> is large, 80 * the copy will use a scaled bitmap of the given view. 81 * 82 * @param sceneRoot The ViewGroup in which the view copy will be displayed. 83 * @param view The view to create a copy of. 84 * @param parent The parent of view. 85 */ 86 public static View copyViewImage(ViewGroup sceneRoot, View view, View parent) { 87 Matrix matrix = new Matrix(); 88 matrix.setTranslate(-parent.getScrollX(), -parent.getScrollY()); 89 view.transformMatrixToGlobal(matrix); 90 sceneRoot.transformMatrixToLocal(matrix); 91 RectF bounds = new RectF(0, 0, view.getWidth(), view.getHeight()); 92 matrix.mapRect(bounds); 93 int left = Math.round(bounds.left); 94 int top = Math.round(bounds.top); 95 int right = Math.round(bounds.right); 96 int bottom = Math.round(bounds.bottom); 97 98 ImageView copy = new ImageView(view.getContext()); 99 copy.setScaleType(ImageView.ScaleType.CENTER_CROP); 100 Bitmap bitmap = createViewBitmap(view, matrix, bounds); 101 if (bitmap != null) { 102 copy.setImageBitmap(bitmap); 103 } 104 int widthSpec = View.MeasureSpec.makeMeasureSpec(right - left, View.MeasureSpec.EXACTLY); 105 int heightSpec = View.MeasureSpec.makeMeasureSpec(bottom - top, View.MeasureSpec.EXACTLY); 106 copy.measure(widthSpec, heightSpec); 107 copy.layout(left, top, right, bottom); 108 return copy; 109 } 110 111 /** 112 * Creates a Bitmap of the given view, using the Matrix matrix to transform to the local 113 * coordinates. <code>matrix</code> will be modified during the bitmap creation. 114 * 115 * <p>If the bitmap is large, it will be scaled uniformly down to at most 1MB size.</p> 116 * @param view The view to create a bitmap for. 117 * @param matrix The matrix converting the view local coordinates to the coordinates that 118 * the bitmap will be displayed in. <code>matrix</code> will be modified before 119 * returning. 120 * @param bounds The bounds of the bitmap in the destination coordinate system (where the 121 * view should be presented. Typically, this is matrix.mapRect(viewBounds); 122 * @return A bitmap of the given view or null if bounds has no width or height. 123 */ 124 public static Bitmap createViewBitmap(View view, Matrix matrix, RectF bounds) { 125 Bitmap bitmap = null; 126 int bitmapWidth = Math.round(bounds.width()); 127 int bitmapHeight = Math.round(bounds.height()); 128 if (bitmapWidth > 0 && bitmapHeight > 0) { 129 float scale = Math.min(1f, ((float)MAX_IMAGE_SIZE) / (bitmapWidth * bitmapHeight)); 130 bitmapWidth *= scale; 131 bitmapHeight *= scale; 132 matrix.postTranslate(-bounds.left, -bounds.top); 133 matrix.postScale(scale, scale); 134 bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888); 135 Canvas canvas = new Canvas(bitmap); 136 canvas.concat(matrix); 137 view.draw(canvas); 138 } 139 return bitmap; 140 } 141 142 public static class MatrixEvaluator implements TypeEvaluator<Matrix> { 143 144 float[] mTempStartValues = new float[9]; 145 146 float[] mTempEndValues = new float[9]; 147 148 Matrix mTempMatrix = new Matrix(); 149 150 @Override 151 public Matrix evaluate(float fraction, Matrix startValue, Matrix endValue) { 152 startValue.getValues(mTempStartValues); 153 endValue.getValues(mTempEndValues); 154 for (int i = 0; i < 9; i++) { 155 float diff = mTempEndValues[i] - mTempStartValues[i]; 156 mTempEndValues[i] = mTempStartValues[i] + (fraction * diff); 157 } 158 mTempMatrix.setValues(mTempEndValues); 159 return mTempMatrix; 160 } 161 } 162 } 163