Android Glide加载圆角或圆形图片

1.添加依赖

api ‘com.github.bumptech.glide:glide:4.7.1’

2.GlideImgManager.java为图片加载工具类

public class GlideImgManager {

private static final int ERROR = R.drawable.superman_app_icon;

//加载圆角矩形图片 使用默认的圆角大小 并默认全部圆角

public static void loadRoundImg(Context context,T url,ImageView img){
//RequestOptions 设置请求参数,通过apply方法设置
RequestOptions options = new RequestOptions()
// 但不保证所有图片都按序加载
// 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW
// 默认为Priority.NORMAL
// 如果没设置fallback,model为空时将显示error的Drawable,
// 如果error的Drawable也没设置,就显示placeholder的Drawable
.priority(Priority.NORMAL)//指定加载的优先级,优先级越高越优先加载,
.error(ERROR)
.placeholder(null)
// 缓存原始数据
// .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.optionalTransform(new GlideRoundTransform(CornerType.ALL));
// 图片加载库采用Glide框架
Glide.with(context)
.load(url)
.apply(options)
.transition(new DrawableTransitionOptions().crossFade())
.into(img);
}

//加载圆角矩形图片 使用默认的圆角大小 设置圆角方向
public static <T> void  loadRoundImg(Context context, T url,CornerType type,ImageView img){
    //RequestOptions 设置请求参数,通过apply方法设置
    RequestOptions options = new RequestOptions()
            // 但不保证所有图片都按序加载
            // 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW
            // 默认为Priority.NORMAL
            // 如果没设置fallback,model为空时将显示error的Drawable,
            // 如果error的Drawable也没设置,就显示placeholder的Drawable
            .priority(Priority.NORMAL)//指定加载的优先级,优先级越高越优先加载,
            .error(ERROR)
            .placeholder(null)
            // 缓存原始数据

// .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.optionalTransform(new GlideRoundTransform(type));
// 图片加载库采用Glide框架
Glide.with(context)
.load(url)
.apply(options)
.transition(new DrawableTransitionOptions().crossFade())
.into(img);
}

//加载圆角矩形图片 使用指定的圆角大小 默认圆角方向为所有
public static <T> void loadRoundImg(Context context, T url,ImageView img,float radius){
    //RequestOptions 设置请求参数,通过apply方法设置
    RequestOptions options = new RequestOptions()
            // 但不保证所有图片都按序加载
            // 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW
            // 默认为Priority.NORMAL
            // 如果没设置fallback,model为空时将显示error的Drawable,
            // 如果error的Drawable也没设置,就显示placeholder的Drawable
            .priority(Priority.NORMAL)//指定加载的优先级,优先级越高越优先加载,
            .error(ERROR)
            .placeholder(null)
            // 缓存原始数据

// .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.optionalTransform(new GlideRoundTransform(radius,CornerType.ALL));
// 图片加载库采用Glide框架
Glide.with(context)
.load(url)
.apply(options)
.transition(new DrawableTransitionOptions().crossFade())
.into(img);
}

//加载圆角矩形图片 使用指定的圆角大小 指定圆角方向
public static <T> void loadRoundImg(Context context, T url,ImageView img,CornerType type,float radius){
    //RequestOptions 设置请求参数,通过apply方法设置
    RequestOptions options = new RequestOptions()
            // 但不保证所有图片都按序加载
            // 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW
            // 默认为Priority.NORMAL
            // 如果没设置fallback,model为空时将显示error的Drawable,
            // 如果error的Drawable也没设置,就显示placeholder的Drawable
            .priority(Priority.NORMAL)//指定加载的优先级,优先级越高越优先加载,
            .error(ERROR)
            .placeholder(null)
            // 缓存原始数据

// .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.optionalTransform(new GlideRoundTransform(radius,type));
// 图片加载库采用Glide框架
Glide.with(context)
.load(url)
.apply(options)
.transition(new DrawableTransitionOptions().crossFade())
.into(img);
}

//加载圆角图片
public static <T> void loadCircleImg(Context context, T url,ImageView img){
    //RequestOptions 设置请求参数,通过apply方法设置
    RequestOptions options = new RequestOptions()
            // 但不保证所有图片都按序加载
            // 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW
            // 默认为Priority.NORMAL
            // 如果没设置fallback,model为空时将显示error的Drawable,
            // 如果error的Drawable也没设置,就显示placeholder的Drawable
            .priority(Priority.NORMAL)//指定加载的优先级,优先级越高越优先加载,
            .placeholder(null)
            .error(ERROR)

// .centerCrop()
.circleCrop()
.transform(new GlideCircleTransform());
// 图片加载库采用Glide框架
Glide.with(context)
.load(url)
.apply(options)
.transition(new DrawableTransitionOptions().crossFade())
.into(new SimpleTarget() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
img.setImageDrawable(resource);
}
});
}
}

3.GlideRoundTransform.java

