Android 滑动(一)窗口坐标系和触摸控件——MotionEvent

来源:互联网 发布:启凡网络 编辑:程序博客网 时间:2024/05/19 16:51

一、坐标系

(1)、Android坐标系


(1)、getLocationOnScreen(intlocation[]):获取该视图左上角在Android中的坐标

(2)、getRawX():点击事件距离整个屏幕左边的距离,即绝对坐标

(3)、getRawY():点击事件距离整个屏幕顶边的距离,即绝对坐标

二、视图坐标系

原点不是屏幕的左上角,而是以父视图左上角为坐标原点


(1)、getX():获取点击事件距离控件左边的距离,视图坐标

(2)、getY():获取点击事件距离控件顶边的距离,视图坐标


三、触摸事件——MotionEvent

@Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()){            case MotionEvent.ACTION_DOWN:                //处理输入的按下事件                break;            case MotionEvent.ACTION_MOVE:                //  处理输入的移动事件                break;            case MotionEvent.ACTION_UP:                //处理输入的离开事件                break;        }        return true;    }
在不涉及多点操作的情况下,通常可以使用以上代码来完成触摸事件的监听,不过这里只是一个代码模块,自己根据触摸事件完成具体的逻辑。

来看看MotionEvent重封装的一些常用的事件常量,它定义了触摸事件的不同类型

        /**         * Constant for {@link #getActionMasked}: A pressed gesture has started, the         * motion contains the initial starting location.         * <p>         * This is also a good time to check the button state to distinguish         * secondary and tertiary button clicks and handle them appropriately.         * Use {@link #getButtonState} to retrieve the button state.         * </p>         */        public static final int ACTION_DOWN             = 0;//单点触摸按下事件        /**         * Constant for {@link #getActionMasked}: A pressed gesture has finished, the         * motion contains the final release location as well as any intermediate         * points since the last down or move event.         */        public static final int ACTION_UP               = 1;//单点触摸离开动作        /**         * Constant for {@link #getActionMasked}: A change has happened during a         * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}).         * The motion contains the most recent point, as well as any intermediate         * points since the last down or move event.         */        public static final int ACTION_MOVE             = 2;//触摸点移动动作        /**         * Constant for {@link #getActionMasked}: The current gesture has been aborted.         * You will not receive any more points in it.  You should treat this as         * an up event, but not perform any action that you normally would.         */        public static final int ACTION_CANCEL           = 3;//触摸点取消事件        /**         * Constant for {@link #getActionMasked}: A movement has happened outside of the         * normal bounds of the UI element.  This does not provide a full gesture,         * but only the initial location of the movement/touch.         */        public static final int ACTION_OUTSIDE          = 4;//触摸动作超出边界        /**         * Constant for {@link #getActionMasked}: A non-primary pointer has gone down.         * <p>         * Use {@link #getActionIndex} to retrieve the index of the pointer that changed.         * </p><p>         * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the         * unmasked action returned by {@link #getAction}.         * </p>         */        public static final int ACTION_POINTER_DOWN     = 5;//多点触摸按下动作        /**         * Constant for {@link #getActionMasked}: A non-primary pointer has gone up.         * <p>         * Use {@link #getActionIndex} to retrieve the index of the pointer that changed.         * </p><p>         * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the         * unmasked action returned by {@link #getAction}.         * </p>         */        public static final int ACTION_POINTER_UP       = 6;//多点离开动作


四、Android提供的获取坐标值、相对距离的方法主要有两类:

(1):View提供的获取坐标的方法


getTop():获取的是View自身的顶边到齐父布局顶边的距离

getBottom():获取的是View自身的底边到齐父布局顶边的距离

getLeft():获取的是View自身的左边到齐父布局左边的距离

getRight():获取的是View自身的右边到齐父布局左边的距离


(2)MotionEvent提供的方法

getX():获取点击事件距离控件左边的距离,视图坐标

getY():获取点击事件距离控件顶边的距离,视图坐标

getRawX():点击事件距离整个屏幕左边的距离,即绝对坐标

getRawY():点击事件距离整个屏幕顶边的距离,即绝对坐标













1 0