FileDocCategorySizeDatePackage
SharedElementCallback.javaAPI DocAndroid 5.1 API11748Thu Mar 12 22:22:10 GMT 2015android.app

SharedElementCallback

public abstract class SharedElementCallback extends Object
Listener provided in {@link Activity#setEnterSharedElementCallback(SharedElementCallback)} and {@link Activity#setExitSharedElementCallback(SharedElementCallback)} as well as {@link Fragment#setEnterSharedElementCallback(SharedElementCallback)} and {@link Fragment#setExitSharedElementCallback(SharedElementCallback)} to monitor the Shared element transitions. The events can be used to customize Activity and Fragment Transition behavior.

Fields Summary
private android.graphics.Matrix
mTempMatrix
private static final String
BUNDLE_SNAPSHOT_BITMAP
private static final String
BUNDLE_SNAPSHOT_IMAGE_SCALETYPE
private static final String
BUNDLE_SNAPSHOT_IMAGE_MATRIX
static final SharedElementCallback
NULL_CALLBACK
Constructors Summary
Methods Summary
public android.os.ParcelableonCaptureSharedElementSnapshot(android.view.View sharedElement, android.graphics.Matrix viewToGlobalMatrix, android.graphics.RectF screenBounds)
Creates a snapshot of a shared element to be used by the remote Activity and reconstituted with {@link #onCreateSnapshotView(android.content.Context, android.os.Parcelable)}. A null return value will mean that the remote Activity will have a null snapshot View in {@link #onSharedElementStart(java.util.List, java.util.List, java.util.List)} and {@link #onSharedElementEnd(java.util.List, java.util.List, java.util.List)}.

This is not called for Fragment Transitions.

param
sharedElement The shared element View to create a snapshot for.
param
viewToGlobalMatrix A matrix containing a transform from the view to the screen coordinates.
param
screenBounds The bounds of shared element in screen coordinate space. This is the bounds of the view with the viewToGlobalMatrix applied.
return
A snapshot to send to the remote Activity to be reconstituted with {@link #onCreateSnapshotView(android.content.Context, android.os.Parcelable)} and passed into {@link #onSharedElementStart(java.util.List, java.util.List, java.util.List)} and {@link #onSharedElementEnd(java.util.List, java.util.List, java.util.List)}.

        if (sharedElement instanceof ImageView) {
            ImageView imageView = ((ImageView) sharedElement);
            Drawable d = imageView.getDrawable();
            Drawable bg = imageView.getBackground();
            if (d != null && (bg == null || bg.getAlpha() == 0)) {
                Bitmap bitmap = TransitionUtils.createDrawableBitmap(d);
                if (bitmap != null) {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(BUNDLE_SNAPSHOT_BITMAP, bitmap);
                    bundle.putString(BUNDLE_SNAPSHOT_IMAGE_SCALETYPE,
                            imageView.getScaleType().toString());
                    if (imageView.getScaleType() == ScaleType.MATRIX) {
                        Matrix matrix = imageView.getImageMatrix();
                        float[] values = new float[9];
                        matrix.getValues(values);
                        bundle.putFloatArray(BUNDLE_SNAPSHOT_IMAGE_MATRIX, values);
                    }
                    return bundle;
                }
            }
        }
        if (mTempMatrix == null) {
            mTempMatrix = new Matrix(viewToGlobalMatrix);
        } else {
            mTempMatrix.set(viewToGlobalMatrix);
        }
        return TransitionUtils.createViewBitmap(sharedElement, mTempMatrix, screenBounds);
    
public android.view.ViewonCreateSnapshotView(android.content.Context context, android.os.Parcelable snapshot)
Reconstitutes a snapshot View from a Parcelable returned in {@link #onCaptureSharedElementSnapshot(android.view.View, android.graphics.Matrix, android.graphics.RectF)} to be used in {@link #onSharedElementStart(java.util.List, java.util.List, java.util.List)} and {@link #onSharedElementEnd(java.util.List, java.util.List, java.util.List)}. The returned View will be sized and positioned after this call so that it is ready to be added to the decor View's overlay.

This is not called for Fragment Transitions.

param
context The Context used to create the snapshot View.
param
snapshot The Parcelable returned by {@link #onCaptureSharedElementSnapshot( android.view.View, android.graphics.Matrix, android.graphics.RectF)}.
return
A View to be sent in {@link #onSharedElementStart(java.util.List, java.util.List, java.util.List)} and {@link #onSharedElementEnd(java.util.List, java.util.List, java.util.List)}. A null value will produce a null snapshot value for those two methods.

        View view = null;
        if (snapshot instanceof Bundle) {
            Bundle bundle = (Bundle) snapshot;
            Bitmap bitmap = (Bitmap) bundle.getParcelable(BUNDLE_SNAPSHOT_BITMAP);
            if (bitmap == null) {
                return null;
            }
            ImageView imageView = new ImageView(context);
            view = imageView;
            imageView.setImageBitmap(bitmap);
            imageView.setScaleType(
                    ScaleType.valueOf(bundle.getString(BUNDLE_SNAPSHOT_IMAGE_SCALETYPE)));
            if (imageView.getScaleType() == ScaleType.MATRIX) {
                float[] values = bundle.getFloatArray(BUNDLE_SNAPSHOT_IMAGE_MATRIX);
                Matrix matrix = new Matrix();
                matrix.setValues(values);
                imageView.setImageMatrix(matrix);
            }
        } else if (snapshot instanceof Bitmap) {
            Bitmap bitmap = (Bitmap) snapshot;
            view = new View(context);
            Resources resources = context.getResources();
            view.setBackground(new BitmapDrawable(resources, bitmap));
        }
        return view;
    
public voidonMapSharedElements(java.util.List names, java.util.Map sharedElements)
Lets the SharedElementCallback adjust the mapping of shared element names to Views.

param
names The names of all shared elements transferred from the calling Activity or Fragment in the order they were provided.
param
sharedElements The mapping of shared element names to Views. The best guess will be filled into sharedElements based on the transitionNames.

public voidonRejectSharedElements(java.util.List rejectedSharedElements)
Called after {@link #onMapSharedElements(java.util.List, java.util.Map)} when transferring shared elements in. Any shared elements that have no mapping will be in rejectedSharedElements. The elements remaining in rejectedSharedElements will be transitioned out of the Scene. If a View is removed from rejectedSharedElements, it must be handled by the SharedElementCallback.

Views in rejectedSharedElements will have their position and size set to the position of the calling shared element, relative to the Window decor View and contain snapshots of the View from the calling Activity or Fragment. This view may be safely added to the decor View's overlay to remain in position.

This method is not called for Fragment Transitions. All rejected shared elements will be handled by the exit transition.

param
rejectedSharedElements Views containing visual information of shared elements that are not part of the entering scene. These Views are positioned relative to the Window decor View. A View removed from this list will not be transitioned automatically.

public voidonSharedElementEnd(java.util.List sharedElementNames, java.util.List sharedElements, java.util.List sharedElementSnapshots)
Called after the end state is set for the shared element, but before the end state is captured by the shared element transition.

Any customization done in {@link #onSharedElementStart(java.util.List, java.util.List, java.util.List)} may need to be modified to the final state of the shared element if it is not automatically corrected by layout. For example, rotation or scale will not be affected by layout and if changed in {@link #onSharedElementStart(java.util.List, java.util.List, java.util.List)}, it will also have to be set here again to correct the end state.

param
sharedElementNames The names of the shared elements that were accepted into the View hierarchy.
param
sharedElements The shared elements that are part of the View hierarchy.
param
sharedElementSnapshots The Views containing snap shots of the shared element from the launching Window. These elements will not be part of the scene, but will be positioned relative to the Window decor View. This list will be null for Fragment Transitions.

public voidonSharedElementStart(java.util.List sharedElementNames, java.util.List sharedElements, java.util.List sharedElementSnapshots)
Called immediately after the start state is set for the shared element. The shared element will start at the size and position of the shared element in the launching Activity or Fragment.

param
sharedElementNames The names of the shared elements that were accepted into the View hierarchy.
param
sharedElements The shared elements that are part of the View hierarchy.
param
sharedElementSnapshots The Views containing snap shots of the shared element from the launching Window. These elements will not be part of the scene, but will be positioned relative to the Window decor View. This list is null for Fragment Transitions.