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