美文网首页
2018-01-23 Android中Glide加载圆形图片和

2018-01-23 Android中Glide加载圆形图片和

作者: 王培921223 | 来源:发表于2018-01-23 15:48 被阅读0次

    一、简介:

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

    二、网上的实现方式

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

    使用 Canvas 和 Paint 来绘制

    使用 Android.support.v4.graphics.drawable.RoundedBitmapDrawable

    实现圆形图片:

    /**

     *

     * Glide 圆形图片 Transform

     */

    publicclassGlideCircleTransform extendsBitmapTransformation {

      publicGlideCircleTransform(Context context) {

        super(context);

      }

      @Override

      protectedBitmap transform(BitmapPool pool, Bitmap toTransform, intoutWidth, intoutHeight) {

        returncircleCrop(pool, toTransform);

      }

      privatestaticBitmap circleCrop(BitmapPool pool, Bitmap source) {

        if(source == null) returnnull;

        intsize = Math.min(source.getWidth(), source.getHeight());

        intx = (source.getWidth() - size) / 2;

        inty = (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 = newCanvas(result);

        Paint paint = newPaint();

        paint.setShader(newBitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

        paint.setAntiAlias(true);

        floatr = size / 2f;

        canvas.drawCircle(r, r, r, paint);

        returnresult;

      }

      @Override

      publicString getId() {

        returngetClass().getName();

      }

    }

    实现圆角图片:

    /**

     * Glide 圆角 Transform

     */

    publicclassGlideRoundTransform extendsBitmapTransformation {

      privatestaticfloatradius = 0f;

      /**

     * 构造函数 默认圆角半径 4dp

     *

     * @param context Context

     */

      publicGlideRoundTransform(Context context) {

        this(context, 4);

      }

      /**

     * 构造函数

     *

     * @param context Context

     * @param dp 圆角半径

     */

      publicGlideRoundTransform(Context context, intdp) {

        super(context);

        radius = Resources.getSystem().getDisplayMetrics().density * dp;

      }

      @Override

      protectedBitmap transform(BitmapPool pool, Bitmap toTransform, intoutWidth, intoutHeight) {

        returnroundCrop(pool, toTransform);

      }

      privatestaticBitmap roundCrop(BitmapPool pool, Bitmap source) {

        if(source == null) returnnull;

        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 = newCanvas(result);

        Paint paint = newPaint();

        paint.setShader(newBitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

        paint.setAntiAlias(true);

        RectF rectF = newRectF(0f, 0f, source.getWidth(), source.getHeight());

        canvas.drawRoundRect(rectF, radius, radius, paint);

        returnresult;

      }

      @Override

      publicString getId() {

        returngetClass().getName() + Math.round(radius);

      }

    }

    三、笔者比较喜欢的简便的实现方式

    //加载圆角图片

       publicstaticvoidloadRoundImage(finalContext context, String url,finalImageView imageView){

         Glide.with(context)

             .load(url)

             .asBitmap()

             .placeholder(placeholder)

             .error(placeholder)

             .diskCacheStrategy(DiskCacheStrategy.ALL) //设置缓存

             .into(newBitmapImageViewTarget(imageView){

               @Override

               protectedvoidsetResource(Bitmap resource) {

                 super.setResource(resource);

                 RoundedBitmapDrawable circularBitmapDrawable =

                     RoundedBitmapDrawableFactory.create(context.getResources(), resource);

                 circularBitmapDrawable.setCornerRadius(10); //设置圆角弧度

                 imageView.setImageDrawable(circularBitmapDrawable);

               }

             });

       }

      //加载圆形图片

      publicstaticvoidloadCirclePic(finalContext context, String url, finalImageView imageView) {

        Glide.with(context)

            .load(url)

            .asBitmap()

            .placeholder(placeholder)

            .error(placeholder)

            .diskCacheStrategy(DiskCacheStrategy.ALL) //设置缓存

            .into(newBitmapImageViewTarget(imageView) {

              @Override

              protectedvoidsetResource(Bitmap resource) {

                RoundedBitmapDrawable circularBitmapDrawable =

                    RoundedBitmapDrawableFactory.create(context.getResources(), resource);

                circularBitmapDrawable.setCircular(true);

                imageView.setImageDrawable(circularBitmapDrawable);

              }

            });

      }

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

    publicstaticBitmap drawableToBitmap(Drawable drawable) {

        // 取 drawable 的长宽

        intw = drawable.getIntrinsicWidth();

        inth = 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 = newCanvas(bitmap);

        drawable.setBounds(0, 0, w, h);

        // 把 drawable 内容画到画布中

        drawable.draw(canvas);

        returnbitmap;

      }

    /**

     * RoundedBitmapDrawable 是 V4 下的一个类,不能简单的通过:强制转换成 BitmapDrawable

     * Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap();

     */

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    相关文章

      网友评论

          本文标题:2018-01-23 Android中Glide加载圆形图片和

          本文链接:https://www.haomeiwen.com/subject/jjbsaxtx.html