Glide显示两个圆角的图片

RequestOptions options1 = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.mipmap.ic_launcher)//预加载图片
                        .error(R.mipmap.ic_launcher)//加载失败显示图片
                        .priority(Priority.HIGH)//优先级
                        .diskCacheStrategy(DiskCacheStrategy.NONE)//缓存策略
                        .transform(new GlideRoundTransform(20));//转化为圆角
Glide.with(this).load(imgUrl).apply(options1).into(img1);
public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;

    public GlideRoundTransform() {
        this(4);
    }

    public GlideRoundTransform(int dp) {
        super();
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }


    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        //变换的时候裁切
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, bitmap);
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }


    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);
//        RectF rectRound = new RectF(0f, 100f, source.getWidth(), source.getHeight());
//        canvas.drawRect(rectRound, paint);
        return result;
    }
}

Glide显示两个圆角的图片_第1张图片

RectF rectRound = new RectF(0f, 100f, source.getWidth(), source.getHeight());
canvas.drawRect(rectRound, paint);
Glide显示两个圆角的图片_第2张图片

你可能感兴趣的:(android开发)