最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android裁剪圖片為圓形圖片的實現(xiàn)原理與代碼

 更新時間:2013年01月07日 17:38:42   作者:  
這個方法是根據(jù)傳入的圖片的高度(height)和寬度(width)決定的,如果是 width <= height時,則會裁剪高度,裁剪的區(qū)域是寬度不變高度從頂部到寬度width的長度
以前在eoe論壇中找過裁剪圖片為圓形圖片的方法,但是效果都不是很理想,這幾天因為公司業(yè)務(wù)的要求,需要對頭像進行裁剪以圓形的方式顯示,這個方法是根據(jù)傳入的圖片的高度(height)和寬度(width)決定的,如果是 width <= height時,則會裁剪高度,裁剪的區(qū)域是寬度不變高度從頂部到寬度width的長度;如果 width > height,則會裁剪寬度,裁剪的區(qū)域是高度不變,寬度是取的圖片寬度的中心區(qū)域,不過不同的業(yè)務(wù)需求,對裁剪圖片要求不一樣,可以根據(jù)業(yè)務(wù)的需求來調(diào)整裁剪的區(qū)域。

好了,不多說了,直接上代碼
復(fù)制代碼 代碼如下:

/**
* 轉(zhuǎn)換圖片成圓形
* @param bitmap 傳入Bitmap對象
* @return
*/
public Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,
height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}

相關(guān)文章

最新評論

长海县| 田林县| 工布江达县| 旌德县| 靖远县| 桃江县| 漳浦县| 临颍县| 卓资县| 平湖市| 襄垣县| 太仓市| 工布江达县| 岑溪市| 务川| 驻马店市| 阿合奇县| 临沂市| 桃源县| 西和县| 安化县| 安顺市| 古交市| 墨竹工卡县| 闸北区| 阿荣旗| 科技| 鞍山市| 微博| 正镶白旗| 土默特左旗| 青岛市| 郓城县| 修武县| 冀州市| 莱西市| 湄潭县| 和龙市| 徐水县| 新蔡县| 易门县|