Android-screen adaptation to px to dip, android adaptation to pxdip

Source: Internet
Author: User

Android-screen adaptation to px to dip, android adaptation to pxdip

Reprinted please indicate the source: http://blog.csdn.net/l1028386804/article/details/47356583

All the kids shoes that have been developed for Android are aware that, due to the diversity of Android mobile phone screens, screen adaptation issues are involved if you want your own apps to run well on Android phones with different screens. In many cases, the design drawings provided by the artist in the team are usually in px units. When we use them directly in px units, screen adaptation often occurs. In the official Android documents, it is best to use dip as the unit for the length and width of the control, and sp as the unit for the size of the interface text. So how can we convert px to dip. Next, I will provide a conversion tool class:

The Code is as follows:

Package cn. lyz. mobilesafe. utils; import android. content. context; /*** @ ** @ author liuyazhuang **/public class DensityUtils {/*** converts float to dip * @ param context * @ param px * @ return */public static int px2dip (Context context, float px) {// gets the cell phone density float density = context. getResources (). getDisplayMetrics (). density; // The formula is dip = px * phone density int dip = (int) (px * density); return dip ;}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.