【Android 控件 RoundedBitmapDrawable】
介紹
RoundedBitmapDrawable
是 supportV4
下的一個類,有了它,顯示圓角和圓形圖片的情況下就不需要額外的第三方類庫了,還能和各種圖片加載庫配合使用。
點擊此處 可以看到官方的介紹。
重要方法
return | method |
---|---|
void | setCircular(boolean circular) : Sets the image shape to circular. |
void | setCornerRadius(float cornerRadius) : Sets the corner radius to be applied when drawing the bitmap. |
setCircular(boolean circular)
: 把圖片的形狀設為圓形;
setCornerRadius(float cornerRadius)
: 設置圖片的圓角半徑。
源碼
這里貼一下源碼,更能清晰的知道它的實現:
/**
* Sets the image shape to circular.
* <p>This overwrites any calls made to {@link #setCornerRadius(float)} so far.</p>
*/
public void setCircular(boolean circular) {
mIsCircular = circular;
mApplyGravity = true;
if (circular) {
updateCircularCornerRadius();
mPaint.setShader(mBitmapShader);
invalidateSelf();
} else {
setCornerRadius(0);
}
}
private void updateCircularCornerRadius() {
final int minCircularSize = Math.min(mBitmapHeight, mBitmapWidth);
mCornerRadius = minCircularSize / 2;
}
/**
* Sets the corner radius to be applied when drawing the bitmap.
*/
public void setCornerRadius(float cornerRadius) {
if (mCornerRadius == cornerRadius) return;
mIsCircular = false;
if (isGreaterThanZero(cornerRadius)) {
mPaint.setShader(mBitmapShader);
} else {
mPaint.setShader(null);
}
mCornerRadius = cornerRadius;
invalidateSelf();
}
至于具體的實現,閱讀源碼發現官方使用了 BitmapShader
來實現的圓角。
使用
效果
首先來看下原圖和處理后效果,以及做一些擴展,如添加一個邊框
創建一個簡單的圓角圖片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(100);
iv.setImageDrawable(roundedBitmapDrawable );
通過RoundedBitmapDrawableFactory
傳遞要轉換bitmap 我就可以很簡單的生成一個如下圖的圓角圖片
創建一個正圓形圖片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCircular(true);
可以看到我們僅僅只是改了一個屬性就實現了如下圖正圓形的轉換,但你可能已經發現圖像有一些變形,因為內部在接收到circular == true
后先是對圖像進行了轉換為正方形的操作,這個操作是一個伸縮放操作,而不是裁剪,所以圖像發生了變形,所以在使用setCircular
時最好能保證你的原圖時一個正方形的,如果不是的話,下面我們也會給出相應解決方案
創建一個正圓形帶有邊框的圖片
private Drawable createRoundImageWithBorder(Bitmap bitmap){
//原圖寬度
int bitmapWidth = bitmap.getWidth();
//原圖高度
int bitmapHeight = bitmap.getHeight();
//邊框寬度 pixel
int borderWidthHalf = 20;
//轉換為正方形后的寬高
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 來進行一個中心裁剪
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;
}
我們自己進行對bitmap的裁剪來轉換成正方形,就解決了上面提到的拉伸問題,再繪制邊框就實現了一個如下帶邊框的正圓形圖片
其他
RoundedBitmapDrawable 也可以直接設置轉換過程的
- setAlpha 透明度
- setAntiAlias 抗鋸齒
- setDither 防抖動
這些操作,來更好的工作
到這個里我們就可以把項目中的圓角圖片的控件更換一下,平時記得多留意一下系統提供的一些API,可能會幫我們節省不少時間。
引用:
★★★Android一些容易被忽略的類-RoundedBitmapDrawable
★★Android 必知必會-使用 supportV4 的 RoundedBitmapDrawable 實現圓角