Android 是否可以覆盖SimpleOnGestureListener以检测释放?
我正在尝试实现一个手势处理程序,允许用户以抛物线形状刷卡,就像你单手刷卡时手指的移动方式一样 我已经能够通过如下方式重写onDown和onScroll方法来实现这一点:Android 是否可以覆盖SimpleOnGestureListener以检测释放?,android,gesturelistener,Android,Gesturelistener,我正在尝试实现一个手势处理程序,允许用户以抛物线形状刷卡,就像你单手刷卡时手指的移动方式一样 我已经能够通过如下方式重写onDown和onScroll方法来实现这一点: public class MoveViewTouchListener implements View.OnTouchListener { public GestureDetector mGestureDetector; public View mView; public double a;
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方向的平移
这非常有效,并以抛物线二次形状滑动。但是当用户没有在屏幕上完全滑动时,我想将图标动画化回到起始位置,就像在tinder中如果你没有完全滑动的情况一样
我知道我需要使用MotionEvent.ACTION\u UP,但我不知道如何在我的案例中使用它,因为我正在onScroll和onDown中进行更改。据我所知,mGestureDetector处理该事件只是因为该事件是由MoveViewTouchListener委派的
因此,如果事件是MotionEvent,您不能简单地签入MoveViewTouchListener::onTouch。ACTION\u UP:在这种情况下,不要调用mGestureDetector.OnTouchEvent;,但只需将视图设置回动画,例如:
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);
}
@braaterAfrikaaner为什么要编辑相关信息?非常感谢我以前尝试过这种方式。但是没有意识到它是event.getAction==MotionEvent.event\u UP