admin
2021-07-06 abce02c7a61820f5d580f87364d542e817be429c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.wpc.library.widget;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.Gallery;
 
public class CustomGallery extends Gallery {
    private float mLastMotionX;// 初始x坐标
    private float mLastMotionY;// 初始y坐标
    private int mTouchSlop;// 触摸最短距离
 
    public CustomGallery(Context context) {
        super(context);
    }
 
    public CustomGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
 
    }
 
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final ViewConfiguration configuration = ViewConfiguration
                .get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();// 触摸最短距离
        final int action = ev.getAction();
        // x坐标
        final float x = ev.getX();// 每次触摸发生变化的低吼都会记录下坐标
        // y坐标
        final float y = ev.getY();// 每次触摸发生变化的低吼都会记录下坐标
        switch (action) {
        case MotionEvent.ACTION_DOWN:// 记录初始位置
            mLastMotionX = x;
            mLastMotionY = y;
            break;
        case MotionEvent.ACTION_MOVE:
            // 此时的xy是手指离开时的位置坐标
            final int deltaX = (int) (mLastMotionX - x);// 水平方向滑动距离
            final int deltaY = (int) (mLastMotionY - y);// 垂直方向滑动距离
 
            if (Math.abs(deltaX) > mTouchSlop
                    && Math.abs(deltaY) < Math.abs(deltaX)) {// 表明是横向滑动交给,Gallery
                return true;
            }
            break;
        }
        return false;
    }
}