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

Android 自定義控件基本原理詳解(一):從 Measure、Layout 到 Draw

編輯:關於 Android 程式設計

前言:在日常的 Android 開發中,我們會頻繁地和各種控件打交道。然而,Android 原生提供的控件未必都能滿足業務需求,這時候就得靠我們自己動手實作自定義控件了。今天先來大致了解自定義控件的要求與實作的基本原理。

自定義控件的要求:

     1. 應當遵守 Android 標準規範(命名、可配置、事件處理等)。
     2. 能在 XML 配置中配置控件屬性。
     3. 對交互要有合適的反饋,比如按下、點擊等。
     4. 具備兼容性,Android 版本繁多,應有廣泛的適用性。

自定義控件的學習步驟:

1. View 的工作原理
2. 編寫 View 類
3. 為 View 類增加屬性
4. 繪製畫面
5. 響應用戶訊息
6. 自定義回調函式

自定義控件的兩種方式:

1. 繼承 ViewGroup
例如:ViewGroup、LinearLayout、FrameLayout、RelativeLayout 等。

2. 繼承 View
例如:View、TextView、ImageView、Button 等。

自定義控件的基本繪製原理:

View 的繪製基本上由 measure()、layout()、draw() 這三個函式完成。

1.)測量——Measure 過程用來計算視圖大小,相關方法主要有三個:

public final void measure(int widthMeasureSpec, int heightMeasureSpec)
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight)
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

measure 會呼叫 onMeasure,onMeasure 測量寬高後再呼叫 setMeasureDimension 保存測量結果。measure 與 setMeasureDimension 是 final 類型,View 的子類不需重寫;onMeasure 則在子類中重寫。

關於 MeasureSpec:

(1) UNSPECIFIED:父容器對子容器沒有任何限制,子容器想多大就多大。
(2) EXACTLY:父容器已為子容器設定了尺寸,子容器應當服從這些邊界,不論它想要多大空間。
(3) AT_MOST:子容器可以是聲明大小以內的任意大小。

2.)布局——Layout 過程用來設定視圖在畫面中的顯示位置,相關方法主要有三個:

public void layout(int l, int t, int r, int b)
protected boolean setFrame(int left, int top, int right, int bottom)
protected void onLayout(boolean changed, int left, int top, int right, int bottom)

layout 會呼叫 setFrame(l, t, r, b),其中 l、t、r、b 就是子視圖在父視圖中的具體位置;onLayout 一般只有在自定義 ViewGroup 時才會用到。

3.)繪製——draw 過程主要利用前兩步得到的參數,將視圖呈現在畫面上,至此整個視圖繪製流程就完成了。

public void draw(Canvas canvas)
protected void onDraw(Canvas canvas)

透過呼叫 draw 函式進行視圖繪製,而在 View 類中 onDraw 是個空函式,最終的繪製需求需要在自定義的 onDraw 中實作,比如 ImageView 就是在這裡完成圖片繪製;如果是自定義 ViewGroup,這個函式則不需要重載。

自定義控件範例:

這裡先以繼承 View 的方式為例(其實 ViewGroup 最終也是繼承自 View)。模擬一個需求場景:需要一個圓形顯示百分比。

 public class PercentView extends View { private final static String TAG = PercentView.class.getSimpleName(); private Paint mPaint; public PercentView(Context context) { super(context); init(); } public PercentView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public PercentView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init(){ mPaint = new Paint(); mPaint.setAntiAlias(true); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); Log.e(TAG, "onMeasure--widthMode-->" + widthMode); switch (widthMode) { case MeasureSpec.EXACTLY: break; case MeasureSpec.AT_MOST: break; case MeasureSpec.UNSPECIFIED: break; } Log.e(TAG, "onMeasure--widthSize-->" + widthSize); Log.e(TAG, "onMeasure--heightMode-->" + heightMode); Log.e(TAG, "onMeasure--heightSize-->" + heightSize); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); Log.e(TAG, "onLayout"); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(Color.GRAY); // FILL填充, STROKE描邊,FILL_AND_STROKE填充和描邊 mPaint.setStyle(Paint.Style.FILL_AND_STROKE); int with = getWidth(); int height = getHeight(); Log.e(TAG, "onDraw---->" + with + "*" + height); float radius = with / 4; canvas.drawCircle(with / 2, with / 2, radius, mPaint); mPaint.setColor(Color.BLUE); RectF oval = new RectF(with / 2 - radius, with / 2 - radius, with / 2 + radius, with / 2 + radius); //用於定義的圓弧的形狀和大小的界限 canvas.drawArc(oval, 270, 120, true, mPaint); //根據進度畫圓弧 } }

在配置中如何使用

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.whoislcj.views.PercentView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" /> </LinearLayout> 

顯示效果:

如果把配置檔改成

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.whoislcj.views.PercentView android:layout_width="150dp" android:layout_height="150dp" android:layout_margin="10dp" /> </LinearLayout> 

顯示效果就會變成

總結:本篇主要介紹 Android 自定義控件的基本繪製原理,下一篇將說明如何自定義屬性,敬請期待。

以上就是本文的全部內容,希望對大家的學習有所幫助,也歡迎大家多多支持本站。

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