Android-自定义View2

1.viewGroup绘制流程

  总结:当继承ViewGroup的时候必须要重写onMeasure方法和onLayout方法,在onMeasure方法里面完成对孩子的测量,在onLayout方法里面完成对孩子的摆放.
          当继承View的时候必须要重写onMeasure方法和onDraw方法 在onMeasure方法里面完成对当前view的测量,在onDraw完成绘制.

2.getMesasuredWidth和getwidth区别

     getMesasuredWidth是对view测量后使用
   getWidth 是对view排版后才可以使用

3.交叉布局

 
  
  1. public class CrossLayout extends RelativeLayout {
  2. private boolean isLeft;
  3. public CrossLayout(Context context, AttributeSet attrs) {
  4. super(context, attrs);
  5. }
  6. //对孩子进行摆放
  7. @Override
  8. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  9. int top = 0;
  10. //1.找到所有的孩子
  11. for(int i=0;i<getChildCount();i++){
  12. //2.找到所有的孩子
  13. View child = getChildAt(i);
  14. //3.对孩子进行摆放
  15. int left = 0;
  16. //3.1 对isleft 判断 如果为true 就摆到左边 否则就摆到右边
  17. if (isLeft){
  18. //4.判断是奇数还是偶数 如果是奇数就摆到左边 如果是偶数就摆到右边
  19. if (i % 2 ==0){
  20. //对于控件是奇数 就摆到左边
  21. left = 0;
  22. }else{
  23. //对于控件来说是偶数 摆到右边 left = 容器宽度 - 孩子控件的宽度
  24. left = getMeasuredWidth() - child.getMeasuredWidth();
  25. }
  26. }else{
  27. //5.判断是奇数还是偶数 如果是奇数就摆到左边 如果是偶数就摆到右边
  28. if (i % 2 ==0){
  29. //对于控件是奇数 就摆到左边
  30. left = getMeasuredWidth() - child.getMeasuredWidth();
  31. }else{
  32. //对于控件来说是偶数 摆到右边 left = 容器宽度 - 孩子控件的宽度
  33. left = 0;
  34. }
  35. }
  36. int right = left+child.getMeasuredWidth();
  37. int bootom = top + child.getMeasuredHeight();
  38. child.layout(left,top,right,bootom);
  39. top += child.getMeasuredHeight();
  40. }
  41. }
  42. //用来实现布局切换
  43. public void startSwitchLayout() {
  44. isLeft =!isLeft;
  45. //请求重新排版
  46. requestLayout();
  47. }
  48. }

4.android事件传递.

   


   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值