Android自定义控件(十一)——自定义ViewGroup实现LinearLayout

简介: Android自定义控件(十一)——自定义ViewGroup实现LinearLayout

ViewGroup的绘制流程


要自定以ViewGroup,我们首先需要了解ViewGroup的绘制流程,其实View与ViewGroup绘制基本相同,只是在ViewGroup中,不仅仅要绘制自己,还要绘制其中的子控件,所以ViewGroup的绘制流程分为三步:测量,布局,绘制,分别对应onMeasure(),onLayout(),onDraw()。


1.onMeasure():测量当前控件的大小,为正式布局提供建议,注意仅仅只是建议,至于用不用看onLayout()。


2.onLayout():使用layout()函数对所有子控件进行布局。


3.onDraw():根据布局的位置绘图。


这里onDraw()就不再赘述了,前面自定义的所有View()基本都讲解过如何使用onDraw(),本博文重点介绍onMeasure()和onLayout()函数。


onMeasure()函数与MeasureSpec

首先,我们来看看onMeasure() 函数的定义:


protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec)

这里主要传进去两个参数,分别是widthMeasureSpec和heightMeasureSpec。它们是父类传递过来给当前ViewGroup的一个建议值,即想把当前ViewGroup的尺寸设置为宽widthMeasureSpec,高heightMeasureSpec。


虽然他们两个是int类型,但其实他们是由mode+size两部分组成的,转换位二进制都是32位的,前2位代表模式mode,后30位代表数值。


模式分类

既然说到mode模式,我们来看看它的三种分类:


(1)UNSPECIFIED(未指定):父元素不对子元素施加任何束缚,子元素可以得到任何想要的数值。


(2)EXACTLY(完全):父元素决定子元素的确切大小,子元素将被限定在给定的边界里而忽略它本身的大小。


(3)AT_MOST(至多):子元素至多达到指定大小的值。


既然提到了模式,很显然,我们提取模式进行判断,就需要听过与运算得到,这里Android给我们提供了一个简单的方法,直接提取:

MeasureSpec.getMode(int spec)//获取模式
MeasureSpec.getSize(int spec)//获取数值


那么代码中使用起来的代码就是这样:

int measureWidth=MeasureSpec.getSize(widthMeasureSpec)
int measureWidhtMode=MeasureSpec.getMode(widthMeasureSpec)
//heightMeasureSpec同样如此使用。


如何使用模式

我们先来看看一般我们在XML中如何定义控件的宽高的,有如下三种方式:


(1)warp_content:对应模式MeasureSpec.AT_MOST。


(2)match_parent:对应模式的MeasureSpec.EXACTLY。


(2)具体值(比如设置60px,60dp等):对应模式的MeasureSpec.EXACTLY。


我们从这里看出来,一般我们用不到MeasureSpec.UNSPECIFIED模式。但是我们这里需要注意的是,如果我们设置为MeasureSpec.EXACTLY模式,就不必设置我们计算的大小数值,因为用户已经指定,而设置为MeasureSpec.AT_MOST(warp_content)就需要我们设置具体数值。所以,我们自定义ViewGroup的onMeasure()函数一般都是这样的:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int measureWidth=MeasureSpec.getSize(widthMeasureSpec);
    int measureHeight=MeasureSpec.getSize(heightMeasureSpec);
    int measureWidhtMode=MeasureSpec.getMode(widthMeasureSpec);
    int measureHeightMode=MeasureSpec.getMode(heightMeasureSpec);
    //这里计算width,height
    setMeasuredDimension((measureWidhtMode==MeasureSpec.EXACTLY)?measureWidth:width,(measureHeightMode==MeasureSpec.EXACTLY)?measureHeight:height);
    }


如果等于MeasureSpec.EXACTLY就不需要进行计算设置,如果是MeasureSpec.AT_MOST(warp_content)就需要计算控件大小的步骤。


onLayout()函数

前面已经说过了,onLayout()函数是实现所有子控件布局的函数,需要注意的是,这里是实现所有子控件的布局,至于自己我们后面会介绍。我们先来看看onLayout()函数的定义:

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

可以看到这是一个抽象函数,说明只要你需要自定义ViewGroup,就必须实现该函数,想我们后面自定义ViewGroup实现LinearLayout一样,都需要重写这个函数,然后按照自己的规则,对子控件进行布局。


自定义ViewGroup实现LinearLayout


我们假设,我们需要自定义的ViewGroup是一个垂直布局,所以我们知道,整体控件的高就是子控件高的和,宽度就是子控件中最宽的哪个,所以我们的onMeasure()函数实现如下:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int measureWidth=MeasureSpec.getSize(widthMeasureSpec);
    int measureHeight=MeasureSpec.getSize(heightMeasureSpec);
    int measureWidhtMode=MeasureSpec.getMode(widthMeasureSpec);
    int measureHeightMode=MeasureSpec.getMode(heightMeasureSpec);
    //这里计算width,height
    int height=0;
    int width=0;
    int count=getChildCount();
    for(int i=0;i<count;i++){
        //测量子控件
        View child=getChildAt(i);
        measureChild(child,widthMeasureSpec,heightMeasureSpec);
        int childWidth=child.getMeasuredWidth();
        int childHeight=child.getMeasuredHeight();
        height+=childHeight;//高度叠加
        width=Math.max(width,childWidth);//宽度取最大
    }
    setMeasuredDimension((measureWidhtMode==MeasureSpec.EXACTLY)?measureWidth:width,(measureHeightMode==MeasureSpec.EXACTLY)?measureHeight:height);
    }


