[关闭]
@linux1s1s 2015-05-20T08:34:18.000000Z 字数 6087 阅读 1962

Android 自定义ViewGroup

AndroidView


在展开这个话题之前,先回答几个问题

如果你能够完整回答上面的问题,那么恭喜你,你可以直接无视这篇博客了,反之,你需要花点时间看一下此博客以期回答上面提出的三个问题。

View 测量Mode

TIPS:上面的每一行都有一个一般,意思上述不是绝对的,对于childView的mode的设置还会和ViewGroup的测量mode有一定的关系;当然了,这是第一篇自定义ViewGroup,而且绝大部分情况都是上面的规则,所以为了通俗易懂,暂不深入讨论其他内容。

View 绘制流程

我们知道一般的View绘制流程如下:

View根据ViewGroup传人的测量值和模式,对自己宽高进行确定(onMeasure中完成),然后在onDraw中完成对自己的绘制。
ViewGroup需要给View传入view的测量值和模式(onMeasure中完成),而且对于此ViewGroup的父布局,自己也需要在onMeasure中完成对自己宽和高的确定。此外,需要在onLayout中完成对其childView的位置的指定。

Case

新建类CustomViewGroup继承自ViewGroup

重写generateLayoutParams方法

重写父类的该方法,返回MarginLayoutParams的实例,这样就为我们的ViewGroup指定了其LayoutParams为MarginLayoutParams。

  1. @Override
  2. public LayoutParams generateLayoutParams(AttributeSet attrs)
  3. {
  4. return new MarginLayoutParams(getContext(), attrs);
  5. }

重写onMeasure方法

在onMeasure中计算childView的测量值以及模式,以及设置自己的宽和高:

  1. @Override
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  3. {
  4. /**
  5. * 获得此ViewGroup上级容器为其推荐的宽和高,以及计算模式
  6. */
  7. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  8. int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  9. int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
  10. int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
  11. // 计算出所有的childView的宽和高
  12. measureChildren(widthMeasureSpec, heightMeasureSpec);
  13. /**
  14. * 记录如果是wrap_content是设置的宽和高
  15. */
  16. int width = 0;
  17. int height = 0;
  18. int cCount = getChildCount();
  19. int cWidth = 0;
  20. int cHeight = 0;
  21. MarginLayoutParams cParams = null;
  22. // 用于计算左边两个childView的高度
  23. int lHeight = 0;
  24. // 用于计算右边两个childView的高度,最终高度取二者之间大值
  25. int rHeight = 0;
  26. // 用于计算上边两个childView的宽度
  27. int tWidth = 0;
  28. // 用于计算下面两个childiew的宽度,最终宽度取二者之间大值
  29. int bWidth = 0;
  30. /**
  31. * 根据childView计算的出的宽和高,以及设置的margin计算容器的宽和高,主要用于容器是warp_content时
  32. */
  33. for (int i = 0; i < cCount; i++)
  34. {
  35. View childView = getChildAt(i);
  36. cWidth = childView.getMeasuredWidth();
  37. cHeight = childView.getMeasuredHeight();
  38. cParams = (MarginLayoutParams) childView.getLayoutParams();
  39. // 上面两个childView
  40. if (i == 0 || i == 1)
  41. {
  42. tWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
  43. }
  44. if (i == 2 || i == 3)
  45. {
  46. bWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
  47. }
  48. if (i == 0 || i == 2)
  49. {
  50. lHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
  51. }
  52. if (i == 1 || i == 3)
  53. {
  54. rHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
  55. }
  56. }
  57. width = Math.max(tWidth, bWidth);
  58. height = Math.max(lHeight, rHeight);
  59. /**
  60. * 如果是wrap_content设置为我们计算的值
  61. * 否则:直接设置为父容器计算的值
  62. */
  63. setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth
  64. : width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight
  65. : height);
  66. }
  • 7-10行,获取该ViewGroup父容器为其设置的计算模式和尺寸,大多情况下,只要不是wrap_content,父容器都能正确的计算其尺寸。所以我们自己需要计算如果设置为wrap_content时的宽和高,如何计算呢?那就是通过其childView的宽和高来进行计算。
  • 13行,通过ViewGroup的measureChildren方法为其所有的孩子设置宽和高,此行执行完成后,childView的宽和高都已经正确的计算过了
  • 39-68行,根据childView的宽和高,以及margin,计算ViewGroup在wrap_content时的宽和高。
  • 77-79行,如果宽高属性值为wrap_content,则设置为43-71行中计算的值,否则为其父容器传入的宽和高。

