Android 设置图片某一角为圆角
由于业务需要图片的左下角为圆角,而其他仍保持直角。网上的方法都是统一设置图片4个角为圆角,没有我需要的方法。
为此本人研究android.graphics并参考setXfermode方法的相关说明,自己设计了如下方法来完成任务
/**
* @author starshine
* @param bitmap The picture intent to add round corner
* @param radius The radius of the corner
* @param location Which corner to add round corner:
* 1-left_top;2-left_bottom;3-right_top;4-right_bottom
* @return The picture with round corner
*/
public Bitmap setRoundCorner(Bitmap bitmap, int radius,int... location) {
final int LEFT_TOP = 1;
final int LEFT_BOTTOM = 2;
final int RIGHT_TOP = 3;
final int RIGHT_BOTTOM = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Rect completeRect = new Rect(0, 0, width, height);
Rect leftTopRect = new Rect(0,0,radius,radius);
Rect leftBottomRect = new Rect(0,height-radius,radius,height);
Rect rightTopRect = new Rect(width-radius,0,width,radius);
Rect rightBottomRect = new Rect(width-radius,height-radius,width,height);
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawRect(completeRect, paint);
for (int corner : location) {
switch (corner) {
case LEFT_TOP:
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawRect(leftTopRect, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
canvas.drawCircle(radius, radius, radius, paint);
break;
case LEFT_BOTTOM:
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawRect(leftBottomRect, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
canvas.drawCircle(radius, height-radius, radius, paint);
break;
case RIGHT_TOP:
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawRect(rightTopRect, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
canvas.drawCircle(width-radius, radius, radius, paint);
break;
case RIGHT_BOTTOM:
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawRect(rightBottomRect, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
canvas.drawCircle(width-radius, height-radius, radius, paint);
break;
default:
break;
}
}
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
return output;
}
最后上个图片