温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Android中利用Glide实现一个图片圆角功能

发布时间:2020-12-07 15:25:36 来源:亿速云 阅读:845 作者:Leah 栏目:移动开发

如何在Android中利用Glide实现一个图片圆角功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

一、简介:

介绍两种使用 BitmapTransformation 来实现 Glide 加载圆形图片和圆角图片的方法。Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 来进行处理。

二、网上的实现方式

这里介绍下网上常见的方式和使用 RoundedBitmapDrawable 两种方法,本质上是差不多的:

  1. 使用 Canvas 和 Paint 来绘制
  2. 使用 Android.support.v4.graphics.drawable.RoundedBitmapDrawable

实现圆形图片:

/**
 * 
 * Glide 圆形图片 Transform
 */

public class GlideCircleTransform extends BitmapTransformation {
  public GlideCircleTransform(Context context) {
    super(context);
  }

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

  private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
    Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
    if (result == null) {
      result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    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 String getId() {
    return getClass().getName();
  }
}

实现圆角图片:

/**
 * Glide 圆角 Transform
 */

public class GlideRoundTransform extends BitmapTransformation {

  private static float radius = 0f;

  /**
 * 构造函数 默认圆角半径 4dp
 *
 * @param context Context
 */
  public GlideRoundTransform(Context context) {
    this(context, 4);
  }

  /**
 * 构造函数
 *
 * @param context Context
 * @param dp 圆角半径
 */
  public GlideRoundTransform(Context context, int dp) {
    super(context);
    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 loadRoundImage(final Context context, String url,final ImageView imageView){
     Glide.with(context)
         .load(url)
         .asBitmap()
         .placeholder(placeholder)
         .error(placeholder)
         .diskCacheStrategy(DiskCacheStrategy.ALL) //设置缓存
         .into(new BitmapImageViewTarget(imageView){
           @Override
           protected void setResource(Bitmap resource) {
             super.setResource(resource);
             RoundedBitmapDrawable circularBitmapDrawable =
                 RoundedBitmapDrawableFactory.create(context.getResources(), resource);
             circularBitmapDrawable.setCornerRadius(10); //设置圆角弧度
             imageView.setImageDrawable(circularBitmapDrawable);
           }
         });
   }


  //加载圆形图片
  public static void loadCirclePic(final Context context, String url, final ImageView imageView) {
    Glide.with(context)
        .load(url)
        .asBitmap()
        .placeholder(placeholder)
        .error(placeholder)
        .diskCacheStrategy(DiskCacheStrategy.ALL) //设置缓存
        .into(new BitmapImageViewTarget(imageView) {
          @Override
          protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);
          }
        });

  }

关于drawableToBitmap的源码的实现是这样的

public static Bitmap drawableToBitmap(Drawable drawable) {
    // 取 drawable 的长宽
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();

    // 取 drawable 的颜色格式
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
        : Bitmap.Config.RGB_565;
    // 建立对应 bitmap
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    // 建立对应 bitmap 的画布
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    // 把 drawable 内容画到画布中
    drawable.draw(canvas);
    return bitmap;
  }
/**
 * RoundedBitmapDrawable 是 V4 下的一个类,不能简单的通过:强制转换成 BitmapDrawable
 * Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap();
 */

关于如何在Android中利用Glide实现一个图片圆角功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI