自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

漲姿勢(shì)了,原來(lái)Android屏幕適配還可以這么玩

移動(dòng)開(kāi)發(fā) Android
為了保證用戶獲得一致的用戶體驗(yàn)效果,使得某一元素在Android不同尺寸、不同分辨率的手機(jī)上具備相同的顯示效果,則需要我們進(jìn)行屏幕適配。

為什么要屏幕適配? 

 

 

碎片化

  • 品牌機(jī)型碎片化
  • 屏幕尺寸碎片化
  • 操作系統(tǒng)碎片化

為了保證用戶獲得一致的用戶體驗(yàn)效果,使得某一元素在Android不同尺寸、不同分辨率的手機(jī)上具備相同的顯示效果,則需要我們進(jìn)行屏幕適配。

基礎(chǔ)概念

屏幕尺寸

屏幕尺寸是指屏幕對(duì)角線的長(zhǎng)度,單位是英寸,1 inch=2.54 cm

屏幕分辨率

手機(jī)在橫向和縱向上的像素點(diǎn)數(shù)總和,單位是像素(pixel),1px = 1像素點(diǎn),舉個(gè)栗子,1080x1920,即寬度方向上有1080個(gè)像素點(diǎn),在高度方向上有1920個(gè)像素點(diǎn)。

屏幕像素密度

每英寸像素點(diǎn)個(gè)數(shù),單位是dpi,dots per inch。為簡(jiǎn)便起見(jiàn),Android 將所有屏幕密度分組為六種通用密度: 低、中、高、超高、超超高和超超超高。

  • ldpi(低)~120dpi
  • mdpi(中)~160dpi
  • hdpi(高)~240dpi
  • xhdpi(超高)~320dpi
  • xxhdpi(超超高)~480dpi
  • xxxhdpi(超超超高)~640dpi

 

dpi_example

屏幕密度無(wú)關(guān)像素dp(dip)

Density Independent Pixels,即密度無(wú)關(guān)像素。

  • 160dpi, 1dp = 1px
  • 240dpi, 1dp = 1.5px
  • 320dpi, 1dp = 2px
  • 480dpi, 1dp = 3px
  • 640dpi, 1dp = 4px

 

  • 使用px在低、中、高屏幕密度下的效果 

 

 

 

density-test-bad

  • 使用dp在低、中、高屏幕密度下的效果 

 

 

dimen_example2

獨(dú)立比例像素sp

Scale Independent Pixels, 即sp或sip。

Android開(kāi)發(fā)時(shí)用此單位設(shè)置文字大小,可根據(jù)字體大小***項(xiàng)進(jìn)行縮放,推薦使用12sp、14sp、18sp、22sp作為字體設(shè)置的大小,不推薦使用奇數(shù)和小數(shù),容易造成精度的丟失問(wèn)題,小于12sp的字體會(huì)太小導(dǎo)致用戶看不清。

屏幕適配之圖片適配 

 

 

 

在設(shè)計(jì)圖標(biāo)時(shí),對(duì)于5種主流的像素密度(mdpi,hdpi,xhdpi,xxhdpi和xxxdpi)應(yīng)按照2:3:4:6:8的比例進(jìn)行縮放。例如一個(gè)啟動(dòng)圖片ic_launcher.png,它在各個(gè)像素密度文件夾下大小為:

  • ldpi(低)
  • mdpi(中)48*48
  • hdpi(高)72*72
  • xhdpi(超高)96*96
  • xxhdpi(超超高)144*144
  • xxxhdpi(超超超高)192*192

存在的問(wèn)題

  • 每套分辨率出一套圖,為美工或者設(shè)計(jì)增加了許多工作量
  • 對(duì)Android工程文件的apk包變的很大

解決方法

Android SDK加載圖片流程

  • Android SDK會(huì)根據(jù)屏幕密度自動(dòng)選擇對(duì)應(yīng)的資源文件進(jìn)行渲染加載,比如說(shuō),SDK檢測(cè)到你手機(jī)的分辨率是xhdpi,會(huì)優(yōu)先到xhdpi文件夾下找對(duì)應(yīng)的圖片資源;
  • 如果xhdpi文件夾下沒(méi)有圖片資源,那么就會(huì)去分辨率高的文件夾下查找,比如xxhdpi,直到找到同名圖片資源,將它按比例縮小成xhpi圖片;
  • 如果往上查找圖片還是沒(méi)有找到,那么就會(huì)往低分辨率的文件夾查找,比如hdpi,直到找到同名圖片資源,將它按比例放大成xhpi圖片。

根據(jù)加載圖片的流程,可以得出理論上提供一套圖片就可以了。

那么應(yīng)該提供哪種分辨率規(guī)格呢?

原則上越高越好,同時(shí)結(jié)合當(dāng)前主流分辨率屏幕

自動(dòng)拉伸圖片 

 

 

 

ninepatch_raw 

 

 

 

ninepatch_examples

屏幕適配之布局適配

布局參數(shù)

使用wrap_content, match_parent, layout_weight。

