Android —— 自定义View中,你应该知道的知识点(1)

本文详细解释了AndroidView的测量过程,特别是MeasureSpec的作用、不同测量模式,以及如何自定义FlowLayout的onMeasure和onLayout方法。重点介绍了MeasureSpec在View测量中的关键作用和UNSPECIFIED的特殊用途。
摘要由CSDN通过智能技术生成

View怎么测量大小?


View通过measure来确定大小 measure的作用就是决定View到底有多大。在整个View树种是由View和ViewGroup组成。而measure也分为着两种绘制方式。View的measure只测试自身大小。ViewGroup除了测量自身大小,还负责测量子View的大小。

MeasureSpec的作用

MeasureSpec封装了View的规格尺寸参数,包括View的宽高以及测量模式。

它的高2位代表测量模式(通过mode & MODE_MASK计算),低30位代表尺寸。其中测量模式总共有3中。

  • UNSPECIFIED:未指定模式不对子View的尺寸进行限制。

  • AT_MOST:最大模式对应于wrap_content属性,父容器已经确定子View的大小,并且子View不能大于这个值。

  • EXACTLY:精确模式对应于match_parent属性和具体的数值,子View可以达到父容器指定大小的值。

对于每一个View,都会有一个MeasureSpec属性来保存View的尺寸规格信息。在View测量的时候,通过makeMeasureSpec来保存宽高信息,通过getMode获取测量模式,通过getSize获取宽或高。

MeasureSpec是如何产生的

MeasureSpec相当于View测量过程中的一个规格,在View开始测量前需要先生成MeasureSpec来指导View以何种方式测量。

MeasureSpec生成是由父布局决定的,同时对于顶级ViewDecorView来说是由LayoutParams决定的。

在上面分析View工作流程开始的时候,在ViewRootImpl中开始工作流程前,有一个方法measureHierarchy(),这个方法就是生成DecorView的方式。

private boolean measureHierarchy(final View host, final WindowManager.LayoutParams lp,

final Resources res, final int desiredWindowWidth, final int desiredWindowHeight) {

if (baseSize != 0 && desiredWindowWidth > baseSize) {

childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);

childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);

}

}

在代码中可以看到通过getRootMeasureSpec()方法获取了DecorView的MeasureSpec。

private static int getRootMeasureSpec(int windowSize, int rootDimension) {

int measureSpec;

switch (rootDimension) {

case ViewGroup.LayoutParams.MATCH_PARENT:

// Window can’t resize. Force root view to be windowSize.

measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);

break;

case ViewGroup.LayoutParams.WRAP_CONTENT:

// Window can resize. Set max size for root view.

measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);

break;

default:

// Window wants to be an exact size. Force root view to be that size.

measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);

break;

}

return measureSpec;

}

getRootMeasureSpec()也不复杂,在方法中可以看出如果是LayoutParams.MATCH_PARENT,那么DecorView的大小就是Window的大小;如果是LayoutParams.WRAP_CONTENT,那么DecorView的大小不确定。

对于普通的View,MeasureSpec来自于父布局(ViewGroup)生成。

protected void measureChildWithMargins(View child,

int parentWidthMeasureSpec, int widthUsed,

int parentHeightMeasureSpec, int heightUsed) {

final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,

mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin

  • widthUsed, lp.width);

final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,

mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin

  • heightUsed, lp.height);

child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

}

在这里可以看到生成子View的MeasureSpec时与父布局的MeasureSpec以及padding相关,同时也与View本身的margin有关。

MeasureSpec中UNSPECIFIED的用途

UNSPECIFIED主要在一线父View不限制子View宽高的情况下使用,比如ScrollView

1.UNSPECIFIED会在ScrollView的measure方法里传给子View

2.子View收到UNSPECIFIED,会根据自己的实际内容大小来决定高度

3.UNSPECIFIED与AT_MOST的区别就是,它没有最大size限定这也说明UNSPECIFIED在ScrollView里很实用,因为ScrllView不需要限定子View的大小,它可以滚动嘛

如何自定义FlowLayout


实现自定义View主要需要解决以下3个问题

1.自定义控件的大小,也就是宽和高分别设置多少;

2.如果是 ViewGroup,如何合理安排其内部子 View 的摆放位置。

3.如何根据相应的属性将 UI 元素绘制到界面;

以上 3 个问题依次在如下 3 个方法中得到解决:

onMeasure,onLayout,onDraw

FlowLayout的onMeasure方法

因为自定义的控件是一个容器,onMeasure 方法会更加复杂一些。因为 ViewGroup 在测量自己的宽高之前,需要先确定其内部子 View 的所占大小,然后才能确定自己的大小。

如下所示:

//测量控件的宽和高

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//获得宽高的测量模式和测量值

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

//获得容器中子View的个数

int childCount = getChildCount();

//记录每一行View的总宽度

int totalLineWidth = 0;

//记录每一行最高View的高度

int perLineMaxHeight = 0;

//记录当前ViewGroup的总高度

int totalHeight = 0;