public class GlideRoundTransform extends CenterCrop {

private  float mRadius;
private CornerType mCornerType;

public GlideRoundTransform(float radius, CornerType type){
    this.mRadius = DensityUtils.dp2px(radius);
    this.mCornerType = type;
}

public GlideRoundTransform(CornerType type){
    this(DensityUtils.dp2px(1f),type);
}

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap bm = super.transform(pool,toTransform,outWidth,outHeight);
    return roundCrop(pool,bm);
}

private Bitmap roundCrop(BitmapPool pool, Bitmap source){
    if (source == null){
        return null;
    }
    int width = source.getWidth();
    int height = source.getHeight();
    Bitmap.Config config = Bitmap.Config.ARGB_4444;
    Bitmap result = pool.get(source.getWidth(),source.getHeight(),config);
    if (result == null){
        result = Bitmap.createBitmap(source.getWidth(),source.getHeight(),config);
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(source,BitmapShader.TileMode.CLAMP,BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    Path path = new Path();
    drawRoundRect(canvas,paint,path,width,height);
    return result;
}


private void drawRoundRect(Canvas canvas,Paint paint,Path path,int width,int height){
    float[] rids;
    switch (mCornerType){
        case ALL:
            rids = new float[]{mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case TOP_LEFT:
            rids = new float[]{mRadius,mRadius,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case TOP_RIGHT:
            rids  = new float[]{0.0f,0.0f,mRadius,mRadius,0.0f,0.0f,0.0f,0.0f};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case BOTTOM_RIGHT:
            rids  = new float[]{0.0f,0.0f,0.0f,0.0f,mRadius,mRadius,0.0f,0.0f};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case BOTTOM_LEFT:
            rids  = new float[]{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,mRadius,mRadius};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case TOP:
            rids = new float[]{mRadius,mRadius,mRadius,mRadius,0.0f,0.0f,0.0f,0.0f};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case BOTTOM:
            rids  = new float[]{0.0f,0.0f,0.0f,0.0f,mRadius,mRadius,mRadius,mRadius};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case LEFT:
            rids = new float[]{mRadius,mRadius,0.0f,0.0f,0.0f,0.0f,mRadius,mRadius};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case RIGHT:
            rids  = new float[]{0.0f,0.0f,mRadius,mRadius,mRadius,mRadius,0.0f,0.0f};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case TOP_LEFT_BOTTOM_RIGHT:
            rids  = new float[]{mRadius,mRadius,0.0f,0.0f,mRadius,mRadius,0.0f,0.0f};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case TOP_RIGHT_BOTTOM_LEFT:
            rids  = new float[]{0.0f,0.0f,mRadius,mRadius,0.0f,0.0f,mRadius,mRadius};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT:
            rids  = new float[]{mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,0.0f,0.0f};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        case TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT:
            rids  = new float[]{0.0f,0.0f,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius};
            drawPath(rids,canvas,paint,path,width,height);
            break;
        default:
            throw new RuntimeException("RoundedCorners type not belong to CornerType");
    }
}

//rids 圆角的半径,依次为左上角xy半径,右上角,右下角,左下角
private void drawPath(float[] rids,Canvas canvas,Paint paint,Path path,int width,int height){
    path.addRoundRect(new RectF(0,0,width,height),rids,Path.Direction.CW);
    canvas.drawPath(path,paint);
}

@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

}

}

4.GlideCircleTransform.java

public class GlideCircleTransform extends BitmapTransformation {

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    return circleCrop(pool,toTransform);
}


private Bitmap circleCrop(BitmapPool pool, Bitmap source){
    if (source == null){
        return null;
    }
    //获取资源的长宽,获取最小值 子位图的像素个数
    int size = Math.min(source.getWidth(),source.getHeight());
    // 子位图第一个像素在源位图的X坐标
    int x = (source.getWidth() - size) / 2;
    //子位图第一个像素在源位图的y坐标
    int y = (source.getHeight() - size) / 2;
    //创建新位图  source 源位图
    Bitmap squared = Bitmap.createBitmap(source,x,y,size,size);
    //返回一个正好匹配给定宽、高和配置的只包含透明像素的Bitmap
    // 如果BitmapPool中找不到这样的Bitmap,就返回null
    Bitmap result = pool.get(size,size,Bitmap.Config.ARGB_8888);
    //当返回null 时,创建给定宽、高和配置的新位图
    if (result == null){
        result = Bitmap.createBitmap(size,size,Bitmap.Config.ARGB_8888);
    }
    //画图
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    // 设置shader
    paint.setShader(new BitmapShader(squared,BitmapShader.TileMode.CLAMP,BitmapShader.TileMode.CLAMP));
    //抗锯齿
    paint.setAntiAlias(true);
    float r = size / 2f;
    // 用设置好的画笔绘制一个圆
    canvas.drawCircle(r,r,r,paint);
    return result;
}

@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

}

}

5.调用

//加载圆角矩形图片
GlideImgManager.loadRoundImg(this,R.mipmap.defaut_test,img);
//加载圆形图片
GlideImgManager.loadCircleImg(this,R.mipmap.defaut_test,img);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值