標籤:
適配:即當前應用在相同的手機上面顯示相同的效果。適配前需要首先確定當前手機所屬像素密度類型(如: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下螢幕適配