Android 使用 Glide 实现圆形和圆角图片

1.实现圆形图片

1.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
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

/**
* Created by xpf on 2017/6/17 :)
* Function:Glide Transformation 实现圆形图片
*/

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;

// TODO this could be acquired from the pool too
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();
}
}

1.2用法

1
2
3
4
5
6
Glide.with(mContext)
.load(headpic)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.transform(new GlideCircleTransform(mContext))
.crossFade()
.into(ivIcon);

2.实现加载圆角图片

2.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
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 xpf on 2017/6/17 :)
* Function:Glide Transformation 实现圆角图片
*/

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 == 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);
}
}

2.1用法示例

1
2
3
4
5
Glide.with(mContext)
.load(image_url)
.transform(new GlideRoundTransform(mContext))
.crossFade()
.into(ivCard);

当然圆角的radius可以自己定义,使用重载带radius的构造传入即可

好了,今天就到这里吧~

夜深了,晚安咯( ̄o ̄) . z Z

本文标题:Android 使用 Glide 实现圆形和圆角图片

文章作者:x-sir

发布时间:2017年09月24日 - 23:47

最后更新:2018年12月08日 - 23:08

原始链接:http://www.x-sir.com/2017/09/24/Android 使用Glide实现圆形和圆角图片/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!
0%