for (int i = 0; i < childCount; i++) {

View childView = getChildAt(i);

//对子View进行测量

measureChild(childView, widthMeasureSpec, heightMeasureSpec);

MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();

//获得子View的测量宽度

int childWidth = childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

//获得子View的测量高度

int childHeight = childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

if (totalLineWidth + childWidth > widthSize) {

//统计总高度

totalHeight += perLineMaxHeight;

//开启新的一行

totalLineWidth = childWidth;

perLineMaxHeight = childHeight;

} else {

//记录每一行的总宽度

totalLineWidth += childWidth;

//比较每一行最高的View

perLineMaxHeight = Math.max(perLineMaxHeight, childHeight);

}

//当该View已是最后一个View时,将该行最大高度添加到totalHeight中

if (i == childCount - 1) {

totalHeight += perLineMaxHeight;

}

}

//如果高度的测量模式是EXACTLY,则高度用测量值,否则用计算出来的总高度(这时高度的设置为wrap_content)

heightSize = heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight;

setMeasuredDimension(widthSize, heightSize);

}

上述 onMeasure 方法的主要目的有 2 个:

1.调用 measureChild 方法递归测量子 View;

2.通过叠加每一行的高度,计算出最终 FlowLayout 的最终高度 totalHeight。

FlowLayout的onLayout方法

上面的 FlowLayout 中的 onMeasure 方法只是计算出 ViewGroup 的最终显示宽高,但是并没有规定某一个子 View 应该显示在何处位置。要定义 ViewGroup 内部子 View 的显示规则,则需要复写并实现 onLayout 方法。

onLayout是一个抽象方法,也就是说每一个自定义 ViewGroup 都必须主动实现如何排布子 View,具体就是遍历每一个子 View,调用 child.(l, t, r, b) 方法来为每个子 View 设置具体的布局位置。四个参数分别代表左上右下的坐标位置,一个简易的 FlowLayout 实现如下:

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

mAllViews.clear();

mPerLineMaxHeight.clear();

//存放每一行的子View

List lineViews = new ArrayList<>();

//记录每一行已存放View的总宽度

int totalLineWidth = 0;

//记录每一行最高View的高度

int lineMaxHeight = 0;

/遍历所有View,将View添加到List<List>集合中******/

//获得子View的总个数

int childCount = getChildCount();

for (int i = 0; i < childCount; i++) {

View childView = getChildAt(i);

MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();

int childWidth = childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

int childHeight = childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

if (totalLineWidth + childWidth > getWidth()) {

mAllViews.add(lineViews);

mPerLineMaxHeight.add(lineMaxHeight);

//开启新的一行

totalLineWidth = 0;

lineMaxHeight = 0;

lineViews = new ArrayList<>();

}

totalLineWidth += childWidth;

lineViews.add(childView);

lineMaxHeight = Math.max(lineMaxHeight, childHeight);

}

//单独处理最后一行

mAllViews.add(lineViews);

mPerLineMaxHeight.add(lineMaxHeight);

/遍历集合中的所有View并显示出来/

//表示一个View和父容器左边的距离

int mLeft = 0;

//表示View和父容器顶部的距离

int mTop = 0;

for (int i = 0; i < mAllViews.size(); i++) {

//获得每一行的所有View

lineViews = mAllViews.get(i);

lineMaxHeight = mPerLineMaxHeight.get(i);

for (int j = 0; j < lineViews.size(); j++) {

View childView = lineViews.get(j);

MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();

int leftChild = mLeft + lp.leftMargin;

int topChild = mTop + lp.topMargin;

int rightChild = leftChild + childView.getMeasuredWidth();

int bottomChild = topChild + childView.getMeasuredHeight();

//四个参数分别表示View的左上角和右下角

childView.layout(leftChild, topChild, rightChild, bottomChild);

mLeft += lp.leftMargin + childView.getMeasuredWidth() + lp.rightMargin;

}

mLeft = 0;

mTop += lineMaxHeight;

}

}

一道滴滴面试题


之前在面试滴滴时碰到了这样一首题目,这个问题如果你如果理解了,相信你已经充分掌握了自定义View的measure过程

Activity内根布局LinearLayout,背景颜色为红色,宽高为wrap_content

内部包含View背影颜色为蓝色,宽高也为wrap_content

求界面颜色

<LinearLayout

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:background=“@color/red”

xmlns:android=“http://schemas.android.com/apk/res/android”>

<View

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:background=“@color/blue”

/>

答案是蓝色

在下当时想当然的认为,既然都是wrap_content,界面颜色应该是白色。但是正确答案是蓝色

下面就来分析下具体原因

LinearLayout的onMeasure()

onMeasure()中比较简单,但是这里我们需要明确一下,这个方法的参数是什么含义:

MeasureSpec就不用多说了,记录当前View的尺寸和测量模式

另外明确一点,这里的MeasureSpec是父View的

/**

  • @param widthMeasureSpec horizontal space requirements as imposed by the parent.

  • @param heightMeasureSpec vertical space requirements as imposed by the parent.

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

尾声

如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。

PS:之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

架构篇

《Jetpack全家桶打造全新Google标准架构模式》

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

样的启发、收获。[外链图片转存中…(img-pLixPbyP-1714301014960)]

PS:之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

[外链图片转存中…(img-vTi14Q7Z-1714301014960)]

架构篇

《Jetpack全家桶打造全新Google标准架构模式》
[外链图片转存中…(img-S0upaZXt-1714301014961)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 17
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值