A little explanation of Getdimension,getdimensionpixeloffset and getdimensionpixelsize

Source: Internet
Author: User



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, however, is multiplied by denstiy regardless of whether the DP or SP or PX is written.



The code is as follows:






<resources>  
    <dimen name="dp_01">16dp</dimen>  
    <dimen name="px_01">16px</dimen>  
    <dimen name="sp_01">16sp</dimen>  
</resources>  



float a1=getResources().getDimension(R.dimen.dp_01);    
int a2=getResources().getDimensionPixelOffset(R.dimen.dp_01);    
int a3=getResources().getDimensionPixelSize(R.dimen.dp_01);    
  
float b1=getResources().getDimension(R.dimen.px_01);  
int b2=getResources().getDimensionPixelOffset(R.dimen.px_01);    
int b3=getResources().getDimensionPixelSize(R.dimen.px_01);    
  
float c1=getResources().getDimension(R.dimen.sp_01);    
int c2=getResources().getDimensionPixelOffset(R.dimen.sp_01);    
int c3=getResources().getDimensionPixelSize(R.dimen.sp_01);    
  
Log.d("test", "getDimension= "+a1+", getDimensionPixelOffset="+a2+",getDimensionPixelSize="+a3);    
Log.d("test", "getDimension= "+b1+", getDimensionPixelOffset="+b2+",getDimensionPixelSize="+b3);    
Log.d("test", "getDimension= "+c1+", getDimensionPixelOffset="+c2+",getDimensionPixelSize="+c3);    


View output in Logcat:







//Device (480*800,240dpi,density=1.5):  
getDimension= 24.0, getDimensionPixelOffset=24,getDimensionPixelSize=24    
getDimension= 16.0, getDimensionPixelOffset=16,getDimensionPixelSize=24    
getDimension= 24.0, getDimensionPixelOffset=24,getDimensionPixelSize=24    






Android-gettextsize The return value is in pixels (px), settextsize () in SPCategory: Android2012-03-13 16:09 13386 People read Comments (4) favorite reports 2011-08-26 22:57 509 people read comments (1) favorite reports When you use the following code, you find that the font size does not grow larger, but it becomes smaller:
size = (int) mtext.gettextsize () + 1;
Mtext.settextsize (size);
It was later found that the GetTextSize return value is in pixels (px), and Settextsize () is in SP, which results from inconsistent units.


Here you can use a different form of settextsize () to specify the unit:
settextsize (int unit, int size)
TypedValue.COMPLEX_UNIT_PX:Pixels
TypedValue.COMPLEX_UNIT_SP:Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP:Device Independent Pixels


The following is normal:
size = (int) mtext.gettextsize () + 1;
Mtext.settextsize (typedvalue.complex_unit_px, size);


A little explanation of Getdimension,getdimensionpixeloffset and 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.