可以看到,我们实现的原理,基本与上面讲解的一致,获取子控件最宽的宽度设置为整体ViewGroup的宽度,设置ViewGroup的高度为子控件高度和。因为我们的高度宽度在XML中都设置为了warp_content,这里就需要我们自己计算。(XML代码最后)


接着,就是实现我们的onLayout()函数,因为我们说了我们是实现垂直布局的LinearLayout,所以我们需要在这个函数中布局子控件的位置。代码如下:

protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int top=0;
    int count=getChildCount();
    for(int i=0;i<count;i++){
        View child=getChildAt(i);
        int childWidth=child.getMeasuredWidth();
        int childHeight=child.getMeasuredHeight();
        child.layout(0,top,childWidth,childHeight);
        top+=childHeight;
    }
}


因为我们是垂直布局,也没有设置什么pading,margin,所以左上角坐标就只有top在变化叠加,也就是加上第一个子控件的高度就是第二个子控件的top。我们的XML代码如下:

<?xml version="1.0" encoding="utf-8"?>
<com.liyuanjinglyj.customviewgroup.MyViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_red_dark"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第一个子控件"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第二个子控件"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第三个子控件(但我加长了)"/>
</com.liyuanjinglyj.customviewgroup.MyViewGroup>


但是我们还是需要注意一下getMeasuredWidth()与getWidth()区别,getMeasuredWidth()函数在onMeasure()过程结束后,就可以获取到宽度值,而getWidth()函数要在onLayout()过程结束后才能获取到宽度值,所以我们上面都使用getMeasuredWidth()。


而且getMeasuredWidth()函数是通过setMeasuredDimension()函数进行设置的,getWidth()函数则是通过layout()函数来设置的。所以我们在前面自定义的所有View中都是在onDraw()中使用getWidth(),因为其他地方必须等onMeasure()与onLayout()指定完后才能获取到。


本文Github下载地址:点击下载

相关文章
|
1月前
|
Android开发
Android面试题之自定义View注意事项
在Android开发中,自定义View主要分为四类:直接继承View重写onDraw,继承ViewGroup创建布局,扩展特定View如TextView,以及继承特定ViewGroup。实现时需注意:支持wrap_content通过onMeasure处理,支持padding需在onDraw或onMeasure/onLayout中处理。避免在View中使用Handler,使用post系列方法代替。记得在onDetachedFromWindow时停止线程和动画以防止内存泄漏。处理滑动嵌套时解决滑动冲突,并避免在onDraw中大量创建临时对象。
24 4
|
20天前
|
机器学习/深度学习 人工智能 算法
探索AI在医疗影像分析中的应用探索安卓开发中的自定义View组件
【7月更文挑战第31天】随着人工智能技术的飞速发展,其在医疗健康领域的应用日益广泛。本文将聚焦于AI技术在医疗影像分析中的运用,探讨其如何通过深度学习模型提高诊断的准确性和效率。我们将介绍一些关键的深度学习算法,并通过实际代码示例展示这些算法是如何应用于医学影像的处理和分析中。文章旨在为读者提供对AI在医疗领域应用的深刻理解和实用知识。
22 0
|
1月前
|
前端开发 API Android开发
Android自定义View之Canvas一文搞定
这篇文章介绍了Android自定义View中如何使用Canvas和Paint来绘制图形。Canvas可理解为画布,用于绘制各种形状如文字、点、线、矩形、圆角矩形、圆和弧。常见API包括`drawText()`、`drawPoint()`、`drawLine()`、`drawRect()`等。文章还提到了Canvas的保存、恢复、平移和旋转方法,通过绘制钟表盘的例子展示了如何实际应用。总结关键点:Canvas与Paint结合用于图像绘制,掌握Canvas的基本绘图函数及坐标变换操作是自定义View的关键。
23 0
Android自定义View之Canvas一文搞定
|
1月前
|
消息中间件 前端开发 Android开发
Android面试题自定义View之Window、ViewRootImpl和View的三大流程
Android开发中,View的三大核心流程包括measure(测量)、layout(布局)和draw(绘制)。MeasureSpec类在测量过程中起到关键作用,它结合尺寸大小和模式(EXACTLY、AT_MOST、UNSPECIFIED)来指定View应如何测量。onMeasure方法用于自定义View的测量,布局阶段,ViewGroup调用onLayout确定子元素位置,而draw阶段按照特定顺序绘制背景、内容、子元素和装饰。整个流程始于ViewRootImpl的performTraversals,该方法触发测量、布局和绘制。
25 0
|
1月前
|
XML 数据格式
Android-自定义三角形评分控件
Android-自定义三角形评分控件
22 0
|
1月前
Android-自定义流布局标签
Android-自定义流布局标签
24 0
|
1月前
|
Android开发
Android自定义之高仿淘宝下拉刷新
Android自定义之高仿淘宝下拉刷新
26 0
|
1月前
|
Android开发
Android自定义之QQ身边的人
Android自定义之QQ身边的人
23 0
|
1月前
|
Android开发
Android自定义一个属于自己的刻度尺
Android自定义一个属于自己的刻度尺
30 0
|
1月前
|
Android开发
Android自定义一个属于自己的时间钟表
Android自定义一个属于自己的时间钟表
17 0