一.自定義View繪制流程
Paste_Image.png
二.自定義View分類
自定義ViewGroup
自定義ViewGroup一般是利用現(xiàn)有的組件根據(jù)特定的布局方式來組成新的組件欠啤,大多繼承自ViewGroup或各種Layout。自定義View
在沒有現(xiàn)成的View赎线,需要自己實現(xiàn)的時候邪乍,就使用自定義View,一般繼承自View,SurfaceView或其他的View。
三.幾個重要的函數(shù)
構(gòu)造函數(shù)
構(gòu)造函數(shù)是View的入口春寿,可以用于初始化一些的內(nèi)容朗涩,和獲取自定義屬性忽孽。
View的構(gòu)造函數(shù)有四種重載分別如下:
public CustomView(Context context)
{
super(context);
}
public CustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
}
有四個參數(shù)的構(gòu)造函數(shù)在API21的時候才添加上,暫不考慮谢床。
有三個參數(shù)的構(gòu)造函數(shù)中第三個參數(shù)是默認的Style兄一,這里的默認的Style是指它在當前Application或Activity所用的Theme中的默認Style,且只有在明確調(diào)用的時候才會生效识腿,以系統(tǒng)中的ImageButton為例說明:
public ImageButton(Context context, AttributeSet attrs) {
//調(diào)用了三個參數(shù)的構(gòu)造函數(shù)出革,明確指定第三個參數(shù)
this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
}
public ImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
//此處調(diào)了四個參數(shù)的構(gòu)造函數(shù),無視即可
this(context, attrs, defStyleAttr, 0);
}
注意:即使你在View中使用了Style這個屬性也不會調(diào)用三個參數(shù)的構(gòu)造函數(shù)渡讼,所調(diào)用的依舊是兩個參數(shù)的構(gòu)造函數(shù)骂束。
由于三個參數(shù)的構(gòu)造函數(shù)第三個參數(shù)一般不用耳璧,暫不考慮,第三個參數(shù)的具體用法會在以后用到的時候詳細介紹展箱。
排除了兩個之后旨枯,只剩下一個參數(shù)和兩個參數(shù)的構(gòu)造函數(shù),他們的詳情如下:
//一般在直接New一個View的時候調(diào)用混驰。
public void CustomView(Context context) {}
//一般在layout文件中使用的時候會調(diào)用攀隔,關(guān)于它的所有屬性(包括自定義屬性)都會包含在attrs中傳遞進來。
public void CustomView(Context context, AttributeSet attrs) {}
以下方法調(diào)用的是一個參數(shù)的構(gòu)造函數(shù):
//在Activity中
CustomView customView = new CustomView(this);
```
以下方法調(diào)用的是兩個參數(shù)的構(gòu)造函數(shù):
```
<com.zhoujian.viewevent.view.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
```
###測量View大小(onMeasure)
為什么要測量View大衅苷ァ昆汹?
View的大小不僅由自身所決定,同時也會受到父控件的影響婴栽,為了我們的控件能更好的適應(yīng)各種情況满粗,一般會自己進行測量。
測量View大小使用的是onMeasure函數(shù)愚争,我們可以從onMeasure的兩個參數(shù)中取出寬高的相關(guān)數(shù)據(jù):
```
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthsize MeasureSpec.getSize(widthMeasureSpec); //取出寬度的確切數(shù)值
int widthmode MeasureSpec.getMode(widthMeasureSpec); //取出寬度的測量模式
int heightsize MeasureSpec.getSize(heightMeasureSpec); //取出高度的確切數(shù)值
int heightmode MeasureSpec.getMode(heightMeasureSpec); //取出高度的測量模式
}
```
從上面可以看出 onMeasure 函數(shù)中有 widthMeasureSpec 和 heightMeasureSpec 這兩個 int 類型的參數(shù)败潦, 毫無疑問他們是和寬高相關(guān)的, 但它們其實不是寬和高准脂, 而是由寬劫扒、高和各自方向上對應(yīng)的測量模式來合成的一個值:
測量模式一共有三種, 被定義在 Android 中的 View 類的一個內(nèi)部類View.MeasureSpec中:
| 模式 | 描述 |
|:-------------:|:-----:|
| UNSPECIFIED | 默認值狸膏,父控件沒有給子view任何限制沟饥,子View可以設(shè)置為任意大小 |
| EXACTLY | 表示父控件已經(jīng)確切的指定了子View的大小|
| AT_MOST | 表示子View具體大小沒有尺寸限制,但是存在上限湾戳,上限一般為父View大小 |
注意: 用 MeasureSpec 的 getSize是獲取數(shù)值贤旷, getMode是獲取模式。如果對View的寬高進行修改了砾脑,不要調(diào)用 super.onMeasure( widthMeasureSpec, heightMeasureSpec); 要調(diào)用 setMeasuredDimension( widthsize, heightsize); 這個函數(shù)幼驶。
###確定View大小(onSizeChanged)
這個函數(shù)在視圖大小發(fā)生改變時調(diào)用。
在測量完View并使用setMeasuredDimension函數(shù)之后View的大小基本上已經(jīng)確定了韧衣,那么為什么還要再次確定View的大小呢盅藻?
這是因為View的大小不僅由View本身控制,而且受父控件的影響畅铭,所以我們在確定View大小的時候最好使用系統(tǒng)提供的onSizeChanged回調(diào)函數(shù)氏淑。
```
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
```
可以看出,它又四個參數(shù)硕噩,分別為 寬度假残,高度,上一次寬度炉擅,上一次高度辉懒。
這個函數(shù)比較簡單阳惹,我們只需關(guān)注 寬度(w), 高度(h) 即可,這兩個參數(shù)就是View最終的大小眶俩。
###確定子View布局位置(onLayout)
確定布局的函數(shù)是onLayout穆端,它用于確定子View的位置,在自定義ViewGroup中會用到仿便,他調(diào)用的是子View的layout函數(shù)体啰。
在自定義ViewGroup中,onLayout一般是循環(huán)取出子View嗽仪,然后經(jīng)過計算得出各個子View位置的坐標值荒勇,然后用以下函數(shù)設(shè)置子View位置。
```
child.layout(l, t, r, b);
```
四個參數(shù)分別為:
| 名稱 | 說明 | 對應(yīng)的函數(shù) |
| :-------------: |:-------------:| :-----:|
|l | View左側(cè)距父View左側(cè)的距離 | getLeft(); |
|t | View頂部距父View頂部的距離 | getTop(); |
| r | View右側(cè)距父View左側(cè)的距離 | getRight(); |
| b | View底部距父View頂部的距離 | getBottom();|
###繪制內(nèi)容(onDraw)
onDraw是實際繪制的部分闻坚,也就是我們真正關(guān)心的部分沽翔,使用的是Canvas繪圖。
```
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
```
###完整的代碼
```
package com.zhoujian.viewevent.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by zhoujian on 2017/1/19.
*/
public class CustomView extends View {
public CustomView(Context context)
{
super(context);
}
/**
* 初始化一些的內(nèi)容窿凤,和獲取自定義屬性仅偎。
* @param context
* @param attrs
*/
public CustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
/**
* 測量view大小
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int widthsize = MeasureSpec.getSize(widthMeasureSpec); //取出寬度的確切數(shù)值
int widthmode = MeasureSpec.getMode(widthMeasureSpec); //取出寬度的測量模式
int heightsize = MeasureSpec.getSize(heightMeasureSpec); //取出高度的確切數(shù)值
int heightmode = MeasureSpec.getMode(heightMeasureSpec); //取出高度的測量模式
setMeasuredDimension(widthsize,heightsize);
}
/**
* 確定view大小
* @param w
* @param h
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
}
/**
* 確定子View布局位置
* @param changed
* @param left
* @param top
* @param right
* @param bottom
*/
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
}
/**
* 繪制內(nèi)容
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.RED); //繪制藍色
}
}
```
###對外提供操作方法和監(jiān)聽回調(diào)
自定義完View之后,一般會對外暴露一些接口雳殊,用于控制View的狀態(tài)等橘沥,或者監(jiān)聽View的變化.
###參考文章
[@GcsSloop:安卓自定義View進階-分類與流程]{http://www.gcssloop.com/customview/CustomViewProcess}