android自訂View-------為什麼重寫onMeasure()以及怎麼重寫,androidonmeasure

來源:互聯網
上載者:User

android自訂View-------為什麼重寫onMeasure()以及怎麼重寫,androidonmeasure

這兩天在看關於android自訂群組件的知識,剛開始查閱了很多資料,依然覺得對onMeasure()方法的理解不夠透徹,後來大致知道onMeasure怎麼用了之後,又很好奇為什麼需要去實現onMeasure()這個方法。


這篇文章主要記錄兩個問題,1)自訂群組件時什麼情況下需要實現onMeasure()方法,2)怎麼實現onMeasure()方法


為什麼需要實現onMeasure()方法


我們看以下例子,我們自己定義一個View,很簡單,只是繼承View類。

         

public class MyView extends View {public MyView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);// TODO Auto-generated constructor stub}public MyView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public MyView(Context context) {super(context);// TODO Auto-generated constructor stub}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);}}

然後在布局檔案中定義如下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:MyView="http://schemas.android.com/apk/res/com.notes.notes_onmeasure"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="@dimen/activity_vertical_margin"    tools:context="com.notes.notes_onmeasure.EasyActivity" >            <com.notes.notes_onmeasure.MyView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#78787878" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="能看到我嗎?" /></LinearLayout>


結果顯示如


當MyView的寬和高設定為match_parent時,MyView會填充整個介面是毋庸置疑的,當將其寬和高設定為某個確定值時,MyView的大小也會按這個確定的值顯示。但是上面的例子中,我們把MyView的寬和高設定為wrap_content,它依然填充整個介面。問題就出在這裡,所以我們需要重寫onMeasure()方法控制其大小。其它繼承於View的組件,如Button,TextView,它們都已經通過重寫onMeasuer()方法。


    怎麼重寫onMeasure()方法

    

android中,每個View的大小,不僅取決於自身,而且也受父視圖的影響,可以參考我的另一篇文章 Android介面繪製流程----How Android Draws Views。而onMeasure()是在父視圖計運算元視圖大小時被調用的,其中的細節就不在此詳細講述了,我們暫時只需要這一點即可。

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {       //,,,    }

onMeasure()方法有兩個參數,這兩個參數就是父視圖發過來給子視圖的限制條件。我們直接來重寫上面的MyView中的onMeasure()方法,通過一個例子瞭解onMeasure()方法怎麼實現。


@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = measureDimension(200, widthMeasureSpec);int height = measureDimension(200, heightMeasureSpec);setMeasuredDimension(width, height);}public int measureDimension(int defaultSize, int measureSpec){int result;int specMode = MeasureSpec.getMode(measureSpec);int specSize = MeasureSpec.getSize(measureSpec);if(specMode == MeasureSpec.EXACTLY){result = specSize;}else{result = defaultSize;   //UNSPECIFIEDif(specMode == MeasureSpec.AT_MOST){result = Math.min(result, specSize);}}return result;}

重寫onMeasure()方法時,必須最後調用setMeasureDimension()方法,否則會報錯。然後我們最終setMeasureDimension()的width和height是怎麼計算得到的呢?


首先我們解析widthMeasureSpec獲得兩個資料,一個是父視圖希望子視圖的大小是多少,一個是關於大小的模式,這兩個資料分別通過以下兩句代碼獲得


      int specMode = MeasureSpec.getMode(measureSpec);      int specSize = MeasureSpec.getSize(measureSpec);

MeasureSpec.getMode()方法返回的結果有三種:

  • UNSPECIFIED:父結點對子結點的大小沒有任何要求。
  • EXACTLY:  父結點要求其子節點的大小指定為某個確切的值。其子節點以及其他子孫結點都需要適應該大小。   
  • AT MOST:父結點要求其子節點的大小不能超過某個最大值,其子節點以及其他子孫結點的大小都需要小於這個值


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.