闲谈自定义控件源码-view layout draw

    onMeasure
   onMeasure会先测量父view根据父view的measureSpecSize测量出子view,由上向下的传递。onLayout一样先布局父view然后依次布局子view
    看FrameLayout的onLayout方法
    
    
    
  1. @Override
  2. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  3. layoutChildren(left, top, right, bottom, false /* no force left gravity */);
  4. }
调用layoutChildren方法
     
     
  1. void layoutChildren(int left, int top, int right, int bottom,
  2. boolean forceLeftGravity) {
  3. final int count = getChildCount();
  4. final int parentLeft = getPaddingLeftWithForeground();
  5. final int parentRight = right - left - getPaddingRightWithForeground();
  6. final int parentTop = getPaddingTopWithForeground();
  7. final int parentBottom = bottom - top - getPaddingBottomWithForeground();
  8. for (int i = 0; i < count; i++) {
  9. final View child = getChildAt(i);
  10. if (child.getVisibility() != GONE) {
  11. final LayoutParams lp = (LayoutParams) child.getLayoutParams();
  12. final int width = child.getMeasuredWidth();
  13. final int height = child.getMeasuredHeight();
  14. int childLeft;
  15. int childTop;
  16. ...
  17. switch (verticalGravity) {
  18. case Gravity.TOP:
  19. childTop = parentTop + lp.topMargin;
  20. break;
  21. case Gravity.CENTER_VERTICAL:
  22. childTop = parentTop + (parentBottom - parentTop - height) / 2 +
  23. lp.topMargin - lp.bottomMargin;
  24. break;
  25. case Gravity.BOTTOM:
  26. childTop = parentBottom - height - lp.bottomMargin;
  27. break;
  28. default:
  29. childTop = parentTop + lp.topMargin;
  30. }
  31. child.layout(childLeft, childTop, childLeft + width, childTop + height);
  32. }
根据child的layoutparam和gravity计算childTop和childLeft,然后调用child.layout()-->child.onLayout()布局child。一般viewGroup需要重写onLayout和onMeasure,view很少会需要重写。
   ondraw
ondraw算是源码中最简单的方法了,只是传过来一个canvas,我们通过canvas来绘制。但是有算是最复杂的方法,因为在自定义一个view的时候更多的去重写ondraw方法。会重新写关于ondraw方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值