Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自定義圓角 ImageView:弧度可配置的簡潔實現

編輯:關於 Android 程式設計

這是一個代碼量不大的圓角 ImageView 實現,特點是弧度可通過自定義屬性配置,也能在代碼裡動態設置。

 public class RoundImageView extends ImageView { private Paint paint; private int roundWidth = 50; private int roundHeight = 50; private Paint paint2; public RoundImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs); } public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public void SetRoundValue(float roundValue) { roundWidth = (int) roundValue; roundHeight = (int) roundValue; } public RoundImageView(Context context) { super(context); init(context, null); } @SuppressLint("Recycle") private void init(Context context, AttributeSet attrs) { if (attrs != null) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView); roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth); roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight); } else { float density = context.getResources().getDisplayMetrics().density; roundWidth = (int) (roundWidth * density); roundHeight = (int) (roundHeight * density); } paint = new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); paint2 = new Paint(); paint2.setXfermode(null); } @Override public void draw(Canvas canvas) { Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); Canvas canvas2 = new Canvas(bitmap); super.draw(canvas2); drawLiftUp(canvas2); drawRightUp(canvas2); drawLiftDown(canvas2); drawRightDown(canvas2); canvas.drawBitmap(bitmap, 0, 0, paint2); bitmap.recycle(); bitmap = null; } private void drawLiftUp(Canvas canvas) { Path path = new Path(); path.moveTo(0, roundHeight); path.lineTo(0, 0); path.lineTo(roundWidth, 0); path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90); path.close(); canvas.drawPath(path, paint); } private void drawLiftDown(Canvas canvas) { Path path = new Path(); path.moveTo(0, getHeight() - roundHeight); path.lineTo(0, getHeight()); path.lineTo(roundWidth, getHeight()); path.arcTo(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getWidth()), 90, 90); path.close(); canvas.drawPath(path, paint); } private void drawRightDown(Canvas canvas) { Path path = new Path(); path.moveTo(getWidth() - roundWidth, getHeight()); path.lineTo(getWidth(), getHeight()); path.lineTo(getWidth(), getHeight() - roundHeight); path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0, 90); path.close(); canvas.drawPath(path, paint); } private void drawRightUp(Canvas canvas) { Path path = new Path(); path.moveTo(getWidth(), roundHeight); path.lineTo(getWidth(), 0); path.lineTo(getWidth() - roundWidth, 0); path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90); path.close(); canvas.drawPath(path, paint); } }

實現思路

整體是「先畫原圖,再把四個角擦掉」的路子。

draw 方法中先創建一張與控件等大的空 Bitmap 並用它初始化一個離屏 Canvas,調用 super.draw(canvas2) 讓父類把圖片正常畫上去,接著分別處理左上、右上、左下、右下四個角,最後把成品一次性貼回傳入的 canvas。

擦除靠的是 paint 上的 PorterDuffXfermode(PorterDuff.Mode.DST_OUT)。DST_OUT 的含義是「保留目標中與來源重疊以外的部分」,因此畫上去的白色 Path 會把對應區域抹成透明,圓角就出來了。paint2 則是普通畫筆,用來把結果原樣貼回。

四個角的 Path 都由「兩段直線加一段 arcTo」構成,arcTo 的第一組參數是包圍橢圓的矩形,後兩個是起止角度。理解了這一個,其餘三個只是鏡像與平移的區別。

使用說明

在 attrs.xml 中聲明 RoundAngleImageView 的 roundWidth 與 roundHeight 兩個屬性,布局裡即可寫:

 <com.xxx.RoundImageView android:layout_width="100dp" android:layout_height="100dp" app:roundWidth="20dp" app:roundHeight="20dp" />

代碼中也可以調用 setRoundValue(20) 動態調整。

一點性能提醒

這個實現每次 draw 都會新建一張 Bitmap,在列表等頻繁重繪的場景下容易引發頻繁 GC 甚至內存抖動。如果要用在 ListView 或 RecyclerView 裡,建議改成在 onSizeChanged 時創建一次緩存 Bitmap、後續複用,或者直接改用 BitmapShader + drawRoundRect 的方案(只畫一次,無需離屏緩存)。另外,位圖用完記得 recycle,示例中已經處理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved