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; } }