mirror of
https://github.com/wasabeef/glide-transformations.git
synced 2025-06-08 07:24:05 +08:00
Optimize the code
1. Not to create the mask bitmap. 2. Adjust MaskTransformation’s getId()’s return.
This commit is contained in:
parent
57fcfce9df
commit
cb3cd88ed2
@ -191,7 +191,6 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
|
|||||||
.bitmapTransform(
|
.bitmapTransform(
|
||||||
new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
|
new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
|
||||||
.into(holder.image);
|
.into(holder.image);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case Brightness:
|
case Brightness:
|
||||||
Glide.with(mContext)
|
Glide.with(mContext)
|
||||||
|
@ -22,21 +22,32 @@ import android.graphics.Canvas;
|
|||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.graphics.PorterDuff;
|
import android.graphics.PorterDuff;
|
||||||
import android.graphics.PorterDuffXfermode;
|
import android.graphics.PorterDuffXfermode;
|
||||||
import android.graphics.Rect;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.load.Transformation;
|
import com.bumptech.glide.load.Transformation;
|
||||||
import com.bumptech.glide.load.engine.Resource;
|
import com.bumptech.glide.load.engine.Resource;
|
||||||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
|
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
|
||||||
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
|
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
|
||||||
|
|
||||||
import jp.wasabeef.glide.transformations.internal.Util;
|
import jp.wasabeef.glide.transformations.internal.Util;
|
||||||
|
|
||||||
public class MaskTransformation implements Transformation<Bitmap> {
|
public class MaskTransformation implements Transformation<Bitmap> {
|
||||||
|
|
||||||
|
private static Paint mMaskingPaint = new Paint();
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private BitmapPool mBitmapPool;
|
private BitmapPool mBitmapPool;
|
||||||
private int mMaskId;
|
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) {
|
public MaskTransformation(Context context, int maskId) {
|
||||||
mBitmapPool = Glide.get(context).getBitmapPool();
|
mBitmapPool = Glide.get(context).getBitmapPool();
|
||||||
mContext = context;
|
mContext = context;
|
||||||
@ -46,33 +57,26 @@ public class MaskTransformation implements Transformation<Bitmap> {
|
|||||||
@Override
|
@Override
|
||||||
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
||||||
Bitmap source = resource.get();
|
Bitmap source = resource.get();
|
||||||
|
|
||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
int height = source.getHeight();
|
||||||
|
|
||||||
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
|
||||||
|
if (result == null) {
|
||||||
Drawable drawable = Util.getMaskDrawable(mContext, mMaskId);
|
result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Paint paint = new Paint();
|
Drawable mask = Util.getMaskDrawable(mContext, mMaskId);
|
||||||
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
|
||||||
|
|
||||||
canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(result);
|
||||||
canvas.drawBitmap(mask, new Rect(0, 0, width, height), new Rect(0, 0, width, height), null);
|
mask.setBounds(0, 0, width, height);
|
||||||
canvas.drawBitmap(source, 0, 0, paint);
|
mask.draw(canvas);
|
||||||
|
canvas.drawBitmap(source, 0, 0, mMaskingPaint);
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(result, mBitmapPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String getId() {
|
@Override public String getId() {
|
||||||
return "MaskTransformation(maskId=" + mMaskId + ")";
|
return "MaskTransformation(maskId="
|
||||||
|
+ mContext.getResources().getResourceEntryName(mMaskId) + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,22 +22,33 @@ import android.graphics.Canvas;
|
|||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.graphics.PorterDuff;
|
import android.graphics.PorterDuff;
|
||||||
import android.graphics.PorterDuffXfermode;
|
import android.graphics.PorterDuffXfermode;
|
||||||
import android.graphics.Rect;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.load.Transformation;
|
import com.bumptech.glide.load.Transformation;
|
||||||
import com.bumptech.glide.load.engine.Resource;
|
import com.bumptech.glide.load.engine.Resource;
|
||||||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
|
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
|
||||||
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
|
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
|
||||||
import com.bumptech.glide.request.target.Target;
|
import com.bumptech.glide.request.target.Target;
|
||||||
|
|
||||||
import jp.wasabeef.glide.transformations.internal.Util;
|
import jp.wasabeef.glide.transformations.internal.Util;
|
||||||
|
|
||||||
public class NinePatchMaskTransformation implements Transformation<Bitmap> {
|
public class NinePatchMaskTransformation implements Transformation<Bitmap> {
|
||||||
|
|
||||||
|
private static Paint mMaskingPaint = new Paint();
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private BitmapPool mBitmapPool;
|
private BitmapPool mBitmapPool;
|
||||||
private int mMaskId;
|
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) {
|
public NinePatchMaskTransformation(Context context, int maskId) {
|
||||||
mBitmapPool = Glide.get(context).getBitmapPool();
|
mBitmapPool = Glide.get(context).getBitmapPool();
|
||||||
mContext = context;
|
mContext = context;
|
||||||
@ -47,36 +58,26 @@ public class NinePatchMaskTransformation implements Transformation<Bitmap> {
|
|||||||
@Override
|
@Override
|
||||||
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
||||||
Bitmap source = resource.get();
|
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();
|
Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
|
||||||
int height = source.getHeight();
|
if (result == null) {
|
||||||
int maskWidth = outWidth == Target.SIZE_ORIGINAL ? width : outWidth;
|
result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Paint paint = new Paint();
|
Drawable mask = Util.getMaskDrawable(mContext, mMaskId);
|
||||||
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
|
||||||
|
|
||||||
canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(result);
|
||||||
canvas.drawBitmap(mask, new Rect(0, 0, maskWidth, maskHeight), new Rect(0, 0, width, height),
|
mask.setBounds(0, 0, width, height);
|
||||||
null);
|
mask.draw(canvas);
|
||||||
canvas.drawBitmap(source, 0, 0, paint);
|
canvas.drawBitmap(source, 0, 0, mMaskingPaint);
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(result, mBitmapPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String getId() {
|
@Override public String getId() {
|
||||||
return "NinePatchMaskTransformation(mMaskId=" + mMaskId + ")";
|
return "NinePatchMaskTransformation(mMaskId="
|
||||||
|
+ mContext.getResources().getResourceEntryName(mMaskId) + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user