1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android m新控件 Android自定义控件(十一)mdash;mdash;自定义ViewGroup实现LinearLayout

android m新控件 Android自定义控件(十一)mdash;mdash;自定义ViewGroup实现LinearLayout

时间:2019-11-30 12:24:45

相关推荐

android m新控件 Android自定义控件(十一)mdash;mdash;自定义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

//测量子控件

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

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代码如下:

xmlns:tools="/tools"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@android:color/holo_red_dark"

tools:context=".MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="我是第一个子控件"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="我是第二个子控件"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="我是第三个子控件(但我加长了)"/>

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。