ScreenAdapter
項(xiàng)目地址
ScreenAdapter項(xiàng)目源于開發(fā)時(shí)老被設(shè)計(jì)獅吐槽沒有高度還原設(shè)計(jì)稿皆警,加上Android屏幕分辨率眾多,總是需要微調(diào)或舍棄非主流分辨率的適配烂斋。ScreenAdapter由此而生,經(jīng)歷了多個(gè)項(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對你有幫助秸妥,您的star和issues將是對我最大支持.^_^
示例
當(dāng)你看到此圖時(shí),你會如何在各種分辨率下還原效果沃粗?
適配情況:
描述 | 華為榮耀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)行時(shí)才生效疤祭,因此建議寫布局文件時(shí)優(yōu)先使用dp做為單位,寫完布局后使用pt/in/mm全局替換dp即可.當(dāng)然如果不嫌麻煩的也可以在布局preview時(shí)可以使用模擬器設(shè)備進(jìn)行預(yù)覽饵婆,填寫設(shè)計(jì)稿尺寸,換算好屏幕尺寸即進(jìn)行預(yù)覽戏售。
同時(shí)建議多使用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ù)保留炉奴。
橫豎屏切換時(shí)會以當(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
如有轉(zhuǎn)載,請注明出處:http://blog.csdn.net/hjhrq1991