【移動應(yīng)用開發(fā)技術(shù)】Android屏幕適配怎么分析_第1頁
【移動應(yīng)用開發(fā)技術(shù)】Android屏幕適配怎么分析_第2頁
【移動應(yīng)用開發(fā)技術(shù)】Android屏幕適配怎么分析_第3頁
【移動應(yīng)用開發(fā)技術(shù)】Android屏幕適配怎么分析_第4頁
【移動應(yīng)用開發(fā)技術(shù)】Android屏幕適配怎么分析_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】Android屏幕適配怎么分析

Android屏幕適配怎么分析,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面在下將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。為什么要屏幕適配?碎片化品牌機(jī)型碎片化屏幕尺寸碎片化操作系統(tǒng)碎片化為了保證用戶獲得一致的用戶體驗(yàn)效果,使得某一元素在Android不同尺寸、不同分辨率的手機(jī)上具備相同的顯示效果,則需要我們進(jìn)行屏幕適配?;A(chǔ)概念屏幕尺寸屏幕尺寸是指屏幕對角線的長度,單位是英寸,1inch=2.54cm屏幕分辨率手機(jī)在橫向和縱向上的像素點(diǎn)數(shù)總和,單位是像素(pixel),1px=

1像素點(diǎn),舉個栗子,1080x1920,即寬度方向上有1080個像素點(diǎn),在高度方向上有1920個像素點(diǎn)。屏幕像素密度每英寸像素點(diǎn)個數(shù),單位是dpi,dotsperinch。為簡便起見,Android將所有屏幕密度分組為六種通用密度:

低、中、高、超高、超超高和超超超高。ldpi(低)~120dpimdpi(中)~160dpihdpi(高)~240dpixhdpi(超高)~320dpixxhdpi(超超高)~480dpixxxhdpi(超超超高)~640dpidpi_example屏幕密度無關(guān)像素dp(dip)DensityIndependentPixels,即密度無關(guān)像素。160dpi,1dp=1px240dpi,1dp=1.5px320dpi,1dp=2px480dpi,1dp=3px640dpi,1dp=4px使用px在低、中、高屏幕密度下的效果

density-test-bad使用dp在低、中、高屏幕密度下的效果

dimen_example2獨(dú)立比例像素spScaleIndependentPixels,即sp或sip。Android開發(fā)時用此單位設(shè)置文字大小,可根據(jù)字體大小***項(xiàng)進(jìn)行縮放,推薦使用12sp、14sp、18sp、22sp作為字體設(shè)置的大小,不推薦使用奇數(shù)和小數(shù),容易造成精度的丟失問題,小于12sp的字體會太小導(dǎo)致用戶看不清。屏幕適配之圖片適配在設(shè)計(jì)圖標(biāo)時,對于5種主流的像素密度(mdpi,hdpi,xhdpi,xxhdpi和xxxdpi)應(yīng)按照2:3:4:6:8的比例進(jìn)行縮放。例如一個啟動圖片ic_launcher.png,它在各個像素密度文件夾下大小為:ldpi(低)mdpi(中)48*48hdpi(高)72*72xhdpi(超高)96*96xxhdpi(超超高)144*144xxxhdpi(超超超高)192*192存在的問題每套分辨率出一套圖,為美工或者設(shè)計(jì)增加了許多工作量對Android工程文件的apk包變的很大解決方法AndroidSDK加載圖片流程Android

SDK會根據(jù)屏幕密度自動選擇對應(yīng)的資源文件進(jìn)行渲染加載,比如說,SDK檢測到你手機(jī)的分辨率是xhdpi,會優(yōu)先到xhdpi文件夾下找對應(yīng)的圖片資源;如果xhdpi文件夾下沒有圖片資源,那么就會去分辨率高的文件夾下查找,比如xxhdpi,直到找到同名圖片資源,將它按比例縮小成xhpi圖片;如果往上查找圖片還是沒有找到,那么就會往低分辨率的文件夾查找,比如hdpi,直到找到同名圖片資源,將它按比例放大成xhpi圖片。根據(jù)加載圖片的流程,可以得出理論上提供一套圖片就可以了。那么應(yīng)該提供哪種分辨率規(guī)格呢?原則上越高越好,同時結(jié)合當(dāng)前主流分辨率屏幕自動拉伸圖片ninepatch_rawninepatch_examples屏幕適配之布局適配布局參數(shù)使用wrap_content,match_parent,layout_weight。weight的使用weight_examples當(dāng)layout_width為0dp,layout_weight分別是1和2

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<Button

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="weight

=

1"/>

<Button

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="weight

=

2"/>

</LinearLayout>當(dāng)layout_width為match_parent,layout_weight分別為1和2<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="weight

=

1"/>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="weight

=

2"/>

