本文介绍了ViewFlipper动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewFlipper 它包含两种布局。这两个布局helds只是一个的ImageView 每个。有动画,当我滚动这两个布局之间,但动画并不triggerd之前,我举起我的手指,这是因为这个原因:

 案例MotionEvent.ACTION_UP:
   //做动画
 

如何修改我的code所以动画是triggerd当我拿着我的手指和幻灯片左到右,副verca?

codesnippet做动画:

  @覆盖
    公共布尔的onTouchEvent(MotionEvent的TouchEvent){
        开关(touchevent.getAction())
        {
            案例MotionEvent.ACTION_DOWN:
            {
                oldTouchValue = touchevent.getX();
                打破;
            }
            案例MotionEvent.ACTION_UP:
            {
                //if(this.searchOk==false)返回false;
                浮currentX = touchevent.getX();
                如果(oldTouchValue< currentX)
                {
                   vf.setInAnimation(inFromLeftAnimation());
                   vf.setOutAnimation(outToRightAnimation());
                    vf.showNext();
                }
                如果(oldTouchValue> currentX)
                {
                    vf.setInAnimation(inFromRightAnimation());
                    vf.setOutAnimation(outToLeftAnimation());
                    vf.show previous();
                }
            打破;
            }
        }
        返回false;
    }

    //为previous运动
    公共静态动画inFromRightAnimation(){

        动画inFromRight =新TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,+ 1.0F,Animation.RELATIVE_TO_PARENT,0.0,
        Animation.RELATIVE_TO_PARENT,0.0,Animation.RELATIVE_TO_PARENT,0.0
        );
        inFromRight.setDuration(250);
        inFromRight.setInterpolator(新AccelerateInterpolator());
        返回inFromRight;
        }
    公共静态动画outToLeftAnimation(){
        动画outtoLeft =新TranslateAnimation(
         Animation.RELATIVE_TO_PARENT,0.0,Animation.RELATIVE_TO_PARENT,-1.0F,
         Animation.RELATIVE_TO_PARENT,0.0,Animation.RELATIVE_TO_PARENT,0.0
        );
        outtoLeft.setDuration(250);
        outtoLeft.setInterpolator(新AccelerateInterpolator());
        返回outtoLeft;
        }
    //为下运动
    公共静态动画inFromLeftAnimation(){
        动画inFromLeft =新TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,-1.0F,Animation.RELATIVE_TO_PARENT,0.0,
        Animation.RELATIVE_TO_PARENT,0.0,Animation.RELATIVE_TO_PARENT,0.0
        );
        inFromLeft.setDuration(250);
        inFromLeft.setInterpolator(新AccelerateInterpolator());
        返回inFromLeft;
        }
    公共静态动画outToRightAnimation(){
        动画outtoRight =新TranslateAnimation(
         Animation.RELATIVE_TO_PARENT,0.0,Animation.RELATIVE_TO_PARENT,+ 1.0F,
         Animation.RELATIVE_TO_PARENT,0.0,Animation.RELATIVE_TO_PARENT,0.0
        );
        outtoRight.setDuration(250);
        outtoRight.setInterpolator(新AccelerateInterpolator());
        返回outtoRight;
        }
 

解决方案

如果我没看错,你需要ACTION_MOVE事件。试试下面code。希望它能帮助。

 如果(event.getAction()== MotionEvent.ACTION_DOWN)
{
     isDown = FALSE;
}
如果(event.getAction()== MotionEvent.ACTION_UP)
{
     isDown = TRUE;
}
如果(event.getAction()== MotionEvent.ACTION_MOVE&安培;&安培;!isDown)
{
    //动作要执行
}
 

还有一件事,你可以使用一个GestureDetector

设置手势事件:

 最后GestureDetector gestureDetector =新GestureDetector(新MyGestureDetector());

    View.OnTouchListener gestureListener =新View.OnTouchListener()
    {
        公共布尔onTouch(视图V,MotionEvent事件)
        {
            如果(gestureDetector.onTouchEvent(事件))
            {
                返回true;
            }
            返回false;
        }
    };
 

GestureListener类:

 私有静态最终诠释SWIPE_MIN_DISTANCE = 120;
私有静态最终诠释SWIPE_MAX_OFF_PATH = 250;
私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 200;

类MyGestureDetector扩展SimpleOnGestureListener
{
    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
        尝试
        {
            如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH)
                返回false;
            //从右向左轻扫
            如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY)
            {
                analogflipper.setInAnimation(slideLeftIn);
                analogflipper.setOutAnimation(slideLeftOut);

                analogflipper.showNext();


            }
            否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY)
            {
                analogflipper.setInAnimation(slideRightIn);
                analogflipper.setOutAnimation(slideRightOut);


                analogflipper.show previous();

            }
        }
        赶上(例外五)
        {
            的System.out.println(异常...);
        }
        返回false;
    }
}
 

I have a ViewFlipper which contains two layouts. These two layouts helds just one ImageView each. There is an animation when I scroll between these two layouts, but the animation isn't triggerd before I lift my finger, this happens because of this:

   case MotionEvent.ACTION_UP:
   //do animation

How can I modify my code so the animation is triggerd when I held my finger and slide left to right and vice verca?

Codesnippet for doing the 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;
        }
解决方案

If I am not wrong, you need ACTION_MOVE event. try below code. Hope it helps.

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
}

One more thing you can use is a GestureDetector

Setting gesture event:

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

GestureListener class:

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

这篇关于ViewFlipper动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:39