162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount/*
262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * Copyright (C) 2014 The Android Open Source Project
362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount *
462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * Licensed under the Apache License, Version 2.0 (the "License");
562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * you may not use this file except in compliance with the License.
662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * You may obtain a copy of the License at
762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount *
862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount *      http://www.apache.org/licenses/LICENSE-2.0
962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount *
1062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * Unless required by applicable law or agreed to in writing, software
1162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * distributed under the License is distributed on an "AS IS" BASIS,
1262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * See the License for the specific language governing permissions and
1462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * limitations under the License.
1562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount */
1662ab9b78b77b7dd851c47115f4d8d7611d657585George Mountpackage android.app;
1762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
18480ca829e2d3313740b4d14d30afbea6c98ff965George Mountimport android.content.Context;
19480ca829e2d3313740b4d14d30afbea6c98ff965George Mountimport android.content.res.Resources;
20480ca829e2d3313740b4d14d30afbea6c98ff965George Mountimport android.graphics.Bitmap;
21480ca829e2d3313740b4d14d30afbea6c98ff965George Mountimport android.graphics.Matrix;
22480ca829e2d3313740b4d14d30afbea6c98ff965George Mountimport android.graphics.RectF;
23480ca829e2d3313740b4d14d30afbea6c98ff965George Mountimport android.graphics.drawable.BitmapDrawable;
24017b13a50c1225ca338357dc1315befa38dc3e33Dake Guimport android.graphics.drawable.Drawable;
25017b13a50c1225ca338357dc1315befa38dc3e33Dake Guimport android.os.Bundle;
26480ca829e2d3313740b4d14d30afbea6c98ff965George Mountimport android.os.Parcelable;
2779b2781aa763cbd770f690c16af4448139f37061George Mountimport android.transition.TransitionUtils;
2862ab9b78b77b7dd851c47115f4d8d7611d657585George Mountimport android.view.View;
29017b13a50c1225ca338357dc1315befa38dc3e33Dake Guimport android.widget.ImageView;
30017b13a50c1225ca338357dc1315befa38dc3e33Dake Guimport android.widget.ImageView.ScaleType;
3162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
3262ab9b78b77b7dd851c47115f4d8d7611d657585George Mountimport java.util.List;
3362ab9b78b77b7dd851c47115f4d8d7611d657585George Mountimport java.util.Map;
3462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
3562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount/**
3662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * Listener provided in
376558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount * {@link Activity#setEnterSharedElementCallback(SharedElementCallback)} and
386558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount * {@link Activity#setExitSharedElementCallback(SharedElementCallback)} as well as
3985ea498d86b35ea9baf4e5ef96927bea17209636George Mount * {@link Fragment#setEnterSharedElementCallback(SharedElementCallback)} and
4085ea498d86b35ea9baf4e5ef96927bea17209636George Mount * {@link Fragment#setExitSharedElementCallback(SharedElementCallback)}
416558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount * to monitor the Shared element transitions. The events can be used to customize Activity
426558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount * and Fragment Transition behavior.
4362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount */
446558056e8fccc32f9e1dc59e46d09f8d916b7538George Mountpublic abstract class SharedElementCallback {
45480ca829e2d3313740b4d14d30afbea6c98ff965George Mount    private Matrix mTempMatrix;
46017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu    private static final String BUNDLE_SNAPSHOT_BITMAP = "sharedElement:snapshot:bitmap";
47017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu    private static final String BUNDLE_SNAPSHOT_IMAGE_SCALETYPE = "sharedElement:snapshot:imageScaleType";
48017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu    private static final String BUNDLE_SNAPSHOT_IMAGE_MATRIX = "sharedElement:snapshot:imageMatrix";
49800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount
506558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount    static final SharedElementCallback NULL_CALLBACK = new SharedElementCallback() {
51800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount    };
52800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount
5362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
540c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * In Activity Transitions, onSharedElementStart is called immediately before
550c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * capturing the start of the shared element state on enter and reenter transitions and
560c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * immediately before capturing the end of the shared element state for exit and return
570c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * transitions.
580c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * <p>
590c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * In Fragment Transitions, onSharedElementStart is called immediately before capturing the
600c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * start state of all shared element transitions.
610c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * <p>
620c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * This call can be used to adjust the transition start state by modifying the shared
630c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * element Views. Note that no layout step will be executed between onSharedElementStart
640c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * and the transition state capture.
650c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * <p>
660c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * For Activity Transitions, any changes made in {@link #onSharedElementEnd(List, List, List)}
670c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * that are not updated during by layout should be corrected in onSharedElementStart for exit and
680c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * return transitions. For example, rotation or scale will not be affected by layout and
690c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * if changed in {@link #onSharedElementEnd(List, List, List)}, it will also have to be reset
700c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * in onSharedElementStart again to correct the end state.
7162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *
7262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param sharedElementNames The names of the shared elements that were accepted into
7362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                           the View hierarchy.
7462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param sharedElements The shared elements that are part of the View hierarchy.
7562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param sharedElementSnapshots The Views containing snap shots of the shared element
7662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                               from the launching Window. These elements will not
7762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                               be part of the scene, but will be positioned relative
786558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     *                               to the Window decor View. This list is null for Fragment
796558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     *                               Transitions.
8062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
816558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount    public void onSharedElementStart(List<String> sharedElementNames,
8262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            List<View> sharedElements, List<View> sharedElementSnapshots) {}
8362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
8462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
850c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * In Activity Transitions, onSharedElementEnd is called immediately before
860c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * capturing the end of the shared element state on enter and reenter transitions and
870c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * immediately before capturing the start of the shared element state for exit and return
880c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * transitions.
890c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * <p>
900c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * In Fragment Transitions, onSharedElementEnd is called immediately before capturing the
910c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * end state of all shared element transitions.
920c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * <p>
930c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * This call can be used to adjust the transition end state by modifying the shared
940c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * element Views. Note that no layout step will be executed between onSharedElementEnd
950c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * and the transition state capture.
96800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * <p>
970c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * Any changes made in {@link #onSharedElementStart(List, List, List)} that are not updated
980c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * during layout should be corrected in onSharedElementEnd. For example, rotation or scale
990c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * will not be affected by layout and if changed in
1000c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * {@link #onSharedElementStart(List, List, List)}, it will also have to be reset in
1010c9227871ae8b1bbbe0597e51553d84d52856278George Mount     * onSharedElementEnd again to correct the end state.
10262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *
10362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param sharedElementNames The names of the shared elements that were accepted into
10462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                           the View hierarchy.
10562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param sharedElements The shared elements that are part of the View hierarchy.
10662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param sharedElementSnapshots The Views containing snap shots of the shared element
10762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                               from the launching Window. These elements will not
10862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                               be part of the scene, but will be positioned relative
1096558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     *                               to the Window decor View. This list will be null for
1106558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     *                               Fragment Transitions.
11162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
1126558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount    public void onSharedElementEnd(List<String> sharedElementNames,
11362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            List<View> sharedElements, List<View> sharedElementSnapshots) {}
11462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
11562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
1166558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * Called after {@link #onMapSharedElements(java.util.List, java.util.Map)} when
117800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * transferring shared elements in. Any shared elements that have no mapping will be in
118800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * <var>rejectedSharedElements</var>. The elements remaining in
119800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * <var>rejectedSharedElements</var> will be transitioned out of the Scene. If a
120800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * View is removed from <var>rejectedSharedElements</var>, it must be handled by the
1216558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * <code>SharedElementCallback</code>.
122800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * <p>
123800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * Views in rejectedSharedElements will have their position and size set to the
124800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * position of the calling shared element, relative to the Window decor View and contain
125800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * snapshots of the View from the calling Activity or Fragment. This
126800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * view may be safely added to the decor View's overlay to remain in position.
127800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     * </p>
1286558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * <p>This method is not called for Fragment Transitions. All rejected shared elements
1296558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * will be handled by the exit transition.</p>
13062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *
13162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param rejectedSharedElements Views containing visual information of shared elements
13262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                               that are not part of the entering scene. These Views
13362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                               are positioned relative to the Window decor View. A
13462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                               View removed from this list will not be transitioned
13562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     *                               automatically.
13662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
1376558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount    public void onRejectSharedElements(List<View> rejectedSharedElements) {}
13862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
13962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
1406558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * Lets the SharedElementCallback adjust the mapping of shared element names to
14162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * Views.
142800d72b0e05049e4a8f90ea96ec165fc975264ceGeorge Mount     *
14362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param names The names of all shared elements transferred from the calling Activity
1446558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     *              or Fragment in the order they were provided.
14562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * @param sharedElements The mapping of shared element names to Views. The best guess
1460a2ae002e60f7ea9b6bea282086b5eb0ae3c6e51George Mount     *                       will be filled into sharedElements based on the transitionNames.
14762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
1486558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount    public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {}
149480ca829e2d3313740b4d14d30afbea6c98ff965George Mount
150480ca829e2d3313740b4d14d30afbea6c98ff965George Mount    /**
151480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * Creates a snapshot of a shared element to be used by the remote Activity and reconstituted
1526558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * with {@link #onCreateSnapshotView(android.content.Context, android.os.Parcelable)}. A
153480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * null return value will mean that the remote Activity will have a null snapshot View in
1546558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * {@link #onSharedElementStart(java.util.List, java.util.List, java.util.List)} and
1556558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * {@link #onSharedElementEnd(java.util.List, java.util.List, java.util.List)}.
1566558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     *
1576558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * <p>This is not called for Fragment Transitions.</p>
158480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     *
159480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * @param sharedElement The shared element View to create a snapshot for.
160480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * @param viewToGlobalMatrix A matrix containing a transform from the view to the screen
161480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     *                           coordinates.
162480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * @param screenBounds The bounds of shared element in screen coordinate space. This is
163480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     *                     the bounds of the view with the viewToGlobalMatrix applied.
164480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * @return A snapshot to send to the remote Activity to be reconstituted with
1656558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * {@link #onCreateSnapshotView(android.content.Context, android.os.Parcelable)} and passed
1666558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * into {@link #onSharedElementStart(java.util.List, java.util.List, java.util.List)} and
1676558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * {@link #onSharedElementEnd(java.util.List, java.util.List, java.util.List)}.
168480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     */
1696558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount    public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix,
170480ca829e2d3313740b4d14d30afbea6c98ff965George Mount            RectF screenBounds) {
171017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu        if (sharedElement instanceof ImageView) {
172017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            ImageView imageView = ((ImageView) sharedElement);
173017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            Drawable d = imageView.getDrawable();
174017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            Drawable bg = imageView.getBackground();
175017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            if (d != null && (bg == null || bg.getAlpha() == 0)) {
176017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                Bitmap bitmap = TransitionUtils.createDrawableBitmap(d);
177017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                if (bitmap != null) {
178017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                    Bundle bundle = new Bundle();
179017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                    bundle.putParcelable(BUNDLE_SNAPSHOT_BITMAP, bitmap);
180017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                    bundle.putString(BUNDLE_SNAPSHOT_IMAGE_SCALETYPE,
181017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                            imageView.getScaleType().toString());
182017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                    if (imageView.getScaleType() == ScaleType.MATRIX) {
183017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                        Matrix matrix = imageView.getImageMatrix();
184017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                        float[] values = new float[9];
185017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                        matrix.getValues(values);
186017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                        bundle.putFloatArray(BUNDLE_SNAPSHOT_IMAGE_MATRIX, values);
187017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                    }
188017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                    return bundle;
189017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                }
190017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            }
191017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu        }
19279b2781aa763cbd770f690c16af4448139f37061George Mount        if (mTempMatrix == null) {
19379b2781aa763cbd770f690c16af4448139f37061George Mount            mTempMatrix = new Matrix(viewToGlobalMatrix);
19479b2781aa763cbd770f690c16af4448139f37061George Mount        } else {
195480ca829e2d3313740b4d14d30afbea6c98ff965George Mount            mTempMatrix.set(viewToGlobalMatrix);
196480ca829e2d3313740b4d14d30afbea6c98ff965George Mount        }
19779b2781aa763cbd770f690c16af4448139f37061George Mount        return TransitionUtils.createViewBitmap(sharedElement, mTempMatrix, screenBounds);
198480ca829e2d3313740b4d14d30afbea6c98ff965George Mount    }
199480ca829e2d3313740b4d14d30afbea6c98ff965George Mount
200480ca829e2d3313740b4d14d30afbea6c98ff965George Mount    /**
201480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * Reconstitutes a snapshot View from a Parcelable returned in
2026558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * {@link #onCaptureSharedElementSnapshot(android.view.View, android.graphics.Matrix,
2036558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * android.graphics.RectF)} to be used in {@link #onSharedElementStart(java.util.List,
2046558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * java.util.List, java.util.List)} and {@link #onSharedElementEnd(java.util.List,
205480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * java.util.List, java.util.List)}. The returned View will be sized and positioned after
206480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * this call so that it is ready to be added to the decor View's overlay.
207480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     *
2086558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * <p>This is not called for Fragment Transitions.</p>
2096558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     *
210480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * @param context The Context used to create the snapshot View.
2116558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * @param snapshot The Parcelable returned by {@link #onCaptureSharedElementSnapshot(
212480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * android.view.View, android.graphics.Matrix, android.graphics.RectF)}.
2136558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * @return A View to be sent in {@link #onSharedElementStart(java.util.List, java.util.List,
2146558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount     * java.util.List)} and {@link #onSharedElementEnd(java.util.List, java.util.List,
215480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     * java.util.List)}. A null value will produce a null snapshot value for those two methods.
216480ca829e2d3313740b4d14d30afbea6c98ff965George Mount     */
2176558056e8fccc32f9e1dc59e46d09f8d916b7538George Mount    public View onCreateSnapshotView(Context context, Parcelable snapshot) {
218480ca829e2d3313740b4d14d30afbea6c98ff965George Mount        View view = null;
219017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu        if (snapshot instanceof Bundle) {
220017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            Bundle bundle = (Bundle) snapshot;
221017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            Bitmap bitmap = (Bitmap) bundle.getParcelable(BUNDLE_SNAPSHOT_BITMAP);
222017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            if (bitmap == null) {
223017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                return null;
224017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            }
225017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            ImageView imageView = new ImageView(context);
226017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            view = imageView;
227017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            imageView.setImageBitmap(bitmap);
228017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            imageView.setScaleType(
229017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                    ScaleType.valueOf(bundle.getString(BUNDLE_SNAPSHOT_IMAGE_SCALETYPE)));
230017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            if (imageView.getScaleType() == ScaleType.MATRIX) {
231017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                float[] values = bundle.getFloatArray(BUNDLE_SNAPSHOT_IMAGE_MATRIX);
232017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                Matrix matrix = new Matrix();
233017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                matrix.setValues(values);
234017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu                imageView.setImageMatrix(matrix);
235017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu            }
236017b13a50c1225ca338357dc1315befa38dc3e33Dake Gu        } else if (snapshot instanceof Bitmap) {
237480ca829e2d3313740b4d14d30afbea6c98ff965George Mount            Bitmap bitmap = (Bitmap) snapshot;
238480ca829e2d3313740b4d14d30afbea6c98ff965George Mount            view = new View(context);
239480ca829e2d3313740b4d14d30afbea6c98ff965George Mount            Resources resources = context.getResources();
240480ca829e2d3313740b4d14d30afbea6c98ff965George Mount            view.setBackground(new BitmapDrawable(resources, bitmap));
241480ca829e2d3313740b4d14d30afbea6c98ff965George Mount        }
242480ca829e2d3313740b4d14d30afbea6c98ff965George Mount        return view;
243480ca829e2d3313740b4d14d30afbea6c98ff965George Mount    }
2444dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount
2454dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount    /**
2464dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * Called during an Activity Transition when the shared elements have arrived at the
2474dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * final location and are ready to be transferred. This method is called for both the
2484dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * source and destination Activities.
2494dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * <p>
2504dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * When the shared elements are ready to be transferred,
2514dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * {@link OnSharedElementsReadyListener#onSharedElementsReady()}
2524dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * must be called to trigger the transfer.
2534dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * <p>
2544dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * The default behavior is to trigger the transfer immediately.
2554dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     *
2564dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * @param sharedElementNames The names of the shared elements that are being transferred..
2574dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * @param sharedElements The shared elements that are part of the View hierarchy.
2584dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * @param listener The listener to call when the shared elements are ready to be hidden
2594dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     *                 in the source Activity or shown in the destination Activity.
2604dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     */
2614dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount    public void onSharedElementsArrived(List<String> sharedElementNames,
2624dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount            List<View> sharedElements, OnSharedElementsReadyListener listener) {
2634dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount        listener.onSharedElementsReady();
2644dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount    }
2654dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount
2664dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount    /**
2674dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * Listener to be called after {@link
2684dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * SharedElementCallback#onSharedElementsArrived(List, List, OnSharedElementsReadyListener)}
2694dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * when the shared elements are ready to be hidden in the source Activity and shown in the
2704dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     * destination Activity.
2714dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount     */
2724dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount    public interface OnSharedElementsReadyListener {
2734dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount
2744dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount        /**
2754dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount         * Call this method during or after the OnSharedElementsReadyListener has been received
2764dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount         * in {@link SharedElementCallback#onSharedElementsArrived(List, List,
2774dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount         * OnSharedElementsReadyListener)} to indicate that the shared elements are ready to be
2784dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount         * hidden in the source and shown in the destination Activity.
2794dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount         */
2804dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount        void onSharedElementsReady();
2814dc668cd36fbb46b0c2cc196ae4b7105009ec089George Mount    }
28262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount}
283