MeasureSpec 解析,measurespec解析

來源:互聯網
上載者:User

MeasureSpec 解析,measurespec解析

MeasureSpec

1、A MeasureSpec is comprised of a size and a mode.

列印成二進位:

MODE_MASK=11000000000000000000000000000000, //0011左移動30位得到 

~MODE_MASK =  00111111111111111111111111111111; 

size & ~MODE_MASK  :  MeasureSpec 是個32位的int型,後三十位是是分配給size的。


 

2、mode有三種:

//0

// 01000000000000000000000000000000

 // 10000000000000000000000000000000

 

mode & MODE_MASK  :  & 相同取1,相異取0;所以  01&11 =01 ;10 &11=10,後三十位全部為0

                                  MeasureSpec 是個32位的int型,前2位是是分配給mode 的。

 

MeasureSpec objects are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

  • UNSPECIFIED:    The parent has not imposed any constraint on the child. It can be whatever size it wants. 無限制;,這種情況不多,一般都是父控制項是AdapterView,通過measure方法傳入的模式。
  • EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size. 限制尺寸;當我們將控制項的layout_width或layout_height指定為具體數值時如andorid:layout_width="50dip",或者為FILL_PARENT是,都是控制項大小已經確定的情況,都是精確尺寸。
 
  • AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size. 限制最大尺寸;,當控制項的layout_width或layout_height指定為WRAP_CONTENT時,控制項大小一般隨著控制項的子空間或內容進行變化,此時控制項尺寸只要不超過父控制項允許的最大尺寸即可。因此,此時的mode是AT_MOST,size給出了父控制項允許的最大尺寸。

3、取或:要任一運算式的一位為 1,則結果中的該位為 1。否則,結果中的該位為 0。

(size & ~MODE_MASK) | (mode & MODE_MASK) 

MeasureSpec 是個32位的int型,前2位是是分配給mode 的;後三十位是是分配給size的。

 

 

4 getMode()

 

如果現在一個 MeasureSpec  =01000000000000000000111100000000;

01000000000000000000111100000000 & MODE_MASK (11000000000000000000000000000000)=01000000000000000000000000000000  

//結果值是EXACTLY mode

5 、getSize()

如果現在一個 MeasureSpec  =01000000000000000000111100000000

01000000000000000000111100000000 & ~MODE_MASK(00111111111111111111111111111111)=000000000000000000111100000000  //得出size值111100000000=3840

 

出處:http://www.cnblogs.com/sueZheng/p/4046869.html


android 用linearLayout來實現的簽到進度條問題

這個其實很簡單吧,你在application
裡就可以計算這個長度了,因為一旦運行程式,不考慮橫豎屏切換,只考慮堅屏的話。螢幕寬高是不會變的。
DisplayMetrics metric = getResources().getDisplayMetrics();ImageUtils.SCREEN_WIDTH = metric.widthPixels < metric.heightPixels ? metric.widthPixels : metric.heightPixels;這個就是你螢幕的寬高。減去螢幕左右的10dp就是你要的進度條寬度。
public static int dp2Px(Context c, float dp){if (c == null){return 0;}float density = c.getResources().getDisplayMetrics().density;return (int) (dp * density + 0.5f);}ImageUtils.PROGRESS_WIDTH = ImageUtils.SCREEN_WIDTH - dp2Px(context, 20);這樣你就不用在adapter裡面進行多次運算了

 

聯繫我們

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