RatioLayout一个屏幕适配三方库源码分析
使用dp进行UI设置:
可以看出,在不同分辨率或者密度的屏幕,相同dp值设置的效果是不同的。
使用ratio进行UI设置(按比例设置):
可以看出,在不同分辨率或者密度的屏幕,ratio按比例设置的效果是比较出色的。
简单分析RatioLayout:
1,初始化一些参数,设置数据,为了在xml中设置属性到自定义布局中使用
2,在系统测量之前手动测量一次,获取到参考值(屏幕最大宽度或者最大高度),根据参考值与比例ratio计算出高度或者宽度
3,生成测量规格,将新的测量规格传递给系统onMeasure()方法中,进行测量。
RatioLayout源码分析:
RatioLayoutDelegate 核心类,用于计算出ratio比例的实际宽高的测量规格
public final class RatioLayoutDelegate<TARGET extends View & RatioMeasureDelegate> {
public static <TARGET extends View & RatioMeasureDelegate> RatioLayoutDelegate obtain(TARGET target, AttributeSet attrs) {
return obtain(target,attrs,0);
}
public static <TARGET extends View & RatioMeasureDelegate> RatioLayoutDelegate obtain(TARGET target, AttributeSet attrs, int defStyleAttr) {
return obtain(target,attrs,0,0);
}
@SuppressWarnings("unchecked")
public static <TARGET extends View & RatioMeasureDelegate> RatioLayoutDelegate obtain(TARGET target, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
return new RatioLayoutDelegate(target,attrs,defStyleAttr,defStyleRes);
}
private