For example, DP and SP in Android

Source: Internet
Author: User

There are a lot of SP and DP online in AndroidArticleI have talked about it all, but there is always a feeling that I am not doing anything. Now, let's talk about DP and sp. Let's talk about it. If you are not interested, please click it.

1. DP (or dipDevice Independent pixels)

An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.Different devices have different display effects, which are related to the device hardware.

In AndroidCodeAs follows:

 

// File Location: android4.0 \ frameworks \ base \ core \ Java \ Android \ util \ displaymetrics. java public static final int density_device = Response (); public float density; Public void settodefaults () {widthpixels = 0; heightpixels = 0; density = density_device/(float) density_default; // The percentage of DP densitydpi = density_device; scaleddensity = density; // This is the proportion of SP xdpi = density_device; ydpi = density_device; noncompatwidthpixels = 0; bytes = 0 ;} private Static int getdevicedensity () {// qemu. SF. LCD _density can be used to override Ro. SF. LCD _density // when running in the emulator, allowing for dynamic deployments. // The reason for this is that Ro. SF. LCD _density is write-once and is // set by the INIT process when it parses build. prop before anything else. return systemproperties. getint ("qemu. SF. LCD _density ", systemproperties. getint ("Ro. SF. LCD _density ", density_default); // from the system property Ro. SF. LCD _density to obtain the screen density // File Location: android4.0 \ packages \ inputmethods \ latinime \ Java \ SRC \ com \ Android \ inputmethod \ Latin \ utils. java public static float getdipscale (context) {final float scale = context. getresources (). getdisplaymetrics (). density; return scale;} public static int diptopixel (float scale, int DIP) {return (INT) (Dip * scale + 0.5); // conversion formula from dip to PX}

Ii. Sp (Scaled pixels)

 

It is mainly used for font display. It is a pixel unrelated to the scale. It is similar to DP, but can be scaled based on the user's font size preference.

 

// File Location: android4.0 \ packages \ apps \ Settings \ SRC \ com \ Android \ Settings \ display. java private spinner. onitemselectedlistener mfontsizechanged = new Spinner. onitemselectedlistener () {public void onitemselected (Android. widget. adapterview AV, view V, int position, long ID) {If (position = 0) {// The following Code sets the font ratio: mcurconfig. fontscale =. 75f;} else if (position = 2) {mcurconfig. fontscale = 1.25f;} else {mcurconfig. fontscale = 1.0f;} updatefontscale ();} public void onnothingselected (Android. widget. adapterview AV) {}}; private void updatefontscale () {mdisplaymetrics. scaleddensity = mdisplaymetrics. density * mcurconfig. fontscale; // combine the configured font ratio code into scaleddensity to float size = mtextsizetyped. getdimension (mdisplaymetrics); mpreview. settextsize (typedvalue. complex_unit_px, size );}

 

 

Related Article

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.