我有一个按钮和4个选项,所以我需要用户通过向下/向上滚动选择其中一个,如下所示。我想用动画滚动它,包括fling / velocity效果。使用下面的代码,我可以滚动一个级别,如何继续并返回?
btnLevel.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
gDetector.onTouchEvent(event);
return false;
}
});
gDetector = new GestureDetector(getActivity(), new OnGestureListener()
{
private static final int SWIPE_MIN_DISTANCE = 110;
private static final int SWIPE_MAX_OFF_PATH = 50;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
@Override
public boolean onSingleTapUp(MotionEvent e)
{
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e)
{
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e)
{
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
float dY = e1.getY() - e2.getY();
if (Math.abs(velocityY) >= SWIPE_THRESHOLD_VELOCITY && Math.abs(dY) >= SWIPE_MIN_DISTANCE)
{
if (dY > 0)
{
Toast.makeText(getActivity(), "up", Toast.LENGTH_SHORT).show();
} else
{ Toast.makeText(getActivity(), "down", Toast.LENGTH_SHORT).show();
TranslateAnimation mAnimation = new TranslateAnimation(0, 0, 0, 110);
mAnimation.setDuration(1000);
mAnimation.setRepeatMode(Animation.RELATIVE_TO_SELF);
mAnimation.setInterpolator(new LinearInterpolator());
mAnimation.setFillAfter(true);
btnLevel.startAnimation(mAnimation);
}
return true;
}
return true;
}
@Override
public boolean onDown(MotionEvent e)
{
// TODO Auto-generated method stub
return false;
}
});