• Android中的View与ViewGroup绘制过程,手势监听顺序与使用


    一 View ,ViewGroup的绘制过程

    ViewGroup绘制包括两个步骤:1.measure 2.layout

    在两个步骤中分别调用回调函数:1.onMeasure()   2.onLayout()

    1.onMeasure() 在 这个函数中,ViewGroup会接受childView的请求的大小,然后通过childView的 measure(newWidthMeasureSpec, heightMeasureSpec)函数存储到childView中,以便childView的getMeasuredWidth() andgetMeasuredHeight() 的值可以被后续工作得到。

    2.onLayout() 在这个函数中,ViewGroup会拿到childView的getMeasuredWidth() andgetMeasuredHeight(),用来布局所有的childView。

    3.View.MeasureSpec LayoutParams 这两个类,是ViewGroup与childView协商大小用的。其中,View.MeasureSpec是ViewGroup用来部署childView用的, LayoutParams是childView告诉ViewGroup 我需要多大的地方。

    4.在View 的onMeasure的最后要调用setMeasuredDimension()这个方法存储View的大小,这个方法决定了当前View的大小。

    具体详见android官方文档 dev guide->User Interface->How Android Draws Views


    二 View,ViewGroup的手势监听顺序与使用

    View的手势监听相关回调函数:onTouchEvent()

    ViewGroup的手势监听相关回调函数:onTouchEvent(),onInterceptTouchEvent()

    1.这两个回调函数都会返回一个boolean变量,表示是否消费了此手势。如果消费了,返回true,如果未消费,返回false。

    2.当用户触摸一下屏幕,产生手势MotionEvent,

    ViewGroup的onInterceptTouchEvent()会接受此MotionEvent。

    如果此回调函数返回true,则表示此ViewGroup消费了此手势,不想再让他的childView去处理,childView的onTouchEvent()便不会再接受此手势,同时此ViewGroup的onTouchEvent()会接受此手势。

    如果此回调函数返回false,则表示此ViewGroup未消费了此手势,想让他的childView去处理,childView的onTouchEvent()接受此手势,同时此ViewGroup的onTouchEvent()不会接受此手势。


    3.onTouchEvent()的返回值指的是向上传递event,onInterceptTouchEvent()的返回值指的是向下传递event

  • 相关阅读:
    android传送照片到FTP服务器
    Android对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果
    android上传图片至服务器
    android中如何处理cookie
    【265】shell文件创建链接
    【264】◀▶ Windows 批处理(CMD)
    【263】Linux 添加环境变量 & 全局 shell 脚本
    【262】pscp命令 实现windows与linux互传文件
    合泰 HT66F30 定时器初始化
    错误 是否保存对以下各项的更改 devenv.sin
  • 原文地址:https://www.cnblogs.com/makeryan/p/2528824.html
Copyright © 2020-2023  润新知