SharedElementCallbackpublic abstract class SharedElementCallback extends Object Listener provided in
{@link FragmentActivity#setEnterSharedElementCallback(SharedElementCallback)} and
{@link FragmentActivity#setExitSharedElementCallback(SharedElementCallback)}
to monitor the Activity transitions. The events can be used to customize Activity
Transition behavior. |
Fields Summary |
---|
private android.graphics.Matrix | mTempMatrix | private static int | MAX_IMAGE_SIZE | private static final String | BUNDLE_SNAPSHOT_BITMAP | private static final String | BUNDLE_SNAPSHOT_IMAGE_SCALETYPE | private static final String | BUNDLE_SNAPSHOT_IMAGE_MATRIX |
Methods Summary |
---|
private static android.graphics.Bitmap | createDrawableBitmap(android.graphics.drawable.Drawable drawable)Get a copy of bitmap of given drawable.
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
if (width <= 0 || height <= 0) {
return null;
}
float scale = Math.min(1f, ((float)MAX_IMAGE_SIZE) / (width * height));
if (drawable instanceof BitmapDrawable && scale == 1f) {
// return same bitmap if scale down not needed
return ((BitmapDrawable) drawable).getBitmap();
}
int bitmapWidth = (int) (width * scale);
int bitmapHeight = (int) (height * scale);
Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Rect existingBounds = drawable.getBounds();
int left = existingBounds.left;
int top = existingBounds.top;
int right = existingBounds.right;
int bottom = existingBounds.bottom;
drawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
drawable.draw(canvas);
drawable.setBounds(left, top, right, bottom);
return bitmap;
| public android.os.Parcelable | onCaptureSharedElementSnapshot(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.
if (sharedElement instanceof ImageView) {
ImageView imageView = ((ImageView) sharedElement);
Drawable d = imageView.getDrawable();
Drawable bg = imageView.getBackground();
if (d != null && bg == null) {
Bitmap bitmap = 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;
}
}
}
int bitmapWidth = Math.round(screenBounds.width());
int bitmapHeight = Math.round(screenBounds.height());
Bitmap bitmap = null;
if (bitmapWidth > 0 && bitmapHeight > 0) {
float scale = Math.min(1f, ((float)MAX_IMAGE_SIZE) / (bitmapWidth * bitmapHeight));
bitmapWidth *= scale;
bitmapHeight *= scale;
if (mTempMatrix == null) {
mTempMatrix = new Matrix();
}
mTempMatrix.set(viewToGlobalMatrix);
mTempMatrix.postTranslate(-screenBounds.left, -screenBounds.top);
mTempMatrix.postScale(scale, scale);
bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.concat(mTempMatrix);
sharedElement.draw(canvas);
}
return bitmap;
| public android.view.View | onCreateSnapshotView(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.
ImageView 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 ImageView(context);
view.setImageBitmap(bitmap);
}
return view;
| public void | onMapSharedElements(java.util.List names, java.util.Map sharedElements)Lets the SharedElementCallback adjust the mapping of shared element names to
Views.
| public void | onRejectSharedElements(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
SharedElementListener .
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.
| public void | onSharedElementEnd(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.
| public void | onSharedElementStart(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.
|
|