给图片倒圆角

来源:互联网 发布:入骨相思知不知小说 编辑:程序博客网 时间:2024/05/18 09:15
public class mhFillet {

public static final int LEFT = 1;
public static final int RIGHT = 2;
public static final int TOP = 3;
public static final int BOTTOM = 4;
public static final int ALL = 5;

// public enum HalfType {
// LEFT, // 左上角 + 左下角
// RIGHT, // 右上角 + 右下角
// TOP, // 左上角 + 右上角
// BOTTOM, // 左下角 + 右下角
// ALL // 四角
// }
//

/**
* 将图片的四角圆弧化
*
* @param bitmap 原图
* @param roundPixels 弧度
* @param half (上/下/左/右)半部分圆角
* @return
*/
public static Bitmap getRoundCornerImage(Bitmap bitmap, int roundPixels, int half) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();

Bitmap roundConcerImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//创建一个和原始图片一样大小的位图
Canvas canvas = new Canvas(roundConcerImage);//创建位图画布
Paint paint = new Paint();//创建画笔

Rect rect = new Rect(0, 0, width, height);//创建一个和原始图片一样大小的矩形
RectF rectF = new RectF(rect);
paint.setAntiAlias(true);// 抗锯齿

canvas.drawRoundRect(rectF, roundPixels, roundPixels, paint);//画一个基于前面创建的矩形大小的圆角矩形
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//设置相交模式

canvas.drawBitmap(bitmap, null, rect, paint);//把图片画到矩形去

  switch (half) {


case LEFT:
return Bitmap.createBitmap(roundConcerImage, 0, 0, width - roundPixels, height);
case RIGHT:
return Bitmap.createBitmap(roundConcerImage, width - roundPixels, 0, width - roundPixels, height);
case TOP: // 上半部分圆角化 “- roundPixels”实际上为了保证底部没有圆角,采用截掉一部分的方式,就是截掉和弧度一样大小的长度
return Bitmap.createBitmap(roundConcerImage, 0, 0, width, height - roundPixels);
case BOTTOM:
return Bitmap.createBitmap(roundConcerImage, 0, height - roundPixels, width, height - roundPixels);
case ALL:
return roundConcerImage;
default:
return roundConcerImage;
}
}

}
0 0
原创粉丝点击