1
0
mirror of https://github.com/wasabeef/glide-transformations.git synced 2025-06-07 23:04:06 +08:00

Optimize the code

1. Not to create the mask bitmap.
2. Adjust MaskTransformation’s getId()’s return.
This commit is contained in:
start141 2015-09-17 20:27:30 +08:00
parent 57fcfce9df
commit cb3cd88ed2
3 changed files with 48 additions and 44 deletions

View File

@ -191,7 +191,6 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
.bitmapTransform(
new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
.into(holder.image);
break;
case Brightness:
Glide.with(mContext)

View File

@ -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<Bitmap> {
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<Bitmap> {
@Override
public Resource<Bitmap> transform(Resource<Bitmap> 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) + ")";
}
}

View File

@ -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<Bitmap> {
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<Bitmap> {
@Override
public Resource<Bitmap> transform(Resource<Bitmap> 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) + ")";
}
}