Android onMeasure方法介紹

來源:互聯網
上載者:User

標籤:android   java   使用   os   io   strong   資料   ar   

onMeasure方法在控制項的父元素正要放置它的子控制項時調用.它會問一個問題,“你想要用多大地方啊?”,然後傳入兩個參數——widthMeasureSpec和heightMeasureSpec.

  它們指明控制項可獲得的空間以及關於這個空間描述的中繼資料.
  比返回一個結果要好的方法是你傳遞View的高度和寬度到setMeasuredDimension方法裡.
  
  接下來的程式碼片段給出了如何重寫onMeasure.注意,調用的本地空方法是來計算高度和寬度的.它們會譯解widthHeightSpec和heightMeasureSpec值,並計算出合適的高度和寬度值.

java代碼:

  1. @Override
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  3. int measuredHeight = measureHeight(heightMeasureSpec);
  4. int measuredWidth = measureWidth(widthMeasureSpec);
  5. setMeasuredDimension(measuredHeight, measuredWidth);
  6. }
  7. private int measureHeight(int measureSpec) {
  8. // Return measured widget height.
  9. }
  10. private int measureWidth(int measureSpec) {
  11. // Return measured widget width.
  12. }

       邊界參數——widthMeasureSpec和heightMeasureSpec ,效率的原因以整數的方式傳入。

       MeasureSpec封裝了父布局傳遞給子布局的布局要求,每個MeasureSpec代表了一組寬度和高度的要求。一個MeasureSpec由大小和模式組成。

    它有三種模式:

           UNSPECIFIED(未指定),     父元素不對自元素施加任何束縛,子項目可以得到任意想要的大小;

           EXACTLY(完全),父元素決定自元素的確切大小,子項目將被限定在給定的邊界裡而忽略它本身大小;

           AT_MOST(至多),子項目至多達到指定大小的值。

   它常用的三個函數:

  1.static int getMode(int measureSpec):根據提供的測量值(格式)提模數式(上述三個模式之一)

  2.static int getSize(int measureSpec):根據提供的測量值(格式)提取大小值(這個大小也就是我們通常所說的大小)

  3.static int makeMeasureSpec(int size,int mode):根據提供的大小值和模式建立一個測量值(格式)

     這個類的使用呢,通常在view組件的onMeasure方法裡面調用但也有少數例外

      在它們使用之前,首先要做的是使用MeasureSpec類的靜態方法getMode和getSize來譯解,如下面的片段所示:

java代碼:

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

       依據specMode的值,如果是AT_MOST,specSize 代表的是最大可獲得的空間;如果是EXACTLY,specSize 代表的是精確的尺寸;如果是UNSPECIFIED,對於控制項尺寸來說,沒有任何參考意義。
  當以EXACT方式標記測量尺寸,父元素會堅持在一個指定的精確尺寸地區放置View。在父元素問子項目要多大空間時,AT_MOST指示者會說給我最大的範圍。在很多情況下,你得到的值都是相同的。
  在兩種情況下,你必須絕對的處理這些限制。在一些情況下,它可能會返回超出這些限制的尺寸,在這種情況下,你可以讓父元素選擇如何對待超出的View,使用裁剪還是滾動等技術。

  接下來的架構代碼給出了處理View測量的典型實現:

java代碼:

  1. @Override
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  3. int measuredHeight = measureHeight(heightMeasureSpec);
  4. int measuredWidth = measureWidth(widthMeasureSpec);
  5. setMeasuredDimension(measuredHeight, measuredWidth);
  6. }
  7. private int measureHeight(int measureSpec) {
  8. int specMode = MeasureSpec.getMode(measureSpec);
  9. int specSize = MeasureSpec.getSize(measureSpec);
  10. // Default size if no limits are specified.
  11. int result = 500;
  12. if (specMode == MeasureSpec.AT_MOST){
  13. // Calculate the ideal size of your
  14. // control within this maximum size.
  15. // If your control fills the available
  16. // space return the outer bound.
  17. result = specSize;
  18. }
  19. else if (specMode == MeasureSpec.EXACTLY){
  20. // If your control can fit within these bounds return that value.
  21. result = specSize;
  22. }
  23. return result;
  24. }
  25. private int measureWidth(int measureSpec) {
  26. int specMode = MeasureSpec.getMode(measureSpec);
  27. int specSize = MeasureSpec.getSize(measureSpec);
  28. // Default size if no limits are specified.
  29. int result = 500;
  30. if (specMode == MeasureSpec.AT_MOST){
  31. // Calculate the ideal size of your control
  32. // within this maximum size.
  33. // If your control fills the available space
  34. // return the outer bound.
  35. result = specSize;
  36. }
  37. else if (specMode == MeasureSpec.EXACTLY){
  38. // If your control can fit within these bounds return that value.
  39. result = specSize;
  40. }
  41. return result;
  42. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.