編輯:Android 開發實例
Android 的視圖控件大致分為兩類:ViewGroup 與 View。ViewGroup 充當父容器,負責包含並管理子 View,兩者交錯組合便形成一棵控件樹,每個 ViewGroup 與 View 都是樹上的節點。查找控件時依樹的深度遍歷;上層控件負責下層的測量、繪製與事件傳遞。
Android 控件樹:

Android UI 界面架構圖:

MeasureSpec.getMode() 與 MeasureSpec.getSize() 取得。View 類別預設的 onMeasure() 只處理 EXACTLY 模式。要支援其他模式就必須自行覆寫,模板如下:
package com.example.demoapp.views;
import android.content.Context;
import android.view.View;
public class MeasuredView extends View {
public MeasuredView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 交給父類處理,它最終會呼叫 setMeasuredDimension()
super.onMeasure(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
// 也可以直接呼叫:
// setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
/** 計算寬度 */
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 200;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
/** 計算高度 */
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 200;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
取得 Canvas 的兩種途徑
onDraw(Canvas canvas) 中的 canvas。Canvas canvas = new Canvas(bitmap)。傳入 Bitmap 的過程稱為「畫布的裝載」,該 Bitmap 承載所有繪製在 Canvas 上的像素;後續呼叫 drawBitmap() 等方法,實際作用對象就是這張 Bitmap。繪圖要點
Canvas.drawXXX() 的繪製會直接改寫 Bitmap,下次刷新 View 時看到的就是被改寫後的內容。layout(),把它們放到正確位置。onLayout() 來控制排列。onMeasure()。一般情況下 ViewGroup 自己不需要繪製,它會透過 dispatchDraw() 負責畫出子 View。
onDraw() 決定顯示內容;若要支援 wrap_content,還得覆寫 onMeasure()。幾個重要的回呼時機:
onFinishInflate():從 XML 載入完成後回呼。onSizeChanged():尺寸變動時回呼。onMeasure():進行測量。onLayout():決定顯示位置。onTouchEvent():處理觸控事件。常見的三種實作路線:
onDraw() 擴充原生控件。addView() 塞入各具功能的子控件,組成複合控件。onDraw() 與 onMeasure() 處理繪製,覆寫 onTouchEvent() 處理互動。在 res/values 下建立 attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="customAttr">
<attr name="title" format="string" />
<attr name="fontSize" format="dimension" />
<attr name="fontColor" format="color" />
<attr name="background" format="reference|color" />
<attr name="fontStyle" format="enum" />
<attr name="shadeSupport" format="boolean" />
</declare-styleable>
</resources>
再以 TypedArray 讀取:
public class TestCustomAttrs extends View {
private Context mContext;
private AttributeSet mAttrs;
private String mTitle;
private float mFontSize;
private int mFontColor;
private int mBackground;
private int mFontStyle;
private boolean mShadeSupport;
public TestCustomAttrs(Context context) {
super(context);
this.mContext = context;
}
public TestCustomAttrs(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
this.mAttrs = attrs;
}
public TestCustomAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
this.mAttrs = attrs;
}
private void getCustomAttrs() {
TypedArray ta = mContext.obtainStyledAttributes(mAttrs, R.styleable.customAttr);
mTitle = ta.getString(R.styleable.customAttr_title);
mFontSize = ta.getDimension(R.styleable.customAttr_fontSize, 10);
mFontColor = ta.getColor(R.styleable.customAttr_fontColor, 0);
mBackground = ta.getColor(R.styleable.customAttr_background, 0);
mFontStyle = ta.getInt(R.styleable.customAttr_fontStyle, 0);
mShadeSupport = ta.getBoolean(R.styleable.customAttr_shadeSupport, false);
ta.recycle();
}
}
另外可定義回呼介面,讓自訂控件對外提供靈活的控制方式。
在布局中引用:先宣告命名空間 xmlns:custom="https://schemas.android.com/apk/res-auto",之後就能用 custom: 前綴使用自訂屬性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:custom="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.jy.myrecyclerview.test.TestCustomAttrs
android:id="@+id/id_recyclerview"
android:divider="#ffff0000"
android:dividerHeight="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:title="title"
custom:fontSize="12sp"
custom:fontColor="@color/colorPrimary"
custom:background="@color/colorPrimary"
custom:shadeSupport="false" />
</RelativeLayout>
onMeasure():測量子 View。onLayout():安排子 View 位置。onTouchEvent():處理觸控互動。掌握控件樹的結構與這三個階段的職責,自訂控件的行為就完全在自己的掌控之中。
Android MediaPlayer(多媒體播放):音訊與影片控制入門
Android 提供了許多方法來控制播放的音訊/影片檔案與串流,其中主要的一種是透過名為 MediaPlayer 的類別。 MediaPlayer 類別讓 Android
Android 登入功能實作範例
登入畫面幾乎是每一款需要帳號的應用必備的環節,它透過詢問使用者的帳號與密碼來完成身分驗證,無論是登入 Facebook、微博還是自家服務都離不開它。 本章將說明如何打造一
Android ListView 常見用法實戰:從默認布局到多選與點擊響應
ListView 是列表展示的首選控件(在 RecyclerView 普及之前更是如此)。本文用一套由淺入深的示例,把它的三要素、默認布局、多選模式與事件響應串起來講。
Android 編程:自定義進度條顏色的完整實現
本文記錄了在 Android 中自定義 ProgressBar 顏色的完整過程,供大家參考。 先看效果圖: 由於產品經理的各種自定義需求,我查閱了不少資料,下面就把整個