Android getdimension,getdimensionpixeloffset,getdimensionpixelsize

Source: Internet
Author: User

1. For example, you might want to get the value of a custom property in the onmeasure (int, int) method. Such as:

1TypedArray A =context.obtainstyledattributes (Attrs,2 r.styleable.cascadelayout);3 4         Try {5             //gets the value that is set for the custom property in the XML file6Mhorizontalspacing =A.getdimensionpixelsize (7 r.styleable.cascadelayout_horizontal_spacing,8 getresources (). Getdimensionpixelsize (9 r.dimen.cascade_horizontal_spacing));Ten  OneMverticalspacing =A.getdimensionpixelsize ( A r.styleable.cascadelayout_vertical_spacing, - getresources (). Getdimensionpixelsize ( - r.dimen.cascade_vertical_spacing)); theSystem.out.println ("mhorizontalspacing-->>" +mhorizontalspacing -+ "mverticalspacing" +mverticalspacing); -}Catch(Exception e) { -}finally { + a.recycle (); -}

But I set the cascadelayout_horizontal_spacing and cascadelayout_vertical_spacing in the custom view

1 <Com.example.fanghack003.view.CascadeLayout2         Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent"4 cascade:horizontal_spacing= "30DP"5 cascade:vertical_spacing= "20DP" 6 Android:background= "#FF00FF"7         />

The value set is but the log shows that mhorizontalspacing and mverticalspacing are 60 and 40 respectively.

The functions of getdimension and Getdimensionpixeloffset are similar,

is to get the value of a dimen, but if the unit is a DP or SP, you need to multiply it by density

If it is PX, do not multiply. and getdimension returns float,getdimensionpixeloffset returns INT.

Getdimensionpixelsize, whether it's DP or SP or PX, is multiplied by denstiy

The density of my cell phone is 2.0 so it is multiplied by 2;

2. Example:

1     <Resources>  2         <dimenname= "Dp_01">16dp</dimen>  3         <dimenname= "Px_01">16px</dimen>  4         <dimenname= "Sp_01">16sp</dimen>  5     </Resources>  
1     floata1=getresources (). Getdimension (R.DIMEN.DP_01); 2     intA2=getresources (). Getdimensionpixeloffset (R.DIMEN.DP_01); 3     inta3=getresources (). Getdimensionpixelsize (R.DIMEN.DP_01); 4       5     floatb1=getresources (). Getdimension (R.DIMEN.PX_01); 6     intB2=getresources (). Getdimensionpixeloffset (R.DIMEN.PX_01); 7     intb3=getresources (). Getdimensionpixelsize (R.DIMEN.PX_01); 8       9     floatc1=getresources (). Getdimension (R.DIMEN.SP_01); Ten     intC2=getresources (). Getdimensionpixeloffset (R.DIMEN.SP_01);  One     intc3=getresources (). Getdimensionpixelsize (R.DIMEN.SP_01);  A        -LOG.D ("Test", "getdimension=" +a1+ ", getdimensionpixeloffset=" +a2+ ", getdimensionpixelsize=" +A3);  -LOG.D ("Test", "getdimension=" +b1+ ", getdimensionpixeloffset=" +b2+ ", getdimensionpixelsize=" +b3);  theLOG.D ("Test", "getdimension=" +c1+ ", getdimensionpixeloffset=" +c2+ ", getdimensionpixelsize=" +c3 ");
1     //device (480*800,240dpi,density=1.5):  2    getdimension= 24.0, getdimensionpixeloffset=24,getdimensionpixelsize=24    3    getdimension= 16.0, getdimensionpixeloffset=16,getdimensionpixelsize=24    4     getdimension= 24.0, Getdimensionpixeloffset=24,getdimensionpixelsize=24    

Android getdimension,getdimensionpixeloffset,getdimensionpixelsize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.