創(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)并注明出處。