Android下螢幕適配

來源:互聯網
上載者:User

標籤:

適配:即當前應用在相同的手機上面顯示相同的效果。適配前需要首先確定當前手機所屬像素密度類型(如:xhdpi、hdpi、mdpi等),然後計算其像素密度,按一定比例給出介面元素的布局位置和大小。

在android中,螢幕視頻主要有以下幾種方式:
  • 圖片適配
  • dimens適配
  • 布局檔案適配
  • java代碼適配
  • 權重適配

案例一:

手機型號:G700手機解析度:1280*720 (註:手機兩個直角邊上分別放置了1280及720個像素點)手機尺寸大小:5英寸(手機斜邊長度)假設a,b分別為兩個直角邊,c為斜邊,由勾股定理可得出計算方式:sqrt(a*a+b*b)/c計算結果:sqrt(1280*1280+720*720)/5 ≈ 293.72dpi根據google官方文檔說明得出,當前手機最接近320dpi,則將其歸納在xhdpi手機範圍內,即1dp=2px;

案例二:

手機型號:模擬器手機解析度:800*480(註:手機兩個直角邊上分別放置了800及480個像素點)手機尺寸大小:3.7英寸(手機斜邊大小)計算結果:sqrt(800*800+480*480)/3.7 ≈ 252.15dpi根據google官方文檔(圖1-1)得出,當前手機接近240dpi,則將其歸納在hdpi手機範圍內,即1dp=1.5px。參照以上方式可將市場上大多數手機劃分為5個像素密度等級,分別為:ldpi:120dpi,像素密度與dp轉換關係為:1dp = 0.75pxmdpi:160dpi ,像素密度與dp轉換關係為:1dp = 1pxhdpi:240dpi,像素密度與dp轉換關係為:1dp = 1.5pxxhdpi:320dpi,像素密度與dp轉換關係為:1dp = 2pxxxhdpi:480dpi,像素密度與dp轉換關係為:1dp = 3px


(註:以下案例就當前兩款手機進行螢幕適配測試)

適配方式一:圖片適配

不同像素密度的手機載入工程資源檔(res)中不同資源圖片,以上述兩款手機為例。布局代碼如下:

<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檔案夾下,顯示效果如下:

適配方式二:dimens.xml檔案適配

dimens.xml存在於工程資源(res)檔案夾中不同values(如:value-1280x720、value-800x480)檔案夾下,可用於指定控制項大小,不同像素密度手機載入不同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" >    <!-- 不同的手機載入不同的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>根據上述hdpi dp和px的轉換關係1dp = 1.5px,則160dp = 240px,當前控制項寬度應該位於螢幕中間位置。

G700(xhdpi):載入dimens.xml資源檔,位於res/value-1280x720檔案夾下

<resources>    <dimen name="width">180dp</dimen></resources>根據上述xhdpi dp和px的轉換關係1dp = 2px,則180dp = 360px,當前控制項寬度應該位於螢幕中間位置。

G700(xhdpi)顯示效果如下:

模擬器(hdpi)顯示效果如下:

適配方式三:布局檔案適配

不同解析度的手機,載入不同的布局檔案已達到適配效果。建立多個layout(如:layout-1280x720、layout-800x480)檔案夾用於存放不同像素密度手機所需布局檔案。

模擬器(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手機會去載入的布局檔案" />    </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手機會去載入的布局檔案" />    </RelativeLayout>

G700(xhdpi)顯示效果如下:

模擬器(hdpi)顯示效果如下:

適配方式四:java代碼適配

通過android相應api擷取當前手機的寬高像素值,按比例分配螢幕中控制項的寬高以達到適配效果。核心代碼如下:

布局檔案<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);    //擷取封裝當前手機螢幕資訊對象,用於存放寬高值    DisplayMetrics metrics  = new DisplayMetrics();    //給當前螢幕設定寬高    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)顯示效果如下:

適配方式五:權重適配

通過android提供的(權重)剩餘空間分配,已達到適配效果。顯示介面載入布局檔案如下:

<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)顯示效果如下:

Android下螢幕適配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.