鸿神的AutoLayout堪称适配终结者,虽然项目已经不再维护,对于一些好的框架和实现原理我们还是有学习意义的!现在让我们一起来学习AutoLayout的源码吧!
首先我们应该思考这样一个问题:对于一个框架如何去学习呢?
难道是下载源码后,一个类一个类的去看吗? 这肯定不行,这样会浪费大量的时间,而且还不一定能很好的理解他
正确的流程应该是从如何去使用入手,然后根据调用流程一步一步的去理解,这样就能很好的理解框架的整个流程了!
现在让我们来看一下AutoLayout的使用流程:
1、将Autolayout添加到我们的项目依赖中
dependencies {
// 其他依赖
// ...
implementation 'com.zhy:autolayout:1.4.5'
}
2、在AndroidManifest.xml标明设计稿尺寸
3、在Application中进行初始化
AutoLayoutConifg.getInstance().useDeviceSize().init(this);
4、让你的Activity继承AutoLayoutActivity或者直接在xml使用AutoLinearLayout、AutoRelativeLayout、AutoFrameLayout
首先出现在我们眼前的有AutoLayoutConifg、AutoLayoutActivity、AutoLinearLayout、AutoRelativeLayout、AutoFrameLayout这些类
那我们就按顺序分别去看这些类做了那些事情
1、AutoLayoutConifg
首先这是一个单例类
private static AutoLayoutConifg sIntance = new AutoLayoutConifg();
private AutoLayoutConifg()
{
}
public static AutoLayoutConifg getInstance()
{
return sIntance;
}
它有这些属性
// 屏幕的宽度 private int mScreenWidth; // 屏幕的高度 private int mScreenHeight; // 设计图的宽度 private int mDesignWidth; // 设计图的高度 private int mDesignHeight; // 暂时不知道这个参数是啥,字面意思是使用设备大小 private boolean useDeviceSize;
接下来我们来看init里面做了啥
public void init(Context context)
{
// 通过方法名我们猜想应该是从AndroidManifest读取MetaData里面的设计尺寸
getMetaData(context);
// 这里是获取实际屏幕的大小,传入了useDeviceSize属性
int[] screenSize = ScreenUtils.getScreenSize(context, useDeviceSize);
// 给mScreenWidth赋值
mScreenWidth = screenSize[0];
// 给mScreenHeight赋值
mScreenHeight = screenSize[1];
L.e(" screenWidth =" + mScreenWidth + " ,screenHeight = " + mScreenHeight);
}
接下来我们来看getMetaData方法里的代码
private void getMetaData(Context context)
{
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo;
try
{
applicationInfo = packageManager.getApplicationInfo(context
.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null && applicationInfo.metaData != null)
{
// 给mDesignWidth赋值
mDesignWidth = (int) applicationInfo.metaData.get(KEY_DESIGN_WIDTH);
// 给mDesignHeight赋值
mDesignHeight = (int) applicationInfo.metaData.get(KEY_DESIGN_HEIGHT);
}
} catch (PackageManager.NameNotFoundException e)
{
throw new RuntimeException(
"you must set " + KEY_DESIGN_WIDTH + " and " + KEY_DESIGN_HEIGHT + " in your manifest file.", e);
}
L.e(" designWidth =" + mDesignWidth + " , designHeight = " + mDesignHeight);
}
这里印证了我们的猜想:这个方法就是从AndroidManifest读取MetaData里面的设计尺寸,然后赋值给属性[mDesignWidth、mDesignHeight]
接下来我们看 ScreenUtils.getScreenSize(context, useDeviceSize)方法是如何获取真实的屏幕大小,顺便确定useDeviceSize属性的意思
int[] size = new int[2];
// 这部分的代码是获取屏幕宽高的代码,有多种写法,写法也比较固定
WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
// since SDK_INT = 1;
if (!useDeviceSize)
{
size[0] = widthPixels;
size[1] = heightPixels - getStatusBarHeight(context);
return size;
}
我们看红色注释部分,可以看到从SDK1开始,只有useDeviceSize为false才会走进来
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
try
{
widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored)
{
}
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17)
try
{
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d,realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
} catch (Exception ignored)
{
}
size[0] = widthPixels;
size[1] = heightPixels;
return size;
可以看到接下来部分的代码都不同版本的适配代码,所以我们可以这样理解useDeviceSize:根据版本来适配获取屏幕真实宽高的,所以当我们项目的最低版本大于等于14时,应该在Application里面去将useDeviceSize配置为true。因为现在构建工程的最低版本基本都是19或21了,所以应该将useDeviceSize设置为true。
那么如果我们不设置会出现什么问题呢?
通过DisplayMetrics获取到的heightPixels的值在高版本中是已经减去了状态栏高度的,而代码里面又减了一次状态栏高度,会导致减去了2次状态栏的高度,从而导致适配的缩放比例存在误差
其实看完这个类如果我们善于思考的话,已经能够猜到它的大致原理了
比如设计图上有一个384px*640px的View,设计图是768x1280,我们的手机分辨率是1440x3168,所以:
mScreenWidth = 1440; mScreenHeight = 3168; mDesignWidth = 768; mDesignHeight = 1280;
可以看到设计图上这个View占用了屏幕的4分之1,我们要在1440x3168的屏幕上也显示4分之1,怎么办呢?
我们用1440*384/760 = 720,3168*640/1280 = 1584,所以我们想要在1440x3168的屏幕上显示相同的效果这个控件的大小应该是720px*1584px,目前大致能想到是就这么多了,接下来我们继续看源码是如何实现的!



