Improve ImageView drawable re-use

Bug 22403868

Initial attempt only helped if setImageBitmap() was the only
thing called but during new-loading content it's common for a
placeholder to be set via setImageDrawable.

Tweak ImageView slightly to just have a BitmapDrawable that it
lazy-creates but will hold on to for any subsequent calls
to setImageBitmap

Change-Id: I7380521c7b363d458e4cda041f1f8b2b1fb3a93a
This commit is contained in:
John Reck
2015-07-10 10:02:58 -07:00
parent 9613e9b764
commit 5a1356916d

View File

@@ -91,6 +91,7 @@ public class ImageView extends View {
private boolean mColorMod = false;
private Drawable mDrawable = null;
private ImageViewBitmapDrawable mRecycleableBitmapDrawable = null;
private ColorStateList mDrawableTintList = null;
private PorterDuff.Mode mDrawableTintMode = null;
private boolean mHasDrawableTint = false;
@@ -588,18 +589,16 @@ public class ImageView extends View {
*/
@android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// if this is used frequently, may handle bitmaps explicitly
// to reduce the intermediate drawable object
if (mDrawable instanceof ImageViewBitmapDrawable) {
ImageViewBitmapDrawable recycledDrawable = (ImageViewBitmapDrawable) mDrawable;
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
recycledDrawable.setBitmap(bm);
setImageDrawable(recycledDrawable);
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
setImageDrawable(new ImageViewBitmapDrawable(mContext.getResources(), bm));
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
public void setImageState(int[] state, boolean merge) {
@@ -868,6 +867,10 @@ public class ImageView extends View {
}
private void updateDrawable(Drawable d) {
if (d != mRecycleableBitmapDrawable && mRecycleableBitmapDrawable != null) {
mRecycleableBitmapDrawable.setBitmap(null);
}
if (mDrawable != null) {
mDrawable.setCallback(null);
unscheduleDrawable(mDrawable);