Android中如何生成带圆角的Bitmap图片
有时候我们在开发Android应用时,会遇到圆角图片的问题,那么,我们如何在Android中用代码来生成圆角Bitmap图片呢?下面这段代码也许能够帮到你。
该方法主要用到了drawRoundRect来画圆角矩形,然后通过drawBitmap来画图片。
//生成圆角图片 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; } }
相关推荐
博了个客 2020-05-31
lixiaotao 2020-03-05
songfens 2020-01-10
MIKUScallion 2020-01-10
调调 2013-07-24
83580494 2013-06-29
TongsengsBlog 2013-08-27
Android进阶 2013-08-25
89143951 2011-08-25
八角塘塘主 2019-10-29
lerayZhang 2015-01-11
timewind 2019-07-01
Ashes 2019-06-30
learningITwell 2019-06-28
PGzxc 2019-06-28
xusong 2012-08-01
Android进阶 2019-06-27
helowken 2019-06-21
飞奔的熊猫 2019-06-21