1

我有一个按钮和 4 个选项,所以我需要用户通过向下/向上滚动来选择其中一个,如下所示。我想用动画滚动它,包括投掷/速度效果。使用下面的代码,我可以滚动一级,如何继续并返回?

在此处输入图像描述

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

1 回答 1

0

您需要实现动画侦听器并覆盖 onAnimationEnd 方法

查看此文档

于 2013-09-04T13:06:52.293 回答