Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在第一个循环后停止可绘制动画_Android_Touch Event_Motionevent_Animationdrawable - Fatal编程技术网

Android 在第一个循环后停止可绘制动画

Android 在第一个循环后停止可绘制动画,android,touch-event,motionevent,animationdrawable,Android,Touch Event,Motionevent,Animationdrawable,我的问题是试图找出如何停止可绘制动画后,它迭代通过图像,例如,它是回到葡萄图像。我当然不能使用我在.ACTION.DOWN中实现的方法,所以有人能帮我吗 public boolean onTouchEvent (MotionEvent event){ Drawable currentFrame, checkFrame; if (event.getAction() == MotionEvent.ACTION_DOWN) { if (f

我的问题是试图找出如何停止可绘制动画后,它迭代通过图像,例如,它是回到葡萄图像。我当然不能使用我在.ACTION.DOWN中实现的方法,所以有人能帮我吗

    public boolean onTouchEvent (MotionEvent event){
        Drawable currentFrame, checkFrame;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (fruitAnimation.isRunning()) {
                fruitAnimation.stop();
                //The variable that will guard the frame number
                int frameNumber = 0;
                //Get the frame of the animation
                //Drawable currentFrame, checkFrame;
                currentFrame = fruitAnimation.getCurrent();
                //checks the position of the frame.
                for (int i = 0; i < fruitAnimation.getNumberOfFrames(); i++) {
                    checkFrame = fruitAnimation.getFrame(i);
                    if (checkFrame == currentFrame) {
                        frameNumber = i;
                        break;
                    }

                }
                String fruit = "";

                switch (frameNumber) {
                    case 0:
                        fruit = "Grapes";
                        break;
                    case 1:
                        fruit = "Lemon";
                        break;
                    case 2:
                        fruit = "Orange";
                        break;
                    case 3:
                        fruit = "Pear";
                        break;
                    case 4:
                        fruit = "Strawberry";
                        break;


                }
                Toast.makeText(this, fruit, Toast.LENGTH_SHORT).show();

            }else {
                fruitAnimation.start();


            }
            return true;
        }

        return super.onTouchEvent(event);
    }

你很可能做得不对。因为你可以利用它来监控你的动画,我将动画设置为一次性运行,然后在我的监听器中,在它的onAnimationEnd中,我会检查我是否达到了极限,如果没有,重新开始动画。

我选择了你的答案,因为你是唯一回答这个问题的人,但我所做的是在包含我的图片的xml文件中添加了android:oneshot=true。啊,这是正确的方法。出于某种原因,我读到您希望在给定X播放后停止动画。不管怎样,很高兴它对你有用