In some cases, the artist often give me the PX this time we need to according to the screen resolution to turn PX into a dip or SP below is the calculation formula:
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity " android:background=" @android: Color/white "> <button android:id=" @+id/button " android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center" android:padding= "20DP" android:text= "Start"/> </FrameLayout>
Package Com.example.test5;import Android.app.activity;import Android.content.context;import android.os.Bundle; Import Android.util.displaymetrics;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.toast;public class MainActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.main); Findviewbyid (R.id.button). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Test ();}}); public boolean Isgreaterthan (string time) {string Time1=time.replace (":", ""). Trim (); String time2= "0130"; return Integer.parseint (time2)-integer.parseint (time1) <=0;} private void Test () {final float Fontscale = this.getresources (). Getdisplaymetrics (). scaleddensity; Final float scale = this.getresources (). Getdisplaymetrics (). Density;int screenwidth = Getwindowmanager (). GetDefaultdisplay (). GetWidth (); int screenheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); Displaymetrics dm = new Displaymetrics (); DM = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); float density = dm.density; float densitydpi = dm.densitydpi; LOG.D ("text", "scale=" +scale+ "fontscale=" +fontscale+ "screenwidth=" +screenwidth+ "screenheight=" +screenHeight); LOG.D ("text", "density=" +density+ "densitydpi=" +densitydpi); int A=PX2SP (26,scale); String S1=px2dip (this,13) + ""; String S6=px2dip (this,60) + ""; String S7=px2dip (this,11) + ""; String S8=px2dip (this,162) + ""; String S9=px2dip (this,232) + ""; String S10=px2dip (this,44) + ""; String S11=px2dip (this,178) + ""; String S12=px2dip (this,610) + ""; String S13=px2dip (this,420) + ""; String S14=px2dip (this,126) + ""; String S2=px2dip (this,32) + ""; String S3=PX2SP (Fontscale) + ""; String S4=PX2SP (Fontscale) + ""; String S5=PX2SP (Fontscale) + ""; System.out.println ("S1:" +s1+ "-----" + "S6:" +s6+ "-----" + "S7:"+s7+ "-----" + "S8:" +s8+ "-----" + "S9:" +s9+ "-----" + "S10:" +s10+ "-----" + "S11:" +s11+ "-----" + "S12:" +s12+ "-----" + "S13:" + s13+ "-----" + "S14:" +s14+ "-----" + "s2:" +s2+ "-----" + "S3" +s3+ "-----" + "S4" +s4+ "-----" + "S5" +s5+ "-----");} public static int dip2px (context context, float Dpvalue) {final float scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (Dpvalue * scale + 0.5f);} public static int Px2dip (context context, float Pxvalue) {final float scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (Pxvalue/scale + 0.5f);} public static int px2sp (float pxvalue, float fontscale) {return (int) (Pxvalue/fontscale + 0.5f);} public static int sp2px (float spvalue, float fontscale) {return (int) (Spvalue * fontscale + 0.5f);} public void ST (String s) {Toast.maketext (this, S, Toast.length_short). Show (); }}
Source Download
Android px to dip px to SP rule