0

我有一个ViewFlipper包含两个布局。这两种布局各只有一个ImageView。当我在这两个布局之间滚动时有一个动画,但是在我抬起手指之前动画没有触发,这是因为:

   case MotionEvent.ACTION_UP:
   //do animation

如何修改我的代码,以便在我握住手指并从左向右滑动时触发动画,反之亦然?

用于制作动画的代码片段:

 @Override
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                oldTouchValue = touchevent.getX();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                //if(this.searchOk==false) return false;
                float currentX = touchevent.getX();
                if (oldTouchValue < currentX)
                {
                   vf.setInAnimation(inFromLeftAnimation());
                   vf.setOutAnimation(outToRightAnimation());
                    vf.showNext();
                }
                if (oldTouchValue > currentX)
                {
                    vf.setInAnimation(inFromRightAnimation());
                    vf.setOutAnimation(outToLeftAnimation());
                    vf.showPrevious();
                }
            break;
            }
        }
        return false;
    }

    //for the previous movement
    public static Animation inFromRightAnimation() {

        Animation inFromRight = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
        Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromRight.setDuration(250);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
        }
    public static Animation outToLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
         Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
         Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        outtoLeft.setDuration(250);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
        }    
    // for the next movement
    public static Animation inFromLeftAnimation() {
        Animation inFromLeft = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
        Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromLeft.setDuration(250);
        inFromLeft.setInterpolator(new AccelerateInterpolator());
        return inFromLeft;
        }
    public static Animation outToRightAnimation() {
        Animation outtoRight = new TranslateAnimation(
         Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
         Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        outtoRight.setDuration(250);
        outtoRight.setInterpolator(new AccelerateInterpolator());
        return outtoRight;
        }  
4

1 回答 1

1

如果我没记错的话,你需要 ACTION_MOVE 事件。试试下面的代码。希望能帮助到你。

if(event.getAction() == MotionEvent.ACTION_DOWN)
{
     isDown = false;            
}
if(event.getAction() == MotionEvent.ACTION_UP)
{
     isDown = true;        
}
if(event.getAction() == MotionEvent.ACTION_MOVE && !isDown)
{
    // action you want to perform
}

您可以使用的另一件事是 GestureDetector

设置手势事件:

final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector());

    View.OnTouchListener gestureListener = new View.OnTouchListener() 
    {
        public boolean onTouch(View v, MotionEvent event) 
        {
            if (gestureDetector.onTouchEvent(event)) 
            {
                return true;
            }
            return false;
        }
    };

手势监听类:

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

class MyGestureDetector extends SimpleOnGestureListener 
{
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try 
        {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
            {                   
                analogflipper.setInAnimation(slideLeftIn);
                analogflipper.setOutAnimation(slideLeftOut);                    

                analogflipper.showNext();


            }  
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
            {                       
                analogflipper.setInAnimation(slideRightIn);
                analogflipper.setOutAnimation(slideRightOut);


                analogflipper.showPrevious();

            }
        } 
        catch (Exception e) 
        {
            System.out.println("Exception...");
        }
        return false;
    }
}
于 2012-08-09T07:50:26.703 回答