glide 4.0 之后 和圆角裁剪CenterCrop冲突
作者:互联网
package 包名; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; import com.bumptech.glide.load.resource.bitmap.TransformationUtils; import java.security.MessageDigest; public class GlideRoundTransform extends BitmapTransformation { // private static float radius = 0f; // // public GlideRoundTransform(Context context) { // this(context, 4); // } // // public GlideRoundTransform(Context context, int dp) { // super(context); // this.radius = Resources.getSystem().getDisplayMetrics().density * dp; // } // // @Override // protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { // return roundCrop(pool, toTransform); // } // // private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { // if (source == null) return null; // // Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); // if (result == null) { // result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); // } // // Canvas canvas = new Canvas(result); // Paint paint = new Paint(); // paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); // paint.setAntiAlias(true); // RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); // canvas.drawRoundRect(rectF, radius, radius, paint); // return result; // } // // @Override // public String getId() { // return getClass().getName() + Math.round(radius); // } //----------上面的是glide 4.0 之前的处理方式的 //---------- 下面是 glide 4.0 之后的处理方式 private static float radius = 0f; public GlideRoundTransform(Context context) { this(context, 4); } public GlideRoundTransform(Context context, int dp) { // super(context); this.radius = Resources.getSystem().getDisplayMetrics().density * dp; } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight); return roundCrop(pool, bitmap); } private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rectF, radius, radius, paint); return result; } public String getId() { return getClass().getName() + Math.round(radius); } @Override public void updateDiskCacheKey(MessageDigest messageDigest) { } }
标签:4.0,glide,Bitmap,source,radius,result,import,CenterCrop,pool 来源: https://blog.csdn.net/qq_23911165/article/details/122384528