Glide 圆角图片

1、自定义GlideRoundRect类,继承BitmapTransformation

package com.likebamboo.phoneshow.util;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.widget.ImageView;

public class GlideRoundRect extends BitmapTransformation {

    private static float radius = 0f;

    public GlideRoundRect(Context context) {
        this(context, 4);
    }

    public GlideRoundRect(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);
    }

}

 

调用资源文件图片,显示成圆角:

iv_heard=(ImageView) findViewById(R.id.iv_heard);
        int resourceId = R.drawable.touxiang;
        Glide.with(this).
        load(resourceId).
        transform(new GlideRoundRect(this,5))
        .into(iv_heard);

 

加载url图片:

Glide.with(context)
        .load(HttpRequest.PHOTO+prodata.getIcon())
        .centerCrop()
        .transform(new RoundRect(context,15))
        .into(holder.iv_hplist_icon);

                  

                  

 

发布了17 篇原创文章 · 获赞 6 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览