也许同行看到会笑话我吧,都出4.8.0了你还在使用3.几的版本。那你们笑去吧,我只作为一个笔记,希望能帮助到那些需要的人。不多说直接上代码了,其中后面部分可设置四个角是否为圆角
public static void LoadingRoundImg(Context context, String path,final ImageView imageView) {
Glide.with(context).load(path).asBitmap()
.transform(new MultiTransformation(new CenterCrop(context),new GlideRoundImageView(context,3)))
.placeholder(R.drawable.movie)//加载成功前显示的图片
.fallback(R.drawable.movie)//url为空的时候,显示的图片
.error(R.drawable.movie)
.into(imageView);
}
public class GlideRoundImageViewextends BitmapTransformation {
private static float radius =0f;
public GlideRoundImageView(Context context) {
this(context,4);
}
public GlideRoundImageView(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);
}
}
public static void LoadingRoundImg2(Context context, String path,final ImageView imageView) {
CornerTransform transformation =new CornerTransform(context,10);//只是绘制左上角和右上角圆角
transformation.setExceptCorner(false,false,true,true);
Glide.with(context).load(path).asBitmap()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.transform(new CenterCrop(context),transformation)
.placeholder(R.drawable.movie)//加载成功前显示的图片
.fallback(R.drawable.movie)//url为空的时候,显示的图片
.error(R.drawable.movie)
.into(imageView);
}
public class CornerTransformimplements Transformation {
private BitmapPoolmBitmapPool;
private float radius;
private boolean exceptLeftTop,exceptRightTop,exceptLeftBottom,exceptRightBotoom;
public void setExceptCorner(boolean leftTop,boolean rightTop,boolean leftBottom,boolean rightBottom) {
this.exceptLeftTop = leftTop;
this.exceptRightTop = rightTop;
this.exceptLeftBottom = leftBottom;
this.exceptRightBotoom = rightBottom;
}
public CornerTransform(Context context,float radius) {
this.mBitmapPool = Glide.get(context).getBitmapPool();
this.radius = radius;
}
@Override
public Resource transform(Resource resource,int outWidth,int outHeight) {
Bitmap source = resource.get();
int finalWidth, finalHeight;
float ratio;
if (outWidth > outHeight) {
ratio = (float) outHeight / (float) outWidth;
finalWidth = source.getWidth();
finalHeight = (int) ((float) source.getWidth() * ratio);
if (finalHeight > source.getHeight()) {
ratio = (float) outWidth / (float) outHeight;
finalHeight = source.getHeight();
finalWidth = (int) ((float) source.getHeight() * ratio);
}
}else if (outWidth < outHeight) {
ratio = (float) outWidth / (float) outHeight;
finalHeight = source.getHeight();
finalWidth = (int) ((float) source.getHeight() * ratio);
if (finalWidth > source.getWidth()) {
ratio = (float) outHeight / (float) outWidth;
finalWidth = source.getWidth();
finalHeight = (int) ((float) source.getWidth() * ratio);
}
}else {
finalHeight = source.getHeight();
finalWidth = finalHeight;
}
this.radius *= (float) finalHeight / (float) outHeight;
Bitmap outBitmap =this.mBitmapPool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
if (outBitmap ==null) {
outBitmap = Bitmap.createBitmap(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
}
Canvas canvas =new Canvas(outBitmap);
Paint paint =new Paint();
BitmapShader shader =new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
int width = (source.getWidth() - finalWidth) /2;
int height = (source.getHeight() - finalHeight) /2;
if (width !=0 || height !=0) {
Matrix matrix =new Matrix();
matrix.setTranslate((float) (-width), (float) (-height));
shader.setLocalMatrix(matrix);
}
paint.setShader(shader);
paint.setAntiAlias(true);
RectF rectF =new RectF(0.0F,0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
canvas.drawRoundRect(rectF,this.radius,this.radius, paint);//先绘制圆角矩形
if (exceptLeftTop) {
canvas.drawRect(0,0,radius,radius, paint);
}
if (exceptRightTop) {
canvas.drawRect(canvas.getWidth() -radius,0,radius,radius, paint);
}
if (exceptLeftBottom) {
canvas.drawRect(0, canvas.getHeight() -radius,radius, canvas.getHeight(), paint);
}
if (exceptRightBotoom) {
canvas.drawRect(canvas.getWidth() -radius, canvas.getHeight() -radius, canvas.getWidth(), canvas.getHeight(), paint);
}
return BitmapResource.obtain(outBitmap,this.mBitmapPool);
}
@Override
public String getId() {
return this.getClass().getName();
}