Source code
package android.support.v4.app;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import java.util.List;
import java.util.Map;
public abstract class SharedElementCallback {
private static final String BUNDLE_SNAPSHOT_BITMAP = "sharedElement:snapshot:bitmap";
private static final String BUNDLE_SNAPSHOT_IMAGE_MATRIX = "sharedElement:snapshot:imageMatrix";
private static final String BUNDLE_SNAPSHOT_IMAGE_SCALETYPE = "sharedElement:snapshot:imageScaleType";
private static int MAX_IMAGE_SIZE = 1048576;
private Matrix mTempMatrix;
public void onSharedElementStart(List<String> list, List<View> list2, List<View> list3) {
}
public void onSharedElementEnd(List<String> list, List<View> list2, List<View> list3) {
}
public void onRejectSharedElements(List<View> list) {
}
public void onMapSharedElements(List<String> list, Map<String, View> map) {
}
public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds) {
Bitmap bitmap;
if (sharedElement instanceof ImageView) {
ImageView imageView = (ImageView) sharedElement;
Drawable d = imageView.getDrawable();
Drawable bg = imageView.getBackground();
if (d != null && bg == null) {
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) {
return bundle;
}
float[] values = new float[9];
imageView.getImageMatrix().getValues(values);
bundle.putFloatArray(BUNDLE_SNAPSHOT_IMAGE_MATRIX, values);
return bundle;
}
}
}
int bitmapWidth = Math.round(screenBounds.width());
int bitmapHeight = Math.round(screenBounds.height());
bitmap = null;
if (bitmapWidth > 0 && bitmapHeight > 0) {
float scale = Math.min(1.0f, ((float) MAX_IMAGE_SIZE) / ((float) (bitmapWidth * bitmapHeight)));
bitmapWidth = (int) (((float) bitmapWidth) * scale);
bitmapHeight = (int) (((float) bitmapHeight) * scale);
if (this.mTempMatrix == null) {
this.mTempMatrix = new Matrix();
}
this.mTempMatrix.set(viewToGlobalMatrix);
this.mTempMatrix.postTranslate(-screenBounds.left, -screenBounds.top);
this.mTempMatrix.postScale(scale, scale);
bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.concat(this.mTempMatrix);
sharedElement.draw(canvas);
}
return bitmap;
}
private static Bitmap createDrawableBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
if (width <= 0 || height <= 0) {
return null;
}
float scale = Math.min(1.0f, ((float) MAX_IMAGE_SIZE) / ((float) (width * height)));
if ((drawable instanceof BitmapDrawable) && scale == 1.0f) {
return ((BitmapDrawable) drawable).getBitmap();
}
int bitmapWidth = (int) (((float) width) * scale);
int bitmapHeight = (int) (((float) height) * scale);
Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, 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 View onCreateSnapshotView(Context context, Parcelable snapshot) {
View view = null;
Bitmap bitmap;
if (snapshot instanceof Bundle) {
Bundle bundle = (Bundle) snapshot;
bitmap = (Bitmap) bundle.getParcelable(BUNDLE_SNAPSHOT_BITMAP);
if (bitmap == null) {
return null;
}
View 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) snapshot;
view = new ImageView(context);
view.setImageBitmap(bitmap);
}
return view;
}
}