zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    POJ 2240 Arbitrage spfa 判正环
    POJ 3259 Wormholes spfa 判负环
    POJ1680 Currency Exchange SPFA判正环
    HDU5649 DZY Loves Sorting 线段树
    HDU 5648 DZY Loves Math 暴力打表
    HDU5647 DZY Loves Connecting 树形DP
    CDOJ 1071 秋实大哥下棋 线段树
    HDU5046 Airport dancing links 重复覆盖+二分
    HDU 3335 Divisibility dancing links 重复覆盖
    FZU1686 神龙的难题 dancing links 重复覆盖
  • 原文地址:https://www.cnblogs.com/makeryan/p/2528824.html
Copyright © 2011-2022 走看看