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

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?

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

示例

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


設(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)行時(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

Apache 2.0

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末阔墩,一起剝皮案震驚了整個(gè)濱河市嘿架,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌啸箫,老刑警劉巖耸彪,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異忘苛,居然都是意外死亡蝉娜,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門扎唾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來召川,“玉大人,你說我怎么就攤上這事稽屏“绯瑁” “怎么了?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵狐榔,是天一觀的道長坛增。 經(jīng)常有香客問我,道長薄腻,這世上最難降的妖魔是什么收捣? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮庵楷,結(jié)果婚禮上罢艾,老公的妹妹穿的比我還像新娘。我一直安慰自己尽纽,他們只是感情好咐蚯,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著弄贿,像睡著了一般春锋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上差凹,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天期奔,我揣著相機(jī)與錄音侧馅,去河邊找鬼。 笑死呐萌,一個(gè)胖子當(dāng)著我的面吹牛馁痴,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播肺孤,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼罗晕,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了赠堵?” 一聲冷哼從身側(cè)響起攀例,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎顾腊,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挖胃,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡杂靶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了酱鸭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吗垮。...
    茶點(diǎn)故事閱讀 40,110評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖凹髓,靈堂內(nèi)的尸體忽然破棺而出烁登,到底是詐尸還是另有隱情,我是刑警寧澤蔚舀,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布饵沧,位于F島的核電站,受9級特大地震影響赌躺,放射性物質(zhì)發(fā)生泄漏狼牺。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一礼患、第九天 我趴在偏房一處隱蔽的房頂上張望是钥。 院中可真熱鬧,春花似錦缅叠、人聲如沸悄泥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽弹囚。三九已至,卻和暖如春狼犯,著一層夾襖步出監(jiān)牢的瞬間余寥,已是汗流浹背领铐。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留宋舷,地道東北人绪撵。 一個(gè)月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像祝蝠,于是被迫代替她去往敵國和親音诈。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容