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