最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android中px、sp與dp之間進(jìn)行轉(zhuǎn)換詳解

 更新時(shí)間:2022年08月21日 11:02:59   作者:跑快點(diǎn)  
android中在xml布局中我們可以使用dp和px都可以,但是在代碼中,很多方法只提供了設(shè)置px的方法,這時(shí)候就需要用到dp和px相互切換了,下面這篇文章主要給大家介紹了關(guān)于android中px、sp與dp之間進(jìn)行轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下

由于Android手機(jī)廠商很多,導(dǎo)致了不同設(shè)備屏幕大小和分辨率都不一樣,然而我們開(kāi)發(fā)者要保持在不同設(shè)備上顯示同樣的視覺(jué)效果,就需要做一些適配效果。

相關(guān)名詞解釋

  • 屏幕大?。?/strong>通常指的是屏幕對(duì)角線的長(zhǎng)度,使用“寸”為單位來(lái)衡量。
  • 分辨率:指手機(jī)屏幕的像素點(diǎn)個(gè)數(shù),例如:720*1280,指的是寬有720個(gè)像素點(diǎn),高有1280個(gè)像素點(diǎn)。
  • dpi:指的是每英寸像素,是由對(duì)角線上的像素點(diǎn)數(shù)除以屏幕大小所得。

系統(tǒng)屏幕密度

  • ldpi文件夾下對(duì)應(yīng)的密度為120dpi,對(duì)應(yīng)的分辨率為240*320
  • mdpi文件夾下對(duì)應(yīng)的密度為160dpi,對(duì)應(yīng)的分辨率為320*480
  • hdpi文件夾下對(duì)應(yīng)的密度為240dpi,對(duì)應(yīng)的分辨率為480*800
  • xhdpi文件夾下對(duì)應(yīng)的密度為320dpi,對(duì)應(yīng)的分辨率為720*1280
  • xxhdpi文件夾下對(duì)應(yīng)的密度為480dpi,對(duì)應(yīng)的分辨率為1080*1920

由于各種屏幕密度的不同,導(dǎo)致了同一張圖片在不同的手機(jī)屏幕上顯示不同;在屏幕大小相同的情況下,高密度的屏幕包含了更多的像素點(diǎn)。android系統(tǒng)將密度為160dpi的屏幕作為標(biāo)準(zhǔn)對(duì)于mdpi文件夾,在此屏幕的手機(jī)上1dp=1px。從上面系統(tǒng)屏幕密度可以得出各個(gè)密度值之間的換算;在mdpi中1dp=1px,在hdpi中1dp=1.5px,在xhdpi中1dp=2px,在xxhpi中1dp=3px。換算比例如下:ldpi:mdpi:hdpi:xhdpi:xxhdpi=3:4:6:8:12。

單位換算方法

/**
     * dp轉(zhuǎn)換成px
     */
    private int dp2px(Context context,float dpValue){
        float scale=context.getResources().getDisplayMetrics().density;
        return (int)(dpValue*scale+0.5f);
    }

    /**
     * px轉(zhuǎn)換成dp
     */
    private int px2dp(Context context,float pxValue){
        float scale=context.getResources().getDisplayMetrics().density;
        return (int)(pxValue/scale+0.5f);
    }
    /**
     * sp轉(zhuǎn)換成px
     */
    private int sp2px(Context context,float spValue){
        float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue*fontScale+0.5f);
    }
    /**
     * px轉(zhuǎn)換成sp
     */
    private int px2sp(Context context,float pxValue){
        float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue/fontScale+0.5f);
    }

利用系統(tǒng)TypeValue類來(lái)轉(zhuǎn)換

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());
    }

補(bǔ)充:sp與dp的區(qū)別

下面我們進(jìn)行一下實(shí)驗(yàn): textSize的單位分別設(shè)置為sp和dp,然后改變系統(tǒng)字體大小

<?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做單位,設(shè)置有效果

2、dp做單位沒(méi)有效果

總結(jié)

到此這篇關(guān)于android中px、sp與dp之間進(jìn)行轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)android px sp dp之間轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

台安县| 莱西市| 罗田县| 汪清县| 黄山市| 淮滨县| 周至县| 美姑县| 丹江口市| 鄢陵县| 洞口县| 金湖县| 侯马市| 皮山县| 涞源县| 象州县| 三台县| 大安市| 探索| 罗江县| 东丰县| 申扎县| 会昌县| 隆德县| 锦屏县| 太仆寺旗| 肥乡县| 菏泽市| 桃园县| 柏乡县| 宿州市| 攀枝花市| 长葛市| 金湖县| 梧州市| 且末县| 西贡区| 托克逊县| 全州县| 家居| 准格尔旗|