本文实例讲述了Android生成带圆角的Bitmap图片。分享给大家供大家参考。具体如下:
有时候我们在开发Android应用时,会遇到圆角图片的问题,那么,我们如何在Android中用代码来生成圆角Bitmap图片呢?下面这段代码也许能够帮到你。
该方法主要用到了drawRoundRect来画圆角矩形,然后通过drawBitmap来画图片。
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
|
//生成圆角图片
public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {
try {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect( 0 , 0 , bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new RectF( new Rect( 0 , 0 , bitmap.getWidth(),
bitmap.getHeight()));
final float roundPx = 14 ;
paint.setAntiAlias( true );
canvas.drawARGB( 0 , 0 , 0 , 0 );
paint.setColor(Color.BLACK);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode( new PorterDuffXfermode(Mode.SRC_IN));
final Rect src = new Rect( 0 , 0 , bitmap.getWidth(),
bitmap.getHeight());
canvas.drawBitmap(bitmap, src, rect, paint);
return output;
} catch (Exception e) {
return bitmap;
}
}
|
希望本文所述对大家的Android程序设计有所帮助。