weight的使用 

 

 

 

weight_examples

  • 當(dāng)layout_width為0dp,layout_weight分別是1和2 
  1. <LinearLayout 
  2.       android:layout_width="match_parent" 
  3.       android:layout_height="wrap_content" 
  4.       android:orientation="horizontal"
  5.  
  6.       <Button 
  7.           android:layout_width="0dp" 
  8.           android:layout_height="wrap_content" 
  9.           android:layout_weight="1" 
  10.           android:text="weight = 1"/> 
  11.  
  12.       <Button 
  13.           android:layout_width="0dp" 
  14.           android:layout_height="wrap_content" 
  15.           android:layout_weight="2" 
  16.           android:text="weight = 2"/> 
  17.   </LinearLayout>  

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

  1. <LinearLayout 
  2.       android:layout_width="match_parent" 
  3.       android:layout_height="wrap_content" 
  4.       android:orientation="horizontal"
  5.  
  6.       <Button 
  7.           android:layout_width="match_parent" 
  8.           android:layout_height="wrap_content" 
  9.           android:layout_weight="1" 
  10.           android:text="weight = 1"/> 
  11.  
  12.       <Button 
  13.           android:layout_width="match_parent" 
  14.           android:layout_height="wrap_content" 
  15.           android:layout_weight="2" 
  16.           android:text="weight = 2"/> 
  17.   </LinearLayout>  

weight的計(jì)算

寬度 = 原來(lái)寬度 + 權(quán)重比值 * 剩余寬度

  • 當(dāng)layout_width為0dp,layout_weight分別是1和2

***個(gè)按鈕:寬度 = 0 + 1/3 * 屏寬 = 1/3屏寬

第二個(gè)按鈕:寬度 = 0 + 2/3 * 屏寬 = 2/3屏寬

  • 當(dāng)layout_width為match_parent, layout_weight分別是1和2

***個(gè)按鈕:寬度 = 屏寬 + 1/3 (屏寬 - 2 屏寬) = 2/3屏寬

第二個(gè)按鈕:寬度 = 屏寬 + 2/3 (屏寬 - 2 屏寬) = 1/3屏寬

布局使用

使用相對(duì)布局,禁用絕對(duì)布局。

限定符

尺寸限定符

  • 在手機(jī)較小的屏幕上,加載layout文件夾布局
  • 在平板電腦和電視的屏幕(>7英寸)上, 加載layout-large文件夾的布局
  • Android3.2版本之前

最小寬度限定符

  • 在手機(jī)較小的屏幕上,加載layout文件夾布局
  • 標(biāo)準(zhǔn)7英寸平板(其最小寬度為 600 dp),加載layout-sw600dp文件夾的布局
  • 在Android3.2版本及之后版本

布局別名

  • 適配手機(jī)的單面板(默認(rèn))布局:res/layout/activity_main.xml
  • 適配尺寸>7寸平板的雙面板布局(Android 3.2前):res/layout-large/activity_main.xml
  • 適配尺寸>7寸平板的雙面板布局(Android 3.2后):res/layout-sw600dp/activity_main.xml

***的兩個(gè)文件的xml內(nèi)容是完全相同的,這會(huì)帶來(lái):文件名的重復(fù)從而帶來(lái)一些列后期維護(hù)的問(wèn)題,修改一個(gè)文件,可能忘記修改另外一個(gè)。于是為了要解決這種重復(fù)問(wèn)題,我們引入了布局別名。

  • 適配手機(jī)的單面板(默認(rèn))布局:res/layout/activity_main.xml
  • 適配尺寸>7寸平板的雙面板布局:res/layout/activity_twopanes.xml
  • resalues/layout.xml
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  <resources> 
  3.      <item name="main" type="layout">@layout/activity_main</item> 
  4.  </resources>  
  • resalues-large/layout.xml
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.   <resources> 
  3.       <item name="main" type="layout">@layout/activity_twopanes</item> 
  4.   </resources>  
  • resalues-sw600dp/layout.xml
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.   <resources> 
  3.       <item name="main" type="layout">@layout/activity_twopanes</item> 
  4.   </resources>  
  • setContentView(R.layout.main);

屏幕方向限定符

  • res/layout-land
  • res/layout-port
  • res/layout-sw600dp-land
  • res/layout-sw600dp-port

屏幕適配之dimen適配

  • Nexus 4 (4.7英寸 768x1280:xhdpi) 

 

 

 

Nexus S (4英寸 480x800:hdpi) 

 

 

 

dimen_example2

即使使用dp,依然不能解決屏幕分辨率的適配問(wèn)題,我們可以針對(duì)不同的屏幕創(chuàng)建不同的dimen值。

  • resalues/dimens.xml
  1. <resources> 
  2.  <dimen name="button_length_1">180dp</dimen> 
  3.  <dimen name="button_length_2">160dp</dimen> 
  4.  </resources>  
  • resalues-480x800/dimens.xml
  1. <resources> 
  2.       <dimen name="button_length_1">113dp</dimen> 
  3.       <dimen name="button_length_2">100dp</dimen> 
  4.   </resources>  

