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

打造一個規範的自定義 View——Android 開發藝術探索筆記

編輯:關於 Android 開發

從零打造一個好習慣養成的自定義 View

先看一個不規範的自定義 View

這個自定義 View 的目標很簡單,就是畫出一個圓形。我們先來看看一個不規範的版本是怎麼寫的。

 public class CircleView extends View { private int mColor = Color.RED; private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); public CircleView(Context context) { super(context); init(); } public CircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint.setColor(mColor); } @Override protected void onDraw(Canvas canvas) { int width = getWidth(); int height = getHeight(); int radius = Math.min(width, height) / 2; canvas.drawCircle(width / 2, height / 2, radius, mPaint); } }

對應的 XML:

 <com.ryg.chapter_4.ui.circleview android:background="#000000" android:id="@+id/circleView1" android:layout_height="100dp" android:layout_margin="20dp" android:layout_width="match_parent"></com.ryg.chapter_4.ui.circleview>

這樣寫雖然也能畫出一個圓,但它並不是一個規範的自定義 View,主要存在兩個問題:

一是 android:padding 屬性無法生效;二是使用 wrap_content 時會等同於 match_parent。

再來看一個規範的自定義 View

要解決上述問題,需要重寫 View 的 onMeasure 與 onDraw 方法。完整程式碼如下:

 public class CircleView extends View { private int mColor = Color.RED; private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); public CircleView(Context context) { super(context); init(); } public CircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleView); mColor = a.getColor(R.styleable.CircleView_circle_color, Color.RED); a.recycle(); init(); } private void init() { mPaint.setColor(mColor); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(200, 200); } else if (widthSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(200, heightSpecSize); } else if (heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(widthSpecSize, 200); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); final int paddingTop = getPaddingTop(); final int paddingBottom = getPaddingBottom(); int width = getWidth() - paddingLeft - paddingRight; int height = getHeight() - paddingTop - paddingBottom; int radius = Math.min(width, height) / 2; canvas.drawCircle(paddingLeft + width / 2, paddingTop + height / 2, radius, mPaint); } }

新增自定義屬性

先在 values 資料夾下建立 attrs.xml:

 <resources> <declare-styleable name="CircleView"> </declare-styleable> </resources>

這裡定義了一個名為 CircleView 的屬性集合,並在其中宣告了一個格式為 color 的屬性 circle_color。

接著在 View 的建構子中解析這個自定義屬性:

 public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleView); mColor = a.getColor(R.styleable.CircleView_circle_color, Color.RED); a.recycle(); init(); }

在布局檔中使用自定義屬性:

 <com.ryg.chapter_4.ui.circleview android:background="#000000" android:id="@+id/circleView1" android:layout_height="100dp" android:layout_margin="20dp" android:layout_width="wrap_content" android:padding="20dp" app:circle_color="@color/light_green"></com.ryg.chapter_4.ui.circleview>

使用自定義屬性時,記得先在根布局宣告命名空間 xmlns:app="https://schemas.android.com/apk/res-auto",使用方式與一般屬性類似,寫成 app:circle_color="@color/light_green" 即可。

自定義 View 必知重點

  • 自定義 View 中的 margin 屬性是可以使用的,因為它是由父容器來控制的。
  • 直接繼承 View 或 ViewGroup 時,需要自己處理 wrap_content。
  • View 要在 onDraw 中處理 padding,而 ViewGroup 則要在 onMeasure 和 onLayout 中處理 padding 與 margin。
  • View 中的 post 方法可以取代 Handler。
  • 應在 onDetachedFromWindow 中停止動畫,避免記憶體洩漏。
  • 若存在滑動嵌套的情況,要特別注意滑動衝突的處理。

想打造一個漂亮又穩定的自定義 View 並不容易,唯有多看、多寫、多測試,才能真正掌握精髓。不妨自己動手打造一個輪子,再對比成熟的開源元件,找出差距與不足之處。

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