RoundedBitmapDrawable剪裁

一个Drawable,它包装一个位图,可以用圆角绘制。您可以从文件路径,输入流或Bitmap对象创建RoundedBitmapDrawable

常用方法

  1. setCircular() 将图像形状设置为圆形
  2. setCornerRadius() 设置绘制位图时要应用的拐角半径。
  3. setGravity() 设置用于将位图放置在其边界内的重心。
  4. getBitmap() 返回此drawable用于渲染的位图。
  5. getIntrinsicHeight() 返回drawable的内在高度。
  6. setAlpha() 指定drawable的alpha值。

构造方法

1
2
3
4
5
//导入 package android.support.v4.graphics.drawable;
RoundedBitmapDrawable rd = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
RoundedBitmapDrawable rd = RoundedBitmapDrawableFactory.create(getResources(), "filePath");
RoundedBitmapDrawable rd = RoundedBitmapDrawableFactory.create(getResources(), inputStream);

基本使用

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* 获得圆形图片
*
* @param context
* @param src
* @return
*/
public static RoundedBitmapDrawable getCircleBitmap(Context context, Bitmap src) {
//获得正方形bitmap
Bitmap scaledBitmap = getSquareBitmap(src, 0, 0);
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), scaledBitmap);
drawable.setGravity(Gravity.CENTER);
//圆形
drawable.setCircular(true);
//判断是否是新bitmap,再回收
if (!scaledBitmap.equals(src) && !scaledBitmap.isRecycled()) {
scaledBitmap.recycle();
}
return drawable;
}
/**
* 获得圆形带边框图片
*
* @param context
* @param src
* @return
*/
public static RoundedBitmapDrawable getCircleBorderBitmap(Context context, Bitmap src, int border, int color) {
//获得正方形带边框的bitmap
Bitmap scaledBitmap = getSquareBitmap(src, border, color);
//圆形
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), scaledBitmap);
drawable.setGravity(Gravity.CENTER);
drawable.setCircular(true);
if (!scaledBitmap.equals(src) && !scaledBitmap.isRecycled()) {
scaledBitmap.recycle();
}
return drawable;
}
/**
* 获取圆角图片
*
* @param context
* @param src
* @param radius
* @return
*/
public static RoundedBitmapDrawable getRoundBitmap(Context context, Bitmap src, int radius) {
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), src);
//设置4个角的角度
drawable.setCornerRadius(radius);
return drawable;
}
/**
* 获得正方形bitmap,居中剪裁
*
* @param src
* @return
*/
public static Bitmap getSquareBitmap(Bitmap src, int border, int color) {
int bitmapWidth = src.getWidth();
int bitmapHeight = src.getHeight();
int square = Math.min(bitmapHeight, bitmapWidth);
if (bitmapWidth != bitmapHeight) {
Bitmap scaledBitmap = Bitmap.createBitmap(square, square, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
//计算偏移的位置
int x = square - bitmapWidth + border;
int y = square - bitmapHeight + border;
//居中的正方形
if (border > 0) {
Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(border);
borderPaint.setColor(color);
canvas.drawBitmap(src, x / 2, y / 2, null);
//添加边框
canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, square / 2, borderPaint);
} else {
canvas.drawBitmap(src, x / 2, y / 2, null);
}
return scaledBitmap;
} else {
return src;
}
}