重写onLayout方法

onLayout对其所有childView进行定位(设置childView的绘制区域)

  1. @Override
  2. protected void onLayout(boolean changed, int l, int t, int r, int b)
  3. {
  4. int cCount = getChildCount();
  5. int cWidth = 0;
  6. int cHeight = 0;
  7. MarginLayoutParams cParams = null;
  8. for (int i = 0; i < cCount; i++)
  9. {
  10. View childView = getChildAt(i);
  11. cWidth = childView.getMeasuredWidth();
  12. cHeight = childView.getMeasuredHeight();
  13. cParams = (MarginLayoutParams) childView.getLayoutParams();
  14. int cl = 0, ct = 0, cr = 0, cb = 0;
  15. switch (i)
  16. {
  17. case 0:
  18. cl = cParams.leftMargin;
  19. ct = cParams.topMargin;
  20. break;
  21. case 1:
  22. cl = getWidth() - cWidth - cParams.rightMargin;
  23. ct = cParams.topMargin;
  24. break;
  25. case 2:
  26. cl = cParams.leftMargin;
  27. ct = getHeight() - cHeight - cParams.bottomMargin;
  28. break;
  29. case 3:
  30. cl = getWidth() - cWidth - cParams.rightMargin;
  31. ct = getHeight() - cHeight - cParams.bottomMargin;
  32. break;
  33. }
  34. cr = cl + cWidth;
  35. cb = cHeight + ct;
  36. childView.layout(cl, ct, cr, cb);
  37. }
  38. }

这里说一下和原文不一样的地方,对于cl的计算原文如下:
cl = getWidth() - cWidth - cParams.leftMargin- cParams.rightMargin;
这里纠正为:
cl = getWidth() - cWidth - cParams.rightMargin;

  • 代码比较容易懂:遍历所有的childView,根据childView的宽和高以及margin,然后分别将0,1,2,3位置的childView依次设置到左上、右上、左下、右下的位置。
    如果是第一个View(index=0) :则childView.layout(cl, ct, cr, cb); cl为childView的leftMargin , ct 为topMargin , cr 为cl+ cWidth , cb为 ct + cHeight
  • 如果是第二个View(index=1) :则childView.layout(cl, ct, cr, cb);
  • cl为getWidth() - cWidth - cParams.rightMargin;
  • ct 为topMargin , cr 为cl+ cWidth , cb为 ct + cHeight

TestCase

  1. <com.example.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="200dp"
  4. android:layout_height="200dp"
  5. android:background="#AA333333" >
  6. <TextView
  7. android:layout_width="50dp"
  8. android:layout_height="50dp"
  9. android:background="#FF4444"
  10. android:gravity="center"
  11. android:text="0"
  12. android:textColor="#FFFFFF"
  13. android:textSize="22sp"
  14. android:textStyle="bold" />
  15. <TextView
  16. android:layout_width="50dp"
  17. android:layout_height="50dp"
  18. android:background="#00ff00"
  19. android:gravity="center"
  20. android:text="1"
  21. android:textColor="#FFFFFF"
  22. android:textSize="22sp"
  23. android:textStyle="bold" />
  24. <TextView
  25. android:layout_width="50dp"
  26. android:layout_height="50dp"
  27. android:background="#ff0000"
  28. android:gravity="center"
  29. android:text="2"
  30. android:textColor="#FFFFFF"
  31. android:textSize="22sp"
  32. android:textStyle="bold" />
  33. <TextView
  34. android:layout_width="50dp"
  35. android:layout_height="50dp"
  36. android:background="#0000ff"
  37. android:gravity="center"
  38. android:text="3"
  39. android:textColor="#FFFFFF"
  40. android:textSize="22sp"
  41. android:textStyle="bold" />
  42. </com.example.zhy_custom_viewgroup.CustomImgContainer>

ViewGroup宽和高设置为固定值

此处输入图片的描述

其他几种情况可以自行测试。

通过上面的Demo,我们对前面提及的三个问题有了形象的认识,那么来具体回答一下上面的三个问题吧:

此篇博客转载自CSDN-HongYang的博客,在此向该大牛致敬

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注