绘制图形时,通过绘制相交的部分来实现,不多说,看代码:
public static Bitmap getRoundedCornerBitmap(Bitmap sourceBitmap, float roundX, float roundY) { try { Bitmap targetBitmap = Bitmap.createBitmap(sourceBitmap.getWidth(), sourceBitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(targetBitmap); Paint paint = new Paint(); Rect rect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()); RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); canvas.drawRoundRect(rectF, roundX, roundY, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(sourceBitmap, rect, rect, paint); return targetBitmap; } catch (Exception e) { e.printStackTrace(); } return null; }
?效果图:
?
?
?
?