Loading core/java/android/app/SharedElementCallback.java +46 −2 Original line number Original line Diff line number Diff line Loading @@ -18,13 +18,16 @@ package android.app; import android.content.Context; import android.content.Context; import android.content.res.Resources; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Matrix; import android.graphics.RectF; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.os.Parcelable; import android.transition.TransitionUtils; import android.transition.TransitionUtils; import android.view.View; import android.view.View; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import java.util.List; import java.util.List; import java.util.Map; import java.util.Map; Loading @@ -40,6 +43,9 @@ import java.util.Map; */ */ public abstract class SharedElementCallback { public abstract class SharedElementCallback { private Matrix mTempMatrix; private Matrix mTempMatrix; private static final String BUNDLE_SNAPSHOT_BITMAP = "sharedElement:snapshot:bitmap"; private static final String BUNDLE_SNAPSHOT_IMAGE_SCALETYPE = "sharedElement:snapshot:imageScaleType"; private static final String BUNDLE_SNAPSHOT_IMAGE_MATRIX = "sharedElement:snapshot:imageMatrix"; static final SharedElementCallback NULL_CALLBACK = new SharedElementCallback() { static final SharedElementCallback NULL_CALLBACK = new SharedElementCallback() { }; }; Loading Loading @@ -142,6 +148,27 @@ public abstract class SharedElementCallback { */ */ public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds) { RectF screenBounds) { 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) { if (mTempMatrix == null) { mTempMatrix = new Matrix(viewToGlobalMatrix); mTempMatrix = new Matrix(viewToGlobalMatrix); } else { } else { Loading Loading @@ -169,7 +196,24 @@ public abstract class SharedElementCallback { */ */ public View onCreateSnapshotView(Context context, Parcelable snapshot) { public View onCreateSnapshotView(Context context, Parcelable snapshot) { View view = null; View view = null; if (snapshot instanceof Bitmap) { 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; Bitmap bitmap = (Bitmap) snapshot; view = new View(context); view = new View(context); Resources resources = context.getResources(); Resources resources = context.getResources(); Loading core/java/android/transition/TransitionUtils.java +31 −0 Original line number Original line Diff line number Diff line Loading @@ -22,8 +22,10 @@ import android.animation.TypeEvaluator; import android.graphics.Bitmap; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Matrix; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.view.View; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ImageView; Loading Loading @@ -108,6 +110,35 @@ public class TransitionUtils { return copy; return copy; } } /** * Get a copy of bitmap of given drawable, return null if intrinsic size is zero */ public static Bitmap createDrawableBitmap(Drawable 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; } /** /** * Creates a Bitmap of the given view, using the Matrix matrix to transform to the local * Creates a Bitmap of the given view, using the Matrix matrix to transform to the local * coordinates. <code>matrix</code> will be modified during the bitmap creation. * coordinates. <code>matrix</code> will be modified during the bitmap creation. Loading Loading
core/java/android/app/SharedElementCallback.java +46 −2 Original line number Original line Diff line number Diff line Loading @@ -18,13 +18,16 @@ package android.app; import android.content.Context; import android.content.Context; import android.content.res.Resources; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Matrix; import android.graphics.RectF; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.os.Parcelable; import android.transition.TransitionUtils; import android.transition.TransitionUtils; import android.view.View; import android.view.View; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import java.util.List; import java.util.List; import java.util.Map; import java.util.Map; Loading @@ -40,6 +43,9 @@ import java.util.Map; */ */ public abstract class SharedElementCallback { public abstract class SharedElementCallback { private Matrix mTempMatrix; private Matrix mTempMatrix; private static final String BUNDLE_SNAPSHOT_BITMAP = "sharedElement:snapshot:bitmap"; private static final String BUNDLE_SNAPSHOT_IMAGE_SCALETYPE = "sharedElement:snapshot:imageScaleType"; private static final String BUNDLE_SNAPSHOT_IMAGE_MATRIX = "sharedElement:snapshot:imageMatrix"; static final SharedElementCallback NULL_CALLBACK = new SharedElementCallback() { static final SharedElementCallback NULL_CALLBACK = new SharedElementCallback() { }; }; Loading Loading @@ -142,6 +148,27 @@ public abstract class SharedElementCallback { */ */ public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds) { RectF screenBounds) { 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) { if (mTempMatrix == null) { mTempMatrix = new Matrix(viewToGlobalMatrix); mTempMatrix = new Matrix(viewToGlobalMatrix); } else { } else { Loading Loading @@ -169,7 +196,24 @@ public abstract class SharedElementCallback { */ */ public View onCreateSnapshotView(Context context, Parcelable snapshot) { public View onCreateSnapshotView(Context context, Parcelable snapshot) { View view = null; View view = null; if (snapshot instanceof Bitmap) { 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; Bitmap bitmap = (Bitmap) snapshot; view = new View(context); view = new View(context); Resources resources = context.getResources(); Resources resources = context.getResources(); Loading
core/java/android/transition/TransitionUtils.java +31 −0 Original line number Original line Diff line number Diff line Loading @@ -22,8 +22,10 @@ import android.animation.TypeEvaluator; import android.graphics.Bitmap; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Matrix; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.view.View; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ImageView; Loading Loading @@ -108,6 +110,35 @@ public class TransitionUtils { return copy; return copy; } } /** * Get a copy of bitmap of given drawable, return null if intrinsic size is zero */ public static Bitmap createDrawableBitmap(Drawable 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; } /** /** * Creates a Bitmap of the given view, using the Matrix matrix to transform to the local * Creates a Bitmap of the given view, using the Matrix matrix to transform to the local * coordinates. <code>matrix</code> will be modified during the bitmap creation. * coordinates. <code>matrix</code> will be modified during the bitmap creation. Loading