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ū)域。
好了,不多說了,直接上代碼
/**
* 轉(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;
}
好了,不多說了,直接上代碼
復制代碼 代碼如下:
/**
* 轉(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;
}
您可能感興趣的文章:
- Android實現(xiàn)拍照、選擇圖片并裁剪圖片功能
- 解決Android從相冊中獲取圖片出錯圖片卻無法裁剪問題的方法
- Android實現(xiàn)從本地圖庫/相機拍照后裁剪圖片并設(shè)置頭像
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android實現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- Android編程實現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能
- android文件上傳示例分享(android圖片上傳)
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android實現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android實現(xiàn)圖片裁剪和上傳
相關(guān)文章
Android多國語言轉(zhuǎn)換Excel及Excel轉(zhuǎn)換為string詳解
這篇文章主要給大家介紹了關(guān)于Android多國語言轉(zhuǎn)換Excel以及Excel轉(zhuǎn)換為string的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2019-01-01Android中ViewPager1和ViewPager2的使用教程
這篇文章主要介紹了Android中ViewPager1和ViewPager2的使用,效果圖是結(jié)合BottomNavigationView+ViewPager一起使用的,具體實例代碼跟隨小編一起看看吧2021-10-10Android ToolBar 修改邊距的實現(xiàn)方法
這篇文章主要介紹了Android ToolBar 修改邊距的實現(xiàn)方法的相關(guān)資料,通過此文希望能幫助到大家,需要的朋友可以參考下2017-08-08android基于socket的局域網(wǎng)內(nèi)服務(wù)器與客戶端加密通信
本篇文章主要介紹了android基于socket的局域網(wǎng)內(nèi)服務(wù)器與客戶端加密通信,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2017-04-04