</LinearLayout>weight的計(jì)算寬度=原來寬度+權(quán)重比值*剩余寬度當(dāng)layout_width為0dp,layout_weight分別是1和2***個按鈕:寬度=0+1/3*屏寬=1/3屏寬第二個按鈕:寬度=0+2/3*屏寬=2/3屏寬當(dāng)layout_width為match_parent,layout_weight分別是1和2***個按鈕:寬度=屏寬+1/3(屏寬-2屏寬)=2/3屏寬第二個按鈕:寬度=屏寬+2/3(屏寬-2屏寬)=1/3屏寬布局使用使用相對布局,禁用絕對布局。限定符尺寸限定符在手機(jī)較小的屏幕上,加載layout文件夾布局在平板電腦和電視的屏幕(>7英寸)上,加載layout-large文件夾的布局Android3.2版本之前最小寬度限定符在手機(jī)較小的屏幕上,加載layout文件夾布局標(biāo)準(zhǔn)7英寸平板(其最小寬度為600dp),加載layout-sw600dp文件夾的布局在Android3.2版本及之后版本布局別名適配手機(jī)的單面板(默認(rèn))布局:res/layout/activity_main.xml適配尺寸>7寸平板的雙面板布局(Android3.2前):res/layout-large/activity_main.xml適配尺寸>7寸平板的雙面板布局(Android3.2后):res/layout-sw600dp/activity_main.xml***的兩個文件的xml內(nèi)容是完全相同的,這會帶來:文件名的重復(fù)從而帶來一些列后期維護(hù)的問題,修改一個文件,可能忘記修改另外一個。于是為了要解決這種重復(fù)問題,我們引入了布局別名。適配手機(jī)的單面板(默認(rèn))布局:res/layout/activity_main.xml適配尺寸>7寸平板的雙面板布局:res/layout/activity_twopanes.xmlresalues/layout.xml<?xml

version="1.0"

encoding="utf-8"?>

<resources>

<item

name="main"

type="layout">@layout/activity_main</item>

</resources>resalues-large/layout.xml<?xml

version="1.0"

encoding="utf-8"?>

<resources>

<item

name="main"

type="layout">@layout/activity_twopanes</item>

</resources>resalues-sw600dp/layout.xml<?xml

version="1.0"

encoding="utf-8"?>

<resources>

<item

name="main"

type="layout">@layout/activity_twopanes</item>

</resources>setContentView(R.layout.main);屏幕方向限定符res/layout-landres/layout-portres/layout-sw600dp-landres/layout-sw600dp-port屏幕適配之dimen適配Nexus4(4.7英寸768x1280:xhdpi)

NexusS(4英寸480x800:hdpi)dimen_example2即使使用dp,依然不能解決屏幕分辨率的適配問題,我們可以針對不同的屏幕創(chuàng)建不同的dimen值。resalues/dimens.xml<resources>

<dimen

name="button_length_1">180dp</dimen>

<dimen

name="button_length_2">160dp</dimen>

</resources>resalues-480x800/dimens.xml<resources>

<dimen

name="button_length_1">113dp</dimen>

<dimen

name="button_length_2">100dp</dimen>

</resources>屏幕適配之百分比布局官方文檔GithubSample

<?xml

version="1.0"

encoding="utf-8"?>

<android.support.percent.PercentRelativeLayout

xmlns:android="/apk/res/android"

xmlns:app="/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<Button

android:layout_width="0dp"

android:layout_height="wrap_content"

android:text="30%"

app:layout_widthPercent="30%"/>

<Button

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:text="20%"

app:layout_widthPercent="20%"/>

</android.support.percent.PercentRelativeLayout>屏幕適配之自適應(yīng)用戶界面newsreader_landnewsreader_port當(dāng)NewsReader在橫屏?xí)r是雙面板,左側(cè)是HeadLinesFragment,右側(cè)是ArticleFragment,點(diǎn)擊新聞標(biāo)題,

切換ArticleFragment的內(nèi)容。當(dāng)NewsReader在豎屏?xí)r是單面板,只有個HeadLinesFragment,

點(diǎn)擊新聞標(biāo)題,跳轉(zhuǎn)到ArticleActivity去顯示新聞內(nèi)容。所以,要實(shí)現(xiàn)這樣的橫豎屏適配,只是通過布局是完成不了的,不同業(yè)務(wù)邏輯的處理,還需要寫代碼來完成,這就是我們的自適應(yīng)用戶界面。使用布局別名resalues/layouts.xml

<resources>

<item

name="main_layout"

type="layout">@layout/onepane_with_bar</item>

<bool

name="has_two_panes">false</bool>

</resources>resalues-sw600dp-land/layouts.xml

<resources>

<item

name="main_layout"

type="layout">@layout/twopanes</item>

<bool

name="has_two_panes">true</bool>

</resources>resalues-sw600dp-port/layouts.xml<resources>

<item

name="main_layout"

type="layout">@layout/onepane</item>

<bool

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論