RoundedBitmapDrawable 實(shí)現(xiàn)Bitmap圓角

創(chuàng)建一個(gè)正圓形帶有邊框的圖片
private Drawable createRoundImageWithBorder(Bitmap bitmap){
//原圖寬度
int bitmapWidth = bitmap.getWidth();
//原圖高度
int bitmapHeight = bitmap.getHeight();
//邊框?qū)挾?pixel
int borderWidthHalf = 20;

//轉(zhuǎn)換為正方形后的寬高
int bitmapSquareWidth = Math.min(bitmapWidth,bitmapHeight);

//最終圖像的寬高
int newBitmapSquareWidth = bitmapSquareWidth+borderWidthHalf;

Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquareWidth,newBitmapSquareWidth,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(roundedBitmap);
int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;
int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;

//裁剪后圖像,注意X,Y要除以2 來(lái)進(jìn)行一個(gè)中心裁剪
canvas.drawBitmap(bitmap, x/2, y/2, null);
Paint borderPaint = new Paint();
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidthHalf);
borderPaint.setColor(Color.WHITE);

//添加邊框
canvas.drawCircle(canvas.getWidth()/2, canvas.getWidth()/2, newBitmapSquareWidth/2, borderPaint);

RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),roundedBitmap);
roundedBitmapDrawable.setGravity(Gravity.CENTER);
roundedBitmapDrawable.setCircular(true);
return roundedBitmapDrawable;

}

作者:李濤丶
鏈接:http://www.itdecent.cn/p/ed42d8c90a45
來(lái)源:簡(jiǎn)書
簡(jiǎn)書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容