Glide实现圆角 圆形图片

本文介绍了一种使用Glide库在Android应用中展示圆形图片的方法。通过自定义`GlideRoundTransform`类,可以轻松地将任何图片转换为带有圆角的图片。此外,还提供了一个利用`RoundedBitmapDrawable`实现圆形图片显示的例子。
  1. 实现圆形图片 , 传参第一个为上下文, 第二个为角度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.hh.beauter.util;
 
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 com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
 
/**
* Created by Hh on 2017/1/5.
*/
 
public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;
 
    public GlideRoundTransform(Context context) {
        this(context, 4);
    }
 
    public GlideRoundTransform(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 == nullreturn 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);
    }
}

  使用方法:

1
2
3
Glide.with(ActActivity.this).load(UrlUtil.LOCALL_IP+good.getImgs().get(0).getImg())
                   .transform(new GlideRoundTransform(ActActivity.this,20))
                   .into(viewHolder.img);

  

 

  2. 加载圆形图片

1
2
3
4
5
6
7
8
9
10
//圆形
 Glide.with(this).load(photoUrl).asBitmap().centerCrop().into(new BitmapImageViewTarget(photo) {
 @Override
 protected void setResource(Bitmap resource) {
 RoundedBitmapDrawable circularBitmapDrawable =
 RoundedBitmapDrawableFactory.create(getResources(), resource);
 circularBitmapDrawable.setCircular(true);
 photo.setImageDrawable(circularBitmapDrawable);
 }
 });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值