Android 自定义View 实现手势监听,左右滑动,上下滑动

package com.example.administrator.myapplication;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * Created by Administrator on 2016-09-13.
 */
public class MyLayout extends RelativeLayout implements GestureDetector.OnGestureListener {
    private
    int verticalMinDistance = 20;

    private
    int minVelocity = 0;

    private MyLayoutCallBack myLayoutCallBack;
    private GestureDetector gestureDetector;
    Context context;

    public void setCallBack(MyLayoutCallBack myLayoutCallBack) {
        this.myLayoutCallBack = myLayoutCallBack;
    }

    public MyLayout(Context context) {
        super(context);
        gestureDetector = new GestureDetector(context, this);
        this.context = context;
    }

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gestureDetector.onTouchEvent(event);
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        Log.d("pingan", "onDown");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.d("pingan", "onScroll" + distanceX + "distanceY:" + distanceY);

        if (distanceX < -verticalMinDistance) {
            Log.d("pingan", "向右手势");
            myLayoutCallBack.scrollByX(20);

        } else if (distanceX > verticalMinDistance){

            Log.d("pingan", "向左手势");
            myLayoutCallBack.scrollByX(-20);

        }else if (distanceY <- verticalMinDistance) {
            Log.d("pingan", "向下手势");
            myLayoutCallBack.scrollByY(20);

        } else if (distanceY > verticalMinDistance ) {

            Log.d("pingan", "向上手势");
            myLayoutCallBack.scrollByY(-20);

        }
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.d("pingan", "onFling");

        if (e1.getX()
                - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
            Log.d("pingan", "向左手势");
            myLayoutCallBack.scrollByX(-20);

        } else if ((e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity)) {

            Log.d("pingan", "向右手势");
            myLayoutCallBack.scrollByX(20);

        } else if (e1.getY()
                - e2.getY() > verticalMinDistance && Math.abs(velocityY) > minVelocity) {
            Log.d("pingan", "向上手势");
            myLayoutCallBack.scrollByY(-20);

        } else if ((e2.getY() - e1.getY() > verticalMinDistance && Math.abs(velocityY) > minVelocity)) {

            Log.d("pingan", "向下手势");
            myLayoutCallBack.scrollByY(20);

        }

        return true;
    }
}

你可能感兴趣的:(Android 自定义View 实现手势监听,左右滑动,上下滑动)