Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
好了,不多說(shuō)了,直接上代碼
/**
* 轉(zhuǎn)換圖片成圓形
* @param bitmap 傳入Bitmap對(duì)象
* @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實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- 解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無(wú)法裁剪問(wèn)題的方法
- Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android 7.0中拍照和圖片裁剪適配的問(wèn)題詳解
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫(kù)與裁剪圖片功能
- android文件上傳示例分享(android圖片上傳)
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android實(shí)現(xiàn)圖片裁剪和上傳
相關(guān)文章
Android多國(guó)語(yǔ)言轉(zhuǎn)換Excel及Excel轉(zhuǎn)換為string詳解
這篇文章主要給大家介紹了關(guān)于Android多國(guó)語(yǔ)言轉(zhuǎn)換Excel以及Excel轉(zhuǎn)換為string的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-01-01Android中ViewPager1和ViewPager2的使用教程
這篇文章主要介紹了Android中ViewPager1和ViewPager2的使用,效果圖是結(jié)合BottomNavigationView+ViewPager一起使用的,具體實(shí)例代碼跟隨小編一起看看吧2021-10-10android開(kāi)發(fā)PathEffect問(wèn)題處理
本文主要整理了關(guān)于android中PathEffect的問(wèn)題匯總以及處理方式,以及給大家做了關(guān)于PathEffect類的詳細(xì)解釋。2017-11-11Android ToolBar 修改邊距的實(shí)現(xiàn)方法
這篇文章主要介紹了Android ToolBar 修改邊距的實(shí)現(xiàn)方法的相關(guān)資料,通過(guò)此文希望能幫助到大家,需要的朋友可以參考下2017-08-08Flutter開(kāi)發(fā)之路由與導(dǎo)航的實(shí)現(xiàn)
這篇文章主要介紹了Flutter開(kāi)發(fā)之路由與導(dǎo)航的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Android仿網(wǎng)易嚴(yán)選底部彈出菜單效果
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易嚴(yán)選底部彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android中實(shí)現(xiàn)iOS中的毛玻璃效果
為了實(shí)現(xiàn)毛玻璃效果,我們需要一組compute kernels(.rs文件中編寫(xiě)),及一組用于控制renderScript相關(guān)的Javaapi(.rs文件自動(dòng)生成為Java類)。 這篇文章主要介紹了Android中實(shí)現(xiàn)iOS中的毛玻璃效果,需要的朋友可以參考下2017-06-06android編程實(shí)現(xiàn)的自定義注釋模板實(shí)例
這篇文章主要介紹了android編程實(shí)現(xiàn)的自定義注釋模板,以完整實(shí)例形式分析了Android自定義魔板的定義及具體實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2015-11-11android基于socket的局域網(wǎng)內(nèi)服務(wù)器與客戶端加密通信
本篇文章主要介紹了android基于socket的局域網(wǎng)內(nèi)服務(wù)器與客戶端加密通信,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-04-04