我正在使用这个非常普通的课程来圆角:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap .getHeight(),Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0,bitmap.getWidth(),bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0,0); paint.setColor(color); canvas.drawRoundRect(rectF,roundPx,paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap,rect,paint); return output; }
解决方法
这可能不是最有效的方法,但您可以通过在当前面具上绘画来填充圆角.您可以从当前代码开始,然后在适当的区域(角落)上使用canvas.drawRect(在调用canvas.drawRoundRect之后).我想像这样的东西只围绕左上角:
public static Bitmap getRoundedTopLeftCornerBitmap(Bitmap bitmap,int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap .getHeight(),Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0,bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; final Rect topRightRect = new Rect(bitmap.getWidth()/2,bitmap.getHeight()/2); final Rect bottomRect = new Rect(0,bitmap.getHeight()/2,bitmap.getHeight()); paint.setAntiAlias(true); canvas.drawARGB(0,0); paint.setColor(color); canvas.drawRoundRect(rectF,paint); // Fill in upper right corner canvas.drawRect(topRightRect,paint); // Fill in bottom corners canvas.drawRect(bottomRect,paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap,paint); return output; }
如果您愿意,可以在这里进行一些优化,但我认为它应该可行.一般的想法肯定应该.我没有尝试或测试过此代码,如果像素>它看起来不正确bitmap.getWidth()/ 2或像素> bitmap.getHeight()/ 2.然后,这也可能是真的.