是否可以覆盖SimpleOnGestureListener来检测版本?

时间:2018-01-31 13:29:00

标签: android gesturelistener

我正在尝试实现一个手势处理程序,允许用户以抛物线形状滑动(如果你单手滑动,手指的移动方式)。

我已经能够通过使用覆盖onDownonScroll这样的方法来实现这一点:

public class MoveViewTouchListener
        implements View.OnTouchListener
{
    public GestureDetector mGestureDetector;
    public View mView;
    public double a;
    public double b;
    public double c;


    public MoveViewTouchListener(View view,double a, double b, double c)
    {
        mGestureDetector = new GestureDetector(view.getContext(), mGestureListener);
        mView = view;
        this.a = a;
        this.b = b;
        this.c = c;

    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {

        return mGestureDetector.onTouchEvent(event);

    }

    private GestureDetector.OnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener()
    {
        private float mMotionDownX, mMotionDownY;

        @Override
        public boolean onDown(MotionEvent e)
        {

            mMotionDownX = e.getRawX() - mView.getTranslationX();
            mMotionDownY = e.getRawY() - mView.getTranslationY();
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
        {
            mView.setTranslationX(e2.getRawX() - mMotionDownX);

            // Set the x translation
            double x = mView.getTranslationX();

            // Graphing quadratic (parabola)
            // y = ax^2 + bx + c

            double a = 0.00125;
            double b = 0.0;
            double c = 0.0;

            //Converting that to code
            double y = a*x*x + b*x + c;

            Log.d("Chex ","X = " + x +", Y = " + y);

            // Set the y translation
            mView.setTranslationY((float)y);

            return true;
        }

    };
}

基本上,对于x方向上的每个滚动事件,我都会在y方向上设置平移。

这很好用,并以抛物线(二次)形状滑动。但是当用户没有在屏幕上完全滑动时,我想将图标设置回动画回到开始位置(就像你没有完全滑动整个过程中的情况一样)。

我知道我需要使用MotionEvent.ACTION_UP,但我不知道如何在我的情况下使用它,因为我正在onScrollonDown进行更改。

1 个答案:

答案 0 :(得分:1)

据我所知mGestureDetector只处理事件,因为事件是由MoveViewTouchListener委派的。

所以,如果事件是MoveViewTouchListener::onTouch(),你不能简单地检查MotionEvent.ACTION_UP:在这种情况下,不要调用mGestureDetector.onTouchEvent(event);,而只是简单地为view设置动画喜欢(例如:)

mView.animate()
    .x(mViewInitialX)
    .y(mViewInitialY)
    .setDuration(200)
    .start();

并返回true

它会变成类似

的东西
@Override
public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.EVENT_UP) {
        // go back to initial position
        return true;
    }

    return mGestureDetector.onTouchEvent(event);

}
相关问题