屏幕適配之百分比布局

  • 官方文檔
  • Github Sample 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.   <android.support.percent.PercentRelativeLayout 
  3.       xmlns:android="http://schemas.android.com/apk/res/android" 
  4.       xmlns:app="http://schemas.android.com/apk/res-auto" 
  5.       android:layout_width="match_parent" 
  6.       android:layout_height="wrap_content"
  7.  
  8.       <Button 
  9.           android:layout_width="0dp" 
  10.           android:layout_height="wrap_content" 
  11.           android:text="30%" 
  12.           app:layout_widthPercent="30%"/> 
  13.  
  14.       <Button 
  15.           android:layout_width="0dp" 
  16.           android:layout_height="wrap_content" 
  17.           android:layout_alignParentRight="true" 
  18.           android:text="20%" 
  19.           app:layout_widthPercent="20%"/> 
  20.  
  21.   </android.support.percent.PercentRelativeLayout>  

屏幕適配之自適應(yīng)用戶界面 

 

 

 

newsreader_land 

 

 

 

newsreader_port

當(dāng)NewsReader在橫屏?xí)r是雙面板,左側(cè)是HeadLinesFragment, 右側(cè)是ArticleFragment, 點(diǎn)擊新聞標(biāo)題, 切換ArticleFragment的內(nèi)容。當(dāng)NewsReader在豎屏?xí)r是單面板,只有個(gè)HeadLinesFragment, 點(diǎn)擊新聞標(biāo)題,跳轉(zhuǎn)到ArticleActivity去顯示新聞內(nèi)容。所以,要實(shí)現(xiàn)這樣的橫豎屏適配,只是通過(guò)布局是完成不了的,不同業(yè)務(wù)邏輯的處理,還需要寫(xiě)代碼來(lái)完成,這就是我們的自適應(yīng)用戶界面。

使用布局別名

  • resalues/layouts.xml 
  1. <resources> 
  2.  <item name="main_layout" type="layout">@layout/onepane_with_bar</item> 
  3.  <bool name="has_two_panes">false</bool> 
  4.  </resources>  
  • resalues-sw600dp-land/layouts.xml 
  1. <resources> 
  2.  <item name="main_layout" type="layout">@layout/twopanes</item> 
  3.  <bool name="has_two_panes">true</bool> 
  4.  </resources>  
  • resalues-sw600dp-port/layouts.xml
  1. <resources> 
  2.      <item name="main_layout" type="layout">@layout/onepane</item> 
  3.      <bool name="has_two_panes">false</bool> 
  4.  </resources>  

判斷是單面板還是雙面板

  1. View articleView = findViewById(R.id.article); 
  2. mIsDualPane = articleView != null && articleView.getVisibility() == View.VISIBLE;//如果能夠找到ArticleFragment則是雙面板  

單雙面板的不同業(yè)務(wù)邏輯 

  1. public void onHeadlineSelected(int index) { 
  2.     mArtIndex = index;    if (mIsDualPane) {  
  3.  
  4.        // display it on the article fragment 
  5.         mArticleFragment.displayArticle(mCurrentCat.getArticle(index)); 
  6.     }    else {  
  7.  
  8.        // use separate activity 
  9.         Intent i = new Intent(this, ArticleActivity.class); 
  10.         i.putExtra("catIndex", mCatIndex); 
  11.         i.putExtra("artIndex"index); 
  12.         startActivity(i); 
  13.     } 
  14.  
責(zé)任編輯:龐桂玉 來(lái)源: 安卓巴士Android開(kāi)發(fā)者門戶
相關(guān)推薦

2022-12-06 17:30:04

2017-11-27 12:24:02

命令行代碼指令

2024-05-17 09:37:26

format屬性Spring

2019-01-29 10:00:59

GitHub開(kāi)源搜索

2015-08-12 16:32:34

華為/物聯(lián)網(wǎng)

2017-11-06 19:09:45

在線抓娃娃機(jī)

2024-03-12 08:44:56

WebWorkerTypeScript語(yǔ)法

2018-10-28 17:54:00

分布式事務(wù)數(shù)據(jù)

2021-03-02 10:50:23

SpringMVC 參數(shù)JavaWeb

2017-10-28 23:13:43

微服務(wù)架構(gòu)開(kāi)發(fā)單體應(yīng)用

2023-12-11 13:57:00

RFM模型激勵(lì)機(jī)制

2020-07-21 18:54:21

Rust類型轉(zhuǎn)換語(yǔ)言

2024-10-28 07:10:00

scroll標(biāo)記前端網(wǎng)格布局

2016-09-29 17:48:32

騰訊云語(yǔ)音質(zhì)檢珍愛(ài)網(wǎng)

2021-11-30 08:04:32

AIIT運(yùn)維

2017-09-27 14:57:44

IOS 11Siri蘋(píng)果

2016-12-26 09:50:15

2013-05-22 09:49:36

2013-09-18 10:44:01

搜狗輸入法詞語(yǔ)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)