方案1

处理Bitmap为圆形,再给ImageView设置该Bitmap

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
public class Util {
private static final int STROKE_WIDTH = 4;
//从assets资源中获取图片
public static Bitmap getBitmap(Context context, String filename) {
Bitmap image = null;
AssetManager am = context.getResources().getAssets();
try {
InputStream is = am.open(filename);
image = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
public static Bitmap toRoundBitmap(Context context, String filename) {
Bitmap bitmap = getBitmap(context, filename);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
left = 0;
bottom = width;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,
height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(4);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
//画白色圆圈
paint.reset();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(STROKE_WIDTH);
paint.setAntiAlias(true);
canvas.drawCircle(width / 2, width / 2, width / 2 - STROKE_WIDTH / 2, paint);
return output;
}
}

方案2

自定义View继承自ImageView,在setImageDrawable()方法中把BitamapDrawable转为RoundedBitmapDrawable

bug ColorDrawable不能转化,需获取bitmap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Override
public void setBackgroundDrawable(Drawable drawable) {
if (drawable instanceof ColorDrawable) {
Bitmap bm = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(((ColorDrawable)drawable).getColor());
paint.setStyle(Paint.Style.FILL);
//根据创建的bitmap新建一张画布
Canvas canvas = new Canvas(bm);
canvas.drawCircle(getWidth()/2,getWidth()/2,getWidth()/2,paint);
RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(),
bm);
roundedDrawable.setCircular(true);
super.setBackgroundDrawable(roundedDrawable);
} else {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
@SuppressWarnings("ConstantConditions")
RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(),
bitmapDrawable.getBitmap());
roundedDrawable.setCircular(true);
super.setBackgroundDrawable(roundedDrawable);
}
}
  • Drawable转为Bitmap的方法
1
2
3
4
mExpandBm = Bitmap.createBitmap(mExpandDrawable.getIntrinsicWidth(),mExpandDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas cv1 = new Canvas(mExpandBm);
mExpandDrawable.setBounds(0,0,mExpandDrawable.getIntrinsicWidth(),mExpandDrawable.getIntrinsicHeight());
mExpandDrawable.draw(cv1);