学习笔记:View的事件体系

    xiaoxiao2021-04-02  36

    View的基础知识 内容:View的位置参数、MotionEvent、 TouchSlop、VelocityTracker、GestureDetector、Scroller对象

    1、View的位置参数 View的位置主要由四个顶点决定: top、left、right、bottom 根据图可以很容易得出宽高坐标 width=right-left; height=bottom-top; 获取四个成员变量 Left=getLeft(); Right=getRight(); Bottom=getBottom(); Top=getTop();

    Android3.0额外加了几个参数 x、y、translationX、translationY,其中x和y是View左上角的坐标,translationX和translationY是View相对于父容器的偏移量,并且translationX和translationY默认值为0 x=left+translationX; y=top+translationY; 注: getLeft():返回的是相对于父视图的位置(这个位置我理解为最原始的位置,这个位置一旦确定不在改变) getX():返回的是相对于父视图的位置(文档中一个visual阐明的非常到位) getTranslationX():相对于之前视图,移动后的偏移量。 getRawX():返回的是视图相对于屏幕的位置

    2、MotionEvent 在手指接触屏幕后所产生的一系列事件 3、TouchSlop 是系统被认为的最小滑动距离 获取方式:

    ViewConFiguration.get(getContext()).getScaledTouchSlop();

    可以用他做一些过滤,比如当两次滑动事件的滑动距离小于这个值,我们可以认为未达到滑动距离的临界值,因此不认为是滑动。

    4、VelocityTracker 速度追踪,用于追踪手指 在滑动过程中的速度,

    VelocityTracker velocityTracker=VelocityTracker.obtain(); velocityTracker.addMovement(event);

    接着获取当前速度

    velocityTracker.computeCurrentVelocity(1000); int xVelocity=(int)velocityTracker.getXVelocity(); int yVelocity=(int)velocityTracker.getYVelocity();

    注: 1:获取速度之前必须计算速度 2:这里的速度指单位时间内的滑动的像素数(可以是1s也可以是100ms取决于 velocityTracker.computeCurrentVelocity(1000)),可以为负数。 计算方式: 速度=(终点位置-起点位置)/时间段 最后不使用的时候调用clear方法重置并回收内存

    velocityTracker.clear(); velocityTracker.recycle();
    转载请注明原文地址: https://ju.6miu.com/read-665778.html

    最新回复(0)