教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

Android培訓(xùn)實(shí)戰(zhàn)教程之屏幕適配

更新時間:2015年12月28日13時38分 來源:傳智播客Android培訓(xùn)學(xué)院 瀏覽次數(shù):

什么是適配

適配 即當(dāng)前應(yīng)用在相同的手機(jī)上面顯示相同的效果。
 
適配前需要首先確定當(dāng)前手機(jī)所屬像素密度類型(如:xhdpi、hdpi、mdpi等),下面以華為G700、模擬器為例,講解如何計(jì)算像素密度。
案例一:
手機(jī)型號:G700
手機(jī)分辨率:1280*720 (注:手機(jī)兩個直角邊上分別放置了1280及720個像素點(diǎn))
手機(jī)尺寸大?。?英寸(手機(jī)斜邊長度)

假設(shè)a,b分別為兩個直角邊,c為斜邊,由勾股定理可得出計(jì)算方式:sqrt(a*a+b*b)/c
計(jì)算結(jié)果:sqrt(1280*1280+720*720)/5 ≈ 293.72dpi
根據(jù)google官方文檔說明得出,當(dāng)前手機(jī)最接近320dpi,則將其歸納在xhdpi手機(jī)范圍內(nèi),
即1dp=2px;
 
案例二:
手機(jī)型號:模擬器
手機(jī)分辨率:800*480(注:手機(jī)兩個直角邊上分別放置了800及480個像素點(diǎn))
手機(jī)尺寸大小:3.7英寸(手機(jī)斜邊大小)

計(jì)算結(jié)果:sqrt(800*800+480*480)/3.7 ≈ 252.15dpi
根據(jù)google官方文檔(圖1-1)得出,當(dāng)前手機(jī)接近240dpi,則將其歸納在hdpi手機(jī)范圍內(nèi),
即1dp=1.5px。
      
按照以上計(jì)算方式,大致可以將市場上的手機(jī)劃分為5個像素密度等級,具體如下:
  1. ldpi:120dpi,像素密度與dp轉(zhuǎn)換關(guān)系為:1dp = 0.75px
  2. mdpi:160dpi ,像素密度與dp轉(zhuǎn)換關(guān)系為:1dp = 1px
  3. hdpi:240dpi,像素密度與dp轉(zhuǎn)換關(guān)系為:1dp = 1.5px
  4. xhdpi:320dpi,像素密度與dp轉(zhuǎn)換關(guān)系為:1dp = 2px
  5. xxhdpi:480dpi,像素密度與dp轉(zhuǎn)換關(guān)系為:1dp = 3px
 

如何適配

下面以華為手機(jī)G700和模擬器的對比,講解如何進(jìn)行屏幕適配,具體方式如下:

適配方式1:圖片適配

不同像素密度的手機(jī)加載工程資源文件(res)中不同資源圖片,以手機(jī)G700和模擬器為例,圖片的布局代碼如下所示:
<RelativeLayout
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" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/a"/>
</RelativeLayout>
 
若使用G700(xhdpi)加載a.jpg文件,該文件位于res/drawable-xhdpi文件夾下,顯示效果如下:
 
若使用模擬器(hdpi)加載a.jpg文件,該文件位于res/drawable-hdpi文件夾下,顯示效果如下:

適配方式2:dimens.xml文件適配

dimens.xml存在于工程資源(res)文件夾中不同values(如:value-1280x720、value-800x480)文件夾下,可用于指定控件大小,不同像素密度手機(jī)加載不同values文件夾下的dimens.xml文件,使用方式如下:  
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <!-- 不同的手機(jī)加載不同的dp -->
    <TextView
        android:background="#987654"
        android:layout_width="@dimen/width"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</LinearLayout>
 
模擬器(hdpi):加載dimens.xml資源文件,位于res/value-800x480文件夾下
<resources>
    <dimen name="width">160dp</dimen>
</resources>
       根據(jù)上述hdpi dp和px的轉(zhuǎn)換關(guān)系1dp = 1.5px,則160dp = 240px,當(dāng)前控件寬度應(yīng)該位于屏幕中間位置。
G700(xhdpi):加載dimens.xml資源文件,位于res/value-1280x720文件夾下
<resources>
    <dimen name="width">180dp</dimen>
</resources>
       根據(jù)上述xhdpi 中dp和px的轉(zhuǎn)換關(guān)系1dp = 2px,則180dp = 360px,當(dāng)前控件寬度應(yīng)該位于屏幕中間位置。
 
G700(xhdpi)顯示的圖片效果如下所示:
模擬器(hdpi)顯示的圖片效果如下所示:

適配方式3:布局文件適配

不同分辨率的手機(jī),加載不同的布局文件已達(dá)到適配效果。創(chuàng)建多個layout(如:layout-1280x720、layout-800x480)文件夾用于存放不同像素密度手機(jī)所需布局文件。
 
模擬器(hdpi):加載activity_main.xml布局文件,位于res/layout-800x480文件夾下:
<RelativeLayout
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" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="800*480手機(jī)會去加載的布局文件" />
    </RelativeLayout>
 
G700(xhdpi):加載activity_main.xml布局文件,位于res/layout-1280x720文件夾下:
    <RelativeLayout
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" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1280*720手機(jī)會去加載的布局文件" />
    </RelativeLayout>
 
G700(xhdpi)顯示的圖片效果如下:
模擬器(hdpi)顯示的圖片效果如下所示:

適配方式4:java代碼適配

通過android相應(yīng)api獲取當(dāng)前手機(jī)的寬高像素值,按比例分配屏幕中控件的寬高以達(dá)到適配效果,下面是布局和實(shí)現(xiàn)功能的核心代碼:      
       布局文件
<RelativeLayout
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" >
    <TextView
        android:id="@+id/tv"
        android:background="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>
       activity中oncreate核心代碼:
    TextView tv  = (TextView) findViewById(R.id.tv);
    //獲取封裝當(dāng)前手機(jī)屏幕信息對象,用于存放寬高值
    DisplayMetrics metrics  = new DisplayMetrics();
    //給當(dāng)前屏幕設(shè)置寬高
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    //獲取高度
    Constant.srceenHeight = metrics.heightPixels;
    //獲取寬度
    Constant.srceenWidth = metrics.widthPixels;
    Log.i(tag, "Constant.srceenHeight = "+Constant.srceenHeight);
    Log.i(tag, "Constant.srceenWidth = "+Constant.srceenWidth);
    //寬高各占50%
    RelativeLayout.LayoutParams layoutParams = new
RelativeLayout.LayoutParams(
            (int)(Constant.srceenWidth*0.5+0.5),
            (int)(Constant.srceenHeight*0.5+0.5));
    tv.setLayoutParams(layoutParams);
G700(xhdpi)顯示效果如下:
模擬器(hdpi)顯示效果如下:

適配方式5:權(quán)重適配

通過android提供的(權(quán)重)剩余空間分配,達(dá)到適配效果。布局文件如下所示:
<LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
    <TextView
        android:background="#000000"
        android:layout_width="0dp"
        android:layout_weight="1"
         android:layout_height="match_parent"/>
    <TextView
        android:background="#123456"
        android:layout_width="0dp"
        android:layout_weight="1"
    android:layout_height="match_parent"/>
</LinearLayout>
 
G700(xhdpi)顯示的圖片效果如下所示:
模擬器(hdpi)顯示的圖片效果如下所示:
 


本文版權(quán)歸傳智播客Android培訓(xùn)學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明作者出處。謝謝!
作者:傳智播客Android培訓(xùn)學(xué)院
首發(fā):http://oisangadgets.com/android/
0 分享到:
和我們在線交談!