除去attr包以外,我们一起看下哪些我们说过
Paste_Image.png
我们一起回顾下
LinearLayout -> AutoLinearLayout
RelativeLayout -> AutoRelativeLayout
FrameLayout -> AutoFrameLayout
除去attr包以外,哪些还没有用过
Paste_Image.png
public class L
{
public static boolean debug = false;
private static final String TAG = "AUTO_LAYOUT";
public static void e(String msg)
{
if (debug)
{
Log.e(TAG, msg);
}
}
}
public interface UseLandscape
{
}
public class AutoUtils{
public static void auto(View view)
{
autoSize(view);
autoPadding(view);
autoMargin(view);
}
public static void autoMargin(View view)
{
if (!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
return;
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
if (lp == null) return;
Object tag = view.getTag(R.id.id_tag_autolayout_margin);
if (tag != null) return;
view.setTag(R.id.id_tag_autolayout_margin, "Just Identify");
lp.leftMargin = getPercentWidthSize(lp.leftMargin);
lp.topMargin = getPercentHeightSize(lp.topMargin);
lp.rightMargin = getPercentWidthSize(lp.rightMargin);
lp.bottomMargin = getPercentHeightSize(lp.bottomMargin);
}
public static void autoPadding(View view)
{
Object tag = view.getTag(R.id.id_tag_autolayout_padding);
if (tag != null) return;
view.setTag(R.id.id_tag_autolayout_padding, "Just Identify");
int l = view.getPaddingLeft();
int t = view.getPaddingTop();
int r = view.getPaddingRight();
int b = view.getPaddingBottom();
l = getPercentWidthSize(l);
t = getPercentHeightSize(t);
r = getPercentWidthSize(r);
b = getPercentHeightSize(b);
view.setPadding(l, t, r, b);
}
public static void autoSize(View view)
{
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp == null) return;
Object tag = view.getTag(R.id.id_tag_autolayout_size);
if (tag != null) return;
view.setTag(R.id.id_tag_autolayout_size, "Just Identify");
if (lp.width > 0)
{
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
lp.width = (int) (lp.width * 1.0f / designWidth * screenWidth);
}
if (lp.height > 0)
{
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
lp.height = (int) (lp.height * 1.0f / designHeight * screenHeight);
}
}
public static int getPercentWidthSize(int val)
{
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
return (int) (val * 1.0f / designWidth * screenWidth);
}
public static int getPercentWidthSizeBigger(int val)
{
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
int res = val * screenWidth;
if (res % designWidth == 0)
{
return res / designWidth;
} else
{
return res / designWidth + 1;
}
}
public static int getPercentHeightSizeBigger(int val)
{
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
int res = val * screenHeight;
if (res % designHeight == 0)
{
return res / designHeight;
} else
{
return res / designHeight + 1;
}
}
public static int getPercentHeightSize(int val)
{
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
return (int) (val * 1.0f / designHeight * screenHeight);
}
}
我们发现都是静态方法,应该就只是一个帮助类了
public class MetroLayout extends ViewGroup
{
private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
private static class MetroBlock
{
int left;
int top;
int width;
}
private List<MetroBlock> mAvailablePos = new ArrayList<>();
private int mDivider;
public MetroLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MetroLayout);
mDivider = a.getDimensionPixelOffset(R.styleable.MetroLayout_metro_divider, 0);
mDivider = AutoUtils.getPercentWidthSizeBigger(mDivider);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (true)
randomColor();
if (!isInEditMode())
mHelper.adjustChildren();
measureChildren(widthMeasureSpec, heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void randomColor()
{
Random r = new Random(255);
for (int i = 0, n = getChildCount(); i < n; i++)
{
View v = getChildAt(i);
v.setBackgroundColor(Color.argb(100, r.nextInt(), r.nextInt(), r.nextInt()));
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
initAvailablePosition();
int left = 0;
int top = 0;
int divider = mDivider;
for (int i = 0, n = getChildCount(); i < n; i++)
{
View v = getChildAt(i);
if (v.getVisibility() == View.GONE) continue;
MetroBlock newPos = findAvailablePos(v);
left = newPos.left;
top = newPos.top;
int childWidth = v.getMeasuredWidth();
int childHeight = v.getMeasuredHeight();
int right = left + childWidth;
int bottom = top + childHeight;
v.layout(left, top, right, bottom);
if (childWidth + divider < newPos.width)
{
newPos.left += childWidth + divider;
newPos.width -= childWidth + divider;
} else
{
mAvailablePos.remove(newPos);
}
MetroBlock p = new MetroBlock();
p.left = left;
p.top = bottom + divider;
p.width = childWidth;
mAvailablePos.add(p);
mergeAvailablePosition();
}
}
private void mergeAvailablePosition()
{
if (mAvailablePos.size() <= 1) return;
List<MetroBlock> needRemoveBlocks = new ArrayList<>();
MetroBlock one = mAvailablePos.get(0);
MetroBlock two = mAvailablePos.get(1);
for (int i = 1, n = mAvailablePos.size(); i < n - 1; i++)
{
if (one.top == two.top)
{
one.width = one.width + two.width;
needRemoveBlocks.add(one);
two.left = one.left;
two = mAvailablePos.get(i + 1);
} else
{
one = mAvailablePos.get(i);
two = mAvailablePos.get(i + 1);
}
}
mAvailablePos.removeAll(needRemoveBlocks);
}
private void initAvailablePosition()
{
mAvailablePos.clear();
MetroBlock first = new MetroBlock();
first.left = getPaddingLeft();
first.top = getPaddingTop();
first.width = getMeasuredWidth();
mAvailablePos.add(first);
}
private MetroBlock findAvailablePos(View view)
{
MetroBlock p = new MetroBlock();
if (mAvailablePos.size() == 0)
{
p.left = getPaddingLeft();
p.top = getPaddingTop();
p.width = getMeasuredWidth();
return p;
}
int min = mAvailablePos.get(0).top;
MetroBlock minHeightPos = mAvailablePos.get(0);
for (MetroBlock _p : mAvailablePos)
{
if (_p.top < min)
{
min = _p.top;
minHeightPos = _p;
}
}
return minHeightPos;
}
@Override
public MetroLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
{
return new LayoutParams(getContext(), attrs);
}
public static class LayoutParams extends ViewGroup.MarginLayoutParams
implements AutoLayoutHelper.AutoLayoutParams
{
private AutoLayoutInfo mAutoLayoutInfo;
public LayoutParams(Context c, AttributeSet attrs)
{
super(c, attrs);
mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
}
public LayoutParams(int width, int height)
{
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams source)
{
super(source);
}
public LayoutParams(MarginLayoutParams source)
{
super(source);
}
public LayoutParams(LayoutParams source)
{
this((ViewGroup.LayoutParams) source);
mAutoLayoutInfo = source.mAutoLayoutInfo;
}
@Override
public AutoLayoutInfo getAutoLayoutInfo()
{
return mAutoLayoutInfo;
}
}
}
也和前面的AutoXXXLayout类似, 都只是通过 关联和依赖 AutoLayoutHelper , Override对应的 onMeasure(int widthMeasureSpec, int heightMeasureSpec) 和 generateLayoutParams(AttributeSet attrs) 通过静态内部类 的接口, 传递 AutoLayoutHelper的实现 返回 AutoLayoutInfo 对象 这里没有使用,所以暂时不考虑
这里比较复杂,
Paste_Image.png
我们分别看一下这里面的接口和方法
public interface AutoLayoutParams
{
AutoLayoutInfo getAutoLayoutInfo();
}
上一篇我们看到对应的几种AutoXXXLayout,静态内部类LayoutParams,都会实现这个接口 对应的实现,就是 静态方法 getAutoLayoutInfo(Context context,AttributeSet attrs)
private static AutoLayoutConifg mAutoLayoutConifg;
public AutoLayoutHelper(ViewGroup host)
{
mHost = host;
if (mAutoLayoutConifg == null)
{
initAutoLayoutConfig(host);
}
}
private void initAutoLayoutConfig(ViewGroup host)
{
mAutoLayoutConifg = AutoLayoutConifg.getInstance();
mAutoLayoutConifg.init(host.getContext());
}
这里,可以发现,构造只是 传递ViewGroup对象, 和 调用initAutoLayoutConfig方法,初始化 懒汉单例的AutoLayoutConifg对象
在AutoXXXLayout中 都只是通过 关联和依赖 这里的AutoLayoutHelper onMeasure(int widthMeasureSpec, int heightMeasureSpec) 中 用到了 mHelper.adjustChildren()
generateLayoutParams(AttributeSet attrs)中 调用的静态内部类,接口的实现,其实是构造中 mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs) 实现的 这里,我们分别看下这2个方法
public static AutoLayoutInfo getAutoLayoutInfo(Context context,AttributeSet attrs)
{
AutoLayoutInfo info = new AutoLayoutInfo();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoLayout_Layout);
int baseWidth = a.getInt(R.styleable.AutoLayout_Layout_layout_auto_basewidth, 0);
int baseHeight = a.getInt(R.styleable.AutoLayout_Layout_layout_auto_baseheight, 0);
a.recycle();
TypedArray array = context.obtainStyledAttributes(attrs, LL);
int n = array.getIndexCount();
for (int i = 0; i < n; i++)
{
int index = array.getIndex(i);
// String val = array.getString(index);
// if (!isPxVal(val)) continue;
if (!isPxVal(array.peekValue(index))) continue;
int pxVal = 0;
try
{
pxVal = array.getDimensionPixelOffset(index, 0);
} catch (Exception ignore)//not dimension
{
continue;
}
switch (index)
{
case INDEX_TEXT_SIZE:
info.addAttr(new TextSizeAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_PADDING:
info.addAttr(new PaddingAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_PADDING_LEFT:
info.addAttr(new PaddingLeftAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_PADDING_TOP:
info.addAttr(new PaddingTopAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_PADDING_RIGHT:
info.addAttr(new PaddingRightAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_PADDING_BOTTOM:
info.addAttr(new PaddingBottomAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_WIDTH:
info.addAttr(new WidthAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_HEIGHT:
info.addAttr(new HeightAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MARGIN:
info.addAttr(new MarginAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MARGIN_LEFT:
info.addAttr(new MarginLeftAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MARGIN_TOP:
info.addAttr(new MarginTopAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MARGIN_RIGHT:
info.addAttr(new MarginRightAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MARGIN_BOTTOM:
info.addAttr(new MarginBottomAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MAX_WIDTH:
info.addAttr(new MaxWidthAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MAX_HEIGHT:
info.addAttr(new MaxHeightAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MIN_WIDTH:
info.addAttr(new MinWidthAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MIN_HEIGHT:
info.addAttr(new MinHeightAttr(pxVal, baseWidth, baseHeight));
break;
}
}
array.recycle();
L.e(" getAutoLayoutInfo " + info.toString());
return info;
}
这里类中定义了很多静态的int值,用于switch, 也有定义的LL静态数组,对应android中常用的属性
Paste_Image.png
这里获得这些常用属性以后,for循环会通过int的静态值,switch对应的地方 在添加到 容器类 AutoLayoutInfo 的对象中 (也就是 前面提到的接口实现返回的 AutoLayoutInfo 对象) 这里,每个switch,对应的一个 Attr 属性类, 存放在 attr 包中(之前分析单独忽略的那个package) 具体逻辑,之后再一起分析
public void adjustChildren()
{
AutoLayoutConifg.getInstance().checkParams();
for (int i = 0, n = mHost.getChildCount(); i < n; i++)
{
View view = mHost.getChildAt(i);
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params instanceof AutoLayoutParams)
{
AutoLayoutInfo info =
((AutoLayoutParams) params).getAutoLayoutInfo();
if (info != null)
{
info.fillAttrs(view);
}
}
}
}
这个类,相对于上一个方法,简单一点 这里会得到对应ViewGroup的 子View 个数, for循环获取每个 子View的 LayoutParams 如果 params instanceof AutoLayoutParams 也就是说, 是我们前面实现的 接口类型的时候 转型获取前面接口实现的 AutoLayoutInfo 对象 (就是 上一个 getAutoLayoutInfo(Context context,AttributeSet attrs) 返回的对象) 最后 遍历 每个子View的 AutoLayoutInfo中每个常用属性