Android中Glide加载圆形图片和圆角图片实例代码

 更新时间:2017年05月31日 15:18:38   作者:陪你唠嗑  
本篇文章主要介绍了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中自定义一个View的方法详解

    Android中自定义一个View的方法详解

    这篇文章主要介绍了Android中自定义一个View的方法,结合实例形式较为详细的分析了Android中自定义View的具体步骤与相关注意事项,需要的朋友可以参考下
    2016-07-07
  • Kotlin条件控制语句汇总讲解

    Kotlin条件控制语句汇总讲解

    条件控制是每门编程语言中必不可少的,一般就是使用我们所熟知的 ifelse ,来作为我们代码逻辑选择条件控制。 在 Java 中一般使用 ifelse 和 switch-case 来作为条件控制,而在 Kotlin 中则是使用 if-else 和 when 来作为条件控制
    2022-09-09
  • Android网格布局GridView学习使用

    Android网格布局GridView学习使用

    这篇文章主要为大家详细介绍了Android网格布局GirdView的学习使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • android编程之ip2id程序实例

    android编程之ip2id程序实例

    这篇文章主要介绍了android编程之ip2id程序,实例分析了Android实现ip转换id的相关技巧,需要的朋友可以参考下
    2015-04-04
  • android防止提交事件时触发多个表单中的按钮

    android防止提交事件时触发多个表单中的按钮

    这篇文章主要介绍了android防止提交事件时触发多个表单中的按钮,
    2015-05-05
  • 详解用flutter制作上班摸鱼应用

    详解用flutter制作上班摸鱼应用

    本文主要介绍了用flutter制作上班摸鱼应用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Android四大组件之Service详解

    Android四大组件之Service详解

    今天小编就为大家分享一篇关于Android四大组件之Service详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • 为Android的apk应用程序文件加壳以防止反编译的教程

    为Android的apk应用程序文件加壳以防止反编译的教程

    这篇文章主要介绍了为Android的apk应用程序文件加壳以防止反编译的教程,同时对apk程序的解壳操作也有详细讲解,需要的朋友可以参考下
    2016-04-04
  • Android实现获取短信验证码并自动填写功能

    Android实现获取短信验证码并自动填写功能

    这篇文章主要为大家详细介绍了Android实现获取短信验证码并自动填写功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android图片加载缓存框架Glide

    Android图片加载缓存框架Glide

    这篇文章主要为大家详细介绍了Android图片加载缓存框架Glide,感兴趣的小伙伴们可以参考一下
    2016-03-03

最新评论