Android屏幕適配終極方案-實(shí)戰(zhàn)篇

ScreenAdapter


項(xiàng)目地址

ScreenAdapter項(xiàng)目源于開發(fā)時老被設(shè)計(jì)獅吐槽沒有高度還原設(shè)計(jì)稿,加上Android屏幕分辨率眾多,總是需要微調(diào)或舍棄非主流分辨率的適配。ScreenAdapter由此而生,經(jīng)歷了多個項(xiàng)目的實(shí)踐,適配情況基本達(dá)到理想情況。

ScreenAdapter有以下特點(diǎn):

  • 簡單、方便
  • 接入簡單,極少侵入
  • 代碼、布局換算px全局生效
  • 布局更優(yōu)
  • dp可以直接替換為pt/in/mm
  • 可直接使用設(shè)計(jì)稿標(biāo)注尺寸
  • 無須布局嵌套即可實(shí)現(xiàn)
  • 性能更優(yōu)
  • 無須依賴AutoLayout之類的第三方庫

怎樣使用ScreenAdapter?

如果你覺得ScreenAdapter對你有幫助,您的star和issues將是對我最大支持.^_^

示例

當(dāng)你看到此圖時,你會如何在各種分辨率下還原效果?


設(shè)計(jì)圖
效果

適配情況:

描述 華為榮耀8 魅族MX3 魅族MX2
分辨率 1920*1080 1800*1080 1280*800
寬高比 16:9 15:9 16:10
效果圖
榮耀8
mx3
mx2

下載

compile 'com.hjhrq991.screenadapter:ScreenAdapter:1.0.2'

使用

無Application

可在AndroidManifest使用ScreenAdapterApplication

<application
android:name="com.hjhrq991.screenadapter.ScreenAdapterApplication">

</application>

有自定義Application

如你的自定義Application繼承Application,修改成繼承ScreenAdapterApplication即可

public class MyApplication extends ScreenAdapterApplication {

}

如你的自定義Application繼承其他Application,可通過ScreenAdaperHelper來實(shí)現(xiàn)。DESIGN_WIDTH為設(shè)計(jì)稿的寬度(如果你的設(shè)計(jì)稿寬度不統(tǒng)一,請找你們的設(shè)計(jì)獅),DESIGN_WIDTH的值建議使用px換算dp的結(jié)果,即px/2。
具體實(shí)現(xiàn)如下:

private float DESIGN_WIDTH = 375f;

private ScreenAdaperHelper mHelper;

@Override
public void onCreate() {
super.onCreate();
//init helper with default with.
//        mHelper = ScreenAdaperHelper.init(this);
//init helper with the width for design drawing
mHelper = ScreenAdaperHelper.init(this, DESIGN_WIDTH);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mHelper.onConfigurationChanged();
}

@Override
public Resources getResources() {
Resources res = super.getResources();
//Will call before init
if (mHelper != null)
mHelper.getResources(res);
return res;
}

適配

布局適配

由于本方案運(yùn)行時才生效,因此建議寫布局文件時優(yōu)先使用dp做為單位,寫完布局后使用pt/in/mm全局替換dp即可.當(dāng)然如果不嫌麻煩的也可以在布局preview時可以使用模擬器設(shè)備進(jìn)行預(yù)覽,填寫設(shè)計(jì)稿尺寸,換算好屏幕尺寸即進(jìn)行預(yù)覽。
同時建議多使用FrameLayout或者LinearLayout,盡量少用RelativeLayout,能更大程度減少層級。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="375pt"
android:layout_height="240pt"
android:background="@color/white"
android:layout_marginBottom="15pt"
android:orientation="vertical">

<FrameLayout
android:id="@+id/layout_left"
android:layout_width="160pt"
android:layout_height="240pt">

<View
android:layout_width="150pt"
android:layout_height="150pt"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="5pt"
android:background="@color/gray" />

<View
android:layout_width="match_parent"
android:layout_height="14pt"
android:layout_marginLeft="15pt"
android:layout_marginRight="15pt"
android:layout_marginTop="10pt"
android:background="@color/gray" />

<View
android:layout_width="90pt"
android:layout_height="10pt"
android:layout_marginLeft="15pt"
android:layout_marginTop="32pt"
android:background="@color/gray"
android:gravity="center_vertical" />

<View
android:layout_width="60pt"
android:layout_height="26pt"
android:layout_marginLeft="15pt"
android:layout_marginRight="15pt"
android:layout_marginTop="45pt"
android:background="@color/gray" />
</FrameLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/gridview"
android:layout_width="215pt"
android:layout_height="240pt"
android:layout_gravity="right"
android:listSelector="@color/white" />

<View
android:layout_width="0.5pt"
android:layout_height="match_parent"
android:layout_marginLeft="160pt"
android:background="@color/ececec" />

<View
android:layout_width="0.5pt"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_marginRight="107.5pt"
android:background="@color/ececec" />

<View
android:layout_width="215pt"
android:layout_height="0.5pt"
android:layout_gravity="center_vertical|right"
android:background="@color/ececec" />

</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120pt"
android:paddingBottom="2pt"
android:paddingLeft="15pt"
android:paddingRight="15pt"
android:paddingTop="10pt">

<View
android:layout_width="match_parent"
android:layout_height="14pt"
android:background="@color/gray" />

<View
android:layout_width="match_parent"
android:layout_height="10pt"
android:layout_marginRight="15pt"
android:layout_marginTop="16pt"
android:background="@color/gray" />

<View
android:layout_width="77pt"
android:layout_height="77pt"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/gray" />
</FrameLayout>

代碼適配

如需代碼里換算px,可調(diào)用ScreenAdaperHelper的API方法:

ScreenAdaperHelper.ptTopx(mContext, 210);

當(dāng)然,調(diào)用你原有的方法也是可以,本方案已全局換算。

適配情況

各款設(shè)備均能高度還原設(shè)計(jì)稿效果,布局使用pt/in/mm代替dp、sp,dp、sp由于使用頻率較高繼續(xù)保留。
橫豎屏切換時會以當(dāng)前的屏幕寬度進(jìn)行換算,如你的布局非列表或者Scrollview,建議橫豎屏使用不同的layout進(jìn)行適配。

  • v1.0.0
  • 大部分機(jī)型適配,已適配華為、魅族、vivo、oppo、三星、一加、中興、酷派、錘子、樂視等絕大部分機(jī)型
  • 解決部分情況下DisplayMetrics被重置的問題
  • v1.0.1
  • 優(yōu)化可視區(qū)寬度的獲取方法
  • v1.0.2
  • 修復(fù)小米Android5.1.1系統(tǒng)下適配方案失效的問題
  • 增加代碼換算px工具方法

當(dāng)前未解決問題:華為等有可動態(tài)導(dǎo)航欄的設(shè)備,橫屏情況下,收起/展開導(dǎo)航欄并沒有好的方式監(jiān)聽,且不會觸發(fā)頁面刷新,強(qiáng)行刷新UI勢必浪費(fèi)性能。
當(dāng)然,如你需處理該情況,可以自行在Activity監(jiān)聽android.id.R.content寬高的變化進(jìn)行重繪ui。
如您對此問題有好的解決方法,請留言反饋給我!謝謝!

其他

有任何問題,可以在issues給我留言反饋。
或其他方式聯(lián)系我:
Gmail:hjhrq1991@gmail.com
QQ:444563258


License

Apache 2.0

如有轉(zhuǎn)載,請注明出處:http://blog.csdn.net/hjhrq1991

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容