android中px、sp与dp之间进行转换详解

2022-08-21 11:43:58
目录
相关名词解释系统屏幕密度单位换算方法利用系统TypeValue类来转换补充:sp与dp的区别总结

由于Android手机厂商很多,导致了不同设备屏幕大小和分辨率都不一样,然而我们开发者要保持在不同设备上显示同样的视觉效果,就需要做一些适配效果。

相关名词解释

    屏幕大小:通常指的是屏幕对角线的长度,使用“寸”为单位来衡量。分辨率:指手机屏幕的像素点个数,例如:720*1280,指的是宽有720个像素点,高有1280个像素点。dpi:指的是每英寸像素,是由对角线上的像素点数除以屏幕大小所得。

    系统屏幕密度

      ldpi文件夹下对应的密度为120dpi,对应的分辨率为240*320mdpi文件夹下对应的密度为160dpi,对应的分辨率为320*480hdpi文件夹下对应的密度为240dpi,对应的分辨率为480*800xhdpi文件夹下对应的密度为320dpi,对应的分辨率为720*1280xxhdpi文件夹下对应的密度为480dpi,对应的分辨率为1080*1920

      由于各种屏幕密度的不同,导致了同一张图片在不同的手机屏幕上显示不同;在屏幕大小相同的情况下,高密度的屏幕包含了更多的像素点。android系统将密度为160dpi的屏幕作为标准对于mdpi文件夹,在此屏幕的手机上1dp=1px。从上面系统屏幕密度可以得出各个密度值之间的换算;在mdpi中1dp=1px,在hdpi中1dp=1.5px,在xhdpi中1dp=2px,在xxhpi中1dp=3px。换算比例如下:ldpi:mdpi:hdpi:xhdpi:xxhdpi=3:4:6:8:12。

      单位换算方法

      /**
           * dp转换成px
           */
          private int dp2px(Context context,float dpValue){
              float scale=context.getResources().getDisplayMetrics().density;
              return (int)(dpValue*scale+0.5f);
          }
      
          /**
           * px转换成dp
           */
          private int px2dp(Context context,float pxValue){
              float scale=context.getResources().getDisplayMetrics().density;
              return (int)(pxValue/scale+0.5f);
          }
          /**
           * sp转换成px
           */
          private int sp2px(Context context,float spValue){
              float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
              return (int) (spValue*fontScale+0.5f);
          }
          /**
           * px转换成sp
           */
          private int px2sp(Context context,float pxValue){
              float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
              return (int) (pxValue/fontScale+0.5f);
          }

      利用系统TypeValue类来转换

      private int dp2px(Context context,int dpValue){
              return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,context.getResources().getDisplayMetrics());
          }
          private int sp2px(Context context,int spValue){
              return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,context.getResources().getDisplayMetrics());
          }

      补充:sp与dp的区别

      下面我们进行一下实验:>

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
       
          <TextView
              android:layout_width="200dp"
              android:layout_height="wrap_content"
              android:text="尚硅谷科技"
              android:background="#ff0000"
              android:textSize="20sp"/>
       
          <TextView
              android:id="@+id/textView2"
              android:layout_width="200px"
              android:layout_height="wrap_content"
              android:text="尚硅谷科技"
              android:background="#00ff00"
              android:textSize="20dp"/>
       
      </LinearLayout>

      1、用sp做单位,设置有效果

      2、dp做单位没有效果

      总结

      到此这篇关于android中px、sp与dp之间进行转换的文章就介绍到这了,更多相关android>