转发/yangyahuiguo/article/details/52253401?utm_source=blogxgwz2
1.*截取正方形,可设置圆角
Bitmap bitmap = (getResources(),);
//设置()可以获得圆形
Bitmap bitmap1 = ClipSquareBitmap(bitmap,200,());
(bitmap1);
public static Bitmap ClipSquareBitmap(Bitmap bmp, int width, int radius) {
if (bmp == null || width <= 0)
return null;
//如果图片比较小就没必要进行缩放了
/**
* 把图片进行缩放,以宽高最小的一边为准,缩放图片比例
* */
if (() > width && () > width) {
if (() > ()) {
bmp = (bmp, (int) (((float) width) * () / ()), width, false);
} else {
bmp = (bmp, width, (int) (((float) width) * () / ()), false);
}
} else {
width = () > () ? () : ();
("zeyu","宽" + width + ",w" + () + ",h" + ());
if (radius > width) {
radius = width;
}
}
Bitmap output = (width, width, .ARGB_8888);
Canvas canvas = new Canvas(output);
//设置画笔全透明
(0, 0, 0, 0);
Paint paints = new Paint();
();
(true);//去锯齿
(true);
//防抖动
(true);
//把图片圆形绘制矩形
if (radius <= 0)
(new Rect(0, 0, width, width), paints);
else //绘制圆角
(new RectF(0, 0, width, width), radius, radius, paints);
// 取两层绘制交集。显示前景色。
(new PorterDuffXfermode(.SRC_IN));
Rect rect = new Rect();
if (() >= ()) {
((() - width) / 2, 0, (() + width) / 2, width);
} else {
(0, (() - width) / 2, width, (() + width) / 2);
}
Rect rect2 = new Rect(0, 0, width, width);
//第一个rect 针对bmp的绘制区域,rect2表示绘制到上面位置
(bmp, rect, rect2, paints);
();
return output;
}
2.这种方式也可获得圆形,但是不是以*为圆心切的,图像偏上方
public static Bitmap circleBitmap(Bitmap source) {
int width = ();
Bitmap bitmap = (width, width, .ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
(true);
(width / 2, width / 2, width / 2, paint);
//设置图片相交情况下的处理方式
//setXfermode:设置当绘制的图像出现相交情况时候的处理方式的,它包含的常用模式有:
//.SRC_IN 取两层图像交集部分,只显示上层图像
//.DST_IN 取两层图像交集部分,只显示下层图像
(new PorterDuffXfermode(.SRC_IN));
(source, 0, 0, paint);
return bitmap;
}