From cb3cd88ed2790cb585c6eb871a710c1c87899636 Mon Sep 17 00:00:00 2001 From: start141 <415043846@qq.com> Date: Thu, 17 Sep 2015 20:27:30 +0800 Subject: [PATCH] Optimize the code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Not to create the mask bitmap. 2. Adjust MaskTransformation’s getId()’s return. --- .../wasabeef/example/glide/MainAdapter.java | 1 - .../transformations/MaskTransformation.java | 42 +++++++++------- .../NinePatchMaskTransformation.java | 49 ++++++++++--------- 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java index 59ea463..5826253 100644 --- a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java +++ b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java @@ -191,7 +191,6 @@ public class MainAdapter extends RecyclerView.Adapter { .bitmapTransform( new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f))) .into(holder.image); - break; case Brightness: Glide.with(mContext) diff --git a/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java b/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java index d2bfbab..2eb859e 100644 --- a/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java @@ -22,21 +22,32 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; -import android.graphics.Rect; import android.graphics.drawable.Drawable; + import com.bumptech.glide.Glide; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapResource; + import jp.wasabeef.glide.transformations.internal.Util; public class MaskTransformation implements Transformation { + private static Paint mMaskingPaint = new Paint(); private Context mContext; private BitmapPool mBitmapPool; private int mMaskId; + static { + mMaskingPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); + } + + /** + * @param maskId If you change the mask file, please rename the mask file, or Glide will get the + * cache with the old mask. Because getId() will return the same values if using the + * same make file name. If you have a good idea please tell we, thanks. + */ public MaskTransformation(Context context, int maskId) { mBitmapPool = Glide.get(context).getBitmapPool(); mContext = context; @@ -46,33 +57,26 @@ public class MaskTransformation implements Transformation { @Override public Resource transform(Resource resource, int outWidth, int outHeight) { Bitmap source = resource.get(); - int width = source.getWidth(); int height = source.getHeight(); - Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - - Drawable drawable = Util.getMaskDrawable(mContext, mMaskId); - drawable.setBounds(0, 0, width, height); - Canvas canvas = new Canvas(mask); - drawable.draw(canvas); - - Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); - if (bitmap == null) { - bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); + if (result == null) { + result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } - Paint paint = new Paint(); - paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); + Drawable mask = Util.getMaskDrawable(mContext, mMaskId); - canvas = new Canvas(bitmap); - canvas.drawBitmap(mask, new Rect(0, 0, width, height), new Rect(0, 0, width, height), null); - canvas.drawBitmap(source, 0, 0, paint); + Canvas canvas = new Canvas(result); + mask.setBounds(0, 0, width, height); + mask.draw(canvas); + canvas.drawBitmap(source, 0, 0, mMaskingPaint); - return BitmapResource.obtain(bitmap, mBitmapPool); + return BitmapResource.obtain(result, mBitmapPool); } @Override public String getId() { - return "MaskTransformation(maskId=" + mMaskId + ")"; + return "MaskTransformation(maskId=" + + mContext.getResources().getResourceEntryName(mMaskId) + ")"; } } diff --git a/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java b/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java index 6184034..280fbeb 100644 --- a/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java @@ -22,22 +22,33 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; -import android.graphics.Rect; import android.graphics.drawable.Drawable; + import com.bumptech.glide.Glide; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapResource; import com.bumptech.glide.request.target.Target; + import jp.wasabeef.glide.transformations.internal.Util; public class NinePatchMaskTransformation implements Transformation { + private static Paint mMaskingPaint = new Paint(); private Context mContext; private BitmapPool mBitmapPool; private int mMaskId; + static { + mMaskingPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); + } + + /** + * @param maskId If you change the mask file, please rename the mask file, or Glide will get the + * cache with the old mask. Because getId() will return the same values if using the + * same make file name. If you have a good idea please tell we, thanks. + */ public NinePatchMaskTransformation(Context context, int maskId) { mBitmapPool = Glide.get(context).getBitmapPool(); mContext = context; @@ -47,36 +58,26 @@ public class NinePatchMaskTransformation implements Transformation { @Override public Resource transform(Resource resource, int outWidth, int outHeight) { Bitmap source = resource.get(); + int width = outWidth == Target.SIZE_ORIGINAL ? source.getWidth() : outWidth; + int height = outHeight == Target.SIZE_ORIGINAL ? source.getHeight() : outHeight; - int width = source.getWidth(); - int height = source.getHeight(); - int maskWidth = outWidth == Target.SIZE_ORIGINAL ? width : outWidth; - int maskHeight = outHeight == Target.SIZE_ORIGINAL ? height : outHeight; - - Bitmap mask = Bitmap.createBitmap(maskWidth, maskHeight, Bitmap.Config.ARGB_8888); - - Drawable drawable = Util.getMaskDrawable(mContext, mMaskId); - drawable.setBounds(0, 0, maskWidth, maskHeight); - Canvas canvas = new Canvas(mask); - drawable.draw(canvas); - - Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); - if (bitmap == null) { - bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); + if (result == null) { + result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } - Paint paint = new Paint(); - paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); + Drawable mask = Util.getMaskDrawable(mContext, mMaskId); - canvas = new Canvas(bitmap); - canvas.drawBitmap(mask, new Rect(0, 0, maskWidth, maskHeight), new Rect(0, 0, width, height), - null); - canvas.drawBitmap(source, 0, 0, paint); + Canvas canvas = new Canvas(result); + mask.setBounds(0, 0, width, height); + mask.draw(canvas); + canvas.drawBitmap(source, 0, 0, mMaskingPaint); - return BitmapResource.obtain(bitmap, mBitmapPool); + return BitmapResource.obtain(result, mBitmapPool); } @Override public String getId() { - return "NinePatchMaskTransformation(mMaskId=" + mMaskId + ")"; + return "NinePatchMaskTransformation(mMaskId=" + + mContext.getResources().getResourceEntryName(mMaskId) + ")"; } }