1
0
mirror of https://github.com/wasabeef/glide-transformations.git synced 2025-07-01 05:42:45 +08:00
This commit is contained in:
wasabeef 2015-09-16 22:38:16 +09:00
parent 831f038d29
commit 5cf3e3d934
4 changed files with 129 additions and 124 deletions

View File

@ -9,13 +9,10 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
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.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import java.util.List; import java.util.List;
import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.ColorFilterTransformation; import jp.wasabeef.glide.transformations.ColorFilterTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation; import jp.wasabeef.glide.transformations.CropCircleTransformation;
@ -90,7 +87,7 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
.into(holder.image); .into(holder.image);
break; break;
case NinePatchMask: case NinePatchMask:
transformation = new NinePatchMaskTransformation(mContext, R.drawable.chat_me_mask, 300, 300); transformation = new NinePatchMaskTransformation(mContext, R.drawable.chat_me_mask);
Glide.with(mContext) Glide.with(mContext)
.load(R.drawable.demo) .load(R.drawable.demo)
.override(300, 300) .override(300, 300)

View File

@ -24,13 +24,12 @@ import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode; import android.graphics.PorterDuffXfermode;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build;
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;
public class MaskTransformation implements Transformation<Bitmap> { public class MaskTransformation implements Transformation<Bitmap> {
@ -48,46 +47,32 @@ public class MaskTransformation implements Transformation<Bitmap> {
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();
boolean maskFromBitmapPool = true; int width = source.getWidth();
Bitmap mask = mBitmapPool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); int height = source.getHeight();
if (mask == null) {
maskFromBitmapPool = false;
mask = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Drawable drawable; Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
if (Build.VERSION.SDK_INT >= 21) {
drawable = mContext.getDrawable(mMaskId); Drawable drawable = Util.getMaskDrawable(mContext, mMaskId);
} else { drawable.setBounds(0, 0, width, height);
drawable = mContext.getResources().getDrawable(mMaskId);
}
drawable.setBounds(0, 0, source.getWidth(), source.getHeight());
Canvas canvas = new Canvas(mask); Canvas canvas = new Canvas(mask);
drawable.draw(canvas); drawable.draw(canvas);
Bitmap bitmap = mBitmapPool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
if (bitmap == null) { if (bitmap == null) {
bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
} }
Paint paint = new Paint(); Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas = new Canvas(bitmap); canvas = new Canvas(bitmap);
canvas.drawBitmap(mask, new Rect(0, 0, mask.getWidth(), mask.getHeight()), new Rect(0, 0, canvas.drawBitmap(mask, new Rect(0, 0, width, height), new Rect(0, 0, width, height), null);
source.getWidth(), source.getHeight()), null);
canvas.drawBitmap(source, 0, 0, paint); canvas.drawBitmap(source, 0, 0, paint);
if (maskFromBitmapPool) {
mBitmapPool.put(mask);
}
return BitmapResource.obtain(bitmap, mBitmapPool); return BitmapResource.obtain(bitmap, mBitmapPool);
} }
@Override @Override public String getId() {
public String getId() {
return "MaskTransformation(maskId=" + mMaskId + ")"; return "MaskTransformation(maskId=" + mMaskId + ")";
} }
} }

View File

@ -24,79 +24,59 @@ import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode; import android.graphics.PorterDuffXfermode;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build;
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;
public class NinePatchMaskTransformation implements Transformation<Bitmap> { public class NinePatchMaskTransformation implements Transformation<Bitmap> {
private Context mContext; private Context mContext;
private BitmapPool mBitmapPool; private BitmapPool mBitmapPool;
private int mMaskId; private int mMaskId;
private int mWidth;
private int mHeight;
public NinePatchMaskTransformation(Context context, int maskId, int width, int height) { public NinePatchMaskTransformation(Context context, int maskId) {
mBitmapPool = Glide.get(context).getBitmapPool(); mBitmapPool = Glide.get(context).getBitmapPool();
mContext = context; mContext = context;
mMaskId = maskId; mMaskId = maskId;
mWidth = width;
mHeight = height;
} }
@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 width = source.getWidth();
int height = outHeight == Target.SIZE_ORIGINAL ? source.getHeight() : outHeight; int height = source.getHeight();
int maskWidth = outWidth == Target.SIZE_ORIGINAL ? width : outWidth;
int maskHeight = outHeight == Target.SIZE_ORIGINAL ? height : outHeight;
boolean maskFromBitmapPool = true; Bitmap mask = Bitmap.createBitmap(maskWidth, maskHeight, Bitmap.Config.ARGB_8888);
Bitmap mask = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
if (mask == null) {
maskFromBitmapPool = false;
mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
Drawable drawable; Drawable drawable = Util.getMaskDrawable(mContext, mMaskId);
if (Build.VERSION.SDK_INT >= 21) { drawable.setBounds(0, 0, maskWidth, maskHeight);
drawable = mContext.getDrawable(mMaskId);
} else {
drawable = mContext.getResources().getDrawable(mMaskId);
}
drawable.setBounds(0, 0, width, height);
Canvas canvas = new Canvas(mask); Canvas canvas = new Canvas(mask);
drawable.draw(canvas); drawable.draw(canvas);
Bitmap bitmap = mBitmapPool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
if (bitmap == null) { if (bitmap == null) {
bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
} }
Paint paint = new Paint(); Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas = new Canvas(bitmap); canvas = new Canvas(bitmap);
canvas.drawBitmap(mask, new Rect(0, 0, mask.getWidth(), mask.getHeight()), new Rect(0, 0, canvas.drawBitmap(mask, new Rect(0, 0, maskWidth, maskHeight), new Rect(0, 0, width, height),
source.getWidth(), source.getHeight()), null); null);
canvas.drawBitmap(source, 0, 0, paint); canvas.drawBitmap(source, 0, 0, paint);
if (maskFromBitmapPool) {
mBitmapPool.put(mask);
}
return BitmapResource.obtain(bitmap, mBitmapPool); return BitmapResource.obtain(bitmap, mBitmapPool);
} }
@Override @Override public String getId() {
public String getId() { return "NinePatchMaskTransformation(mMaskId=" + mMaskId + ")";
return "NinePatchMaskTransformation(mMaskId=" + mMaskId
+ ", width=" + mWidth + ", height=" + mHeight + ")";
} }
} }

View File

@ -0,0 +1,43 @@
package jp.wasabeef.glide.transformations.internal;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
/**
* Copyright (C) 2015 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public final class Util {
private Util() {
// Utility class.
}
public static Drawable getMaskDrawable(Context context, int maskId) {
Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable = context.getDrawable(maskId);
} else {
drawable = context.getResources().getDrawable(maskId);
}
if (drawable == null) {
throw new IllegalArgumentException("maskId is invalid");
}
return drawable;
}
}