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.