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裡面進行多次運算了