溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android開發(fā)實踐:為什么要繼承onMeasure()

發(fā)布時間:2020-07-28 09:52:16 來源:網(wǎng)絡(luò) 閱讀:11011 作者:Jhuster 欄目:移動開發(fā)

Android開發(fā)中偶爾會用到自定義View,一般情況下,自定義View都需要繼承View類的onMeasure方法,那么,為什么要繼承onMeasure()函數(shù)呢?什么情況下要繼承onMeasure()?系統(tǒng)默認的onMeasure()函數(shù)行為是怎樣的 ?本文就探究探究這些問題。


首先,我們寫一個自定義View,直接調(diào)用系統(tǒng)默認的onMeasure函數(shù),看看會是怎樣的現(xiàn)象:


package com.titcktick.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {
    
    public CustomView(Context context) {
        super(context); 
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);      
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}


1. 父控件使用match_parent,CustomView使用match_parent


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.titcktick.customview.CustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:background="@android:color/black"/>

</LinearLayout>


這里加了10dp的margin并且把View的背景設(shè)置為了黑色,是為了方便辨別我們的CustomView,效果如下:


Android開發(fā)實踐:為什么要繼承onMeasure()


我們可以看到,默認情況下,如果父控件和CustomView都使用match_parent,則CustomView會充滿父控件。


2.  父控件使用match_parent,CustomView使用wrap_content


把layout文件中,CustomView的layout_width/layout_height替換為wrap_content,你會發(fā)現(xiàn),結(jié)果依然是充滿父控件。


3.  父控件使用match_parent,CustomView使用固定的值


把layout文件中,CustomView的layout_width/layout_height替換為50dp,你會發(fā)現(xiàn),CustomView的顯示結(jié)果為50dpx50dp,如圖所示:


Android開發(fā)實踐:為什么要繼承onMeasure()


4.  父控件使用固定的值,CustomView使用match_parent或者wrap_content


那么,如果把父控件的layout_width/layout_height替換為50dp,CustomView設(shè)置為match_parent或者wrap_content,你會發(fā)現(xiàn),CustomView的顯示結(jié)果也是為50dpx50 dp。


5  結(jié)論


如果自定義的CustomView采用默認的onMeasure函數(shù),行為如下:


(1) CustomView設(shè)置為 match_parent 或者 wrap_content 沒有任何區(qū)別,其顯示大小由父控件決定,它會填充滿整個父控件的空間。


(2) CustomView設(shè)置為固定的值,則其顯示大小為該設(shè)定的值。


如果你的自定義控件的大小計算就是跟系統(tǒng)默認的行為一致的話,那么你就不需要重寫onMeasure函數(shù)了。


6. 怎樣編寫onMeasure函數(shù)


系統(tǒng)默認的onMeasure函數(shù)的行為就討論到這,下面也說說怎樣重寫onMeasure函數(shù),以及onMeasure函數(shù)的基本原理,關(guān)鍵部分在代碼中以注釋的形式給出了,僅供參考:


package com.titcktick.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {
    
    private static final int DEFAULT_VIEW_WIDTH = 100;
    private static final int DEFAULT_VIEW_HEIGHT = 100;
    
    public CustomView(Context context) {
        super(context); 
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);      
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        
        int width  = measureDimension(DEFAULT_VIEW_WIDTH, widthMeasureSpec);
        int height = measureDimension(DEFAULT_VIEW_HEIGHT, heightMeasureSpec);
        
        setMeasuredDimension(width, height);                
    }
    
    protected int measureDimension( int defaultSize, int measureSpec ) {
        
        int result = defaultSize;
        
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
                
        //1. layout給出了確定的值,比如:100dp
        //2. layout使用的是match_parent,但父控件的size已經(jīng)可以確定了,比如設(shè)置的是具體的值或者match_parent
        if (specMode == MeasureSpec.EXACTLY) {      
            result = specSize; //建議:result直接使用確定值
        } 
        //1. layout使用的是wrap_content
        //2. layout使用的是match_parent,但父控件使用的是確定的值或者wrap_content
        else if (specMode == MeasureSpec.AT_MOST) {             
            result = Math.min(defaultSize, specSize); //建議:result不能大于specSize
        } 
        //UNSPECIFIED,沒有任何限制,所以可以設(shè)置任何大小
        //多半出現(xiàn)在自定義的父控件的情況下,期望由自控件自行決定大小
        else {      
            result = defaultSize; 
        }
        
        return result;
    }
}


這樣重載了onMeasure函數(shù)之后,你會發(fā)現(xiàn),當CustomView使用match_parent的時候,它會占滿整個父控件,而當CustomView使用wrap_content的時候,它的大小則是代碼中定義的默認大小100x100像素。當然,你也可以根據(jù)自己的需求改寫measureDimension()的實現(xiàn)。


關(guān)于onMeasure的討論就介紹到這兒了,有任何疑問歡迎留言或者來信lujun.hust@gmail.com交流,或者關(guān)注我的新浪微博 @盧_俊 獲取最新的文章和資訊。


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI