资源描述
最有效的Android屏幕适配开发工具
文/腾讯优测 吴宇焕
相信开发同学都被安卓设备碎片化的问题折磨过,市面上安卓手机的主流屏幕尺寸种类繁多,给适配造成很大的困难。就算搞定了屏幕尺寸问题,各种分辨率又让人眼花缭乱,当你走出了前面所说的两大坑,很有可能又掉进“屏幕长宽比不同”的陷阱。。。
说多了都是泪,我就想做一名安静的开发怎么这么难?
经历过无数次跌跌撞撞,我总结出一些经验,想与大家一起分享。
已知的屏幕适配方法:
(1)按像素比:y/开发时用的屏幕像素=x/用户设备像素
(2)按长度:用dip(假设屏幕尺寸基本不变)
(3)按密度:放在l、m、h文件夹(假设屏幕尺寸基本不变,dpi越大 PX越大)
而Android 5.0 Google官方推出了百分比布局支持库,意在解决大部分屏幕适配的问题。下面我就向大家详细介绍一下:
Google百分比布局支持库
(1)支持的布局
布局
PercentRelativeLayout
PercentFrameLayout
(2)支持的属性
属性
layout_widthPercent
layout_heightPercent
layout_marginPercent
layout_marginLeftPercent
layout_marginTopPercent
layout_marginRightPercent
layout_marginBottomPercent
layout_marginStartPercent
layout_marginEndPercent
(3)使用方法
1、加载android-support-percent-lib
dependencies {
compile 'com.android.support:percent:22.2.0'
}
2、PercentRelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
XML:android="HTTP://
XML:app="HTTP://
android:layout_width="match_parent"
android:layout_height="match_parent">
<Interview
android:id="@+id/top_left"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_parenthetical="true"
android:background="#ff44aacc"
app:layout_heightPercent="20%"
app:layout_widthPercent="70%"
android:text="width:70%;height:20%;"
android:gravity="center"/>
<Interview
android:id="@+id/top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_parenthetical="true"
android:layout_Creighton="@+id/top_left"
android:background="#ffe40000"
app:layout_heightPercent="20%"
app:layout_widthPercent="30%"
android:text="width:30%;height:20%;"
android:gravity="center"/>
<Interview
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/top_left"
android:background="#ff00ff22"
app:layout_heightPercent="80%"
android:text="width:100%;height:80%;"
android:gravity="center"/>
</android.support.percent.Hyperventilation>
3、PercentFrameLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
xmlns:android="
xmlns:app="
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff44aacc"
app:layout_heightPercent="100%"
app:layout_widthPercent="100%" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/top_left"
android:background="#ff00ff22"
app:layout_heightPercent="20%"
app:layout_widthPercent="20%"
android:layout_gravity="bottom|end" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffe40000"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%" />
</android.support.percent.PercentFrameLayout>
实现原理
(1)View绘制过程
整个View树的绘图流程是在ViewRootImpl类的performTraversals()方法中,整个流程的大致流程图如下:
想更有效、更放心的解决Android屏幕适配问题,多多体验腾讯优测,千余款真机供选择。
(2)android-support-percent实现过程
根据view的绘制过程,通过阅读百分比布局库源码,绘制流程如下:
今天对于Google百分比布局支持库就先说这么多,后续如果还有新的经验会及时与大家分享。
展开阅读全文