在onTouch()时触摸屏幕已经被调用



当我触摸屏幕并移动手指时,我会做一些事情(pullanimation1和2(,当我释放屏幕时,我也会做其他事情(fireanimation_1和2(。有时,用户可能会在pullAnimation或fireAnimation运行时触摸屏幕,我会在动画运行几次时出错。我想确保当用户再次触摸屏幕时,动画不会运行一次以上。注意:pullAnimation1和2,fireAnimation 1和2是AnimationDrawable以下是我所做的:

    image2.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            boolean bool=false;
            boolean bool2=true;
            int action = arg1.getAction() & MotionEvent.ACTION_MASK;
            switch (action) {
            case MotionEvent.ACTION_DOWN:
            if (bool2) {
            startAnimation(pullAnimation1,pullAnimation2);
            bool=true;
            }
            break;
            case MotionEvent.ACTION_MOVE:
                if (bool2==true){
                Log.w("GAMEACTIVITY","move");
                bool=true;
                bool2=false;
                }
                break;
            case MotionEvent.ACTION_UP:
                startAnimation(fireAnimation1,fireAnimation2);
                bool=false;
                doPhotoTask();
                bool2=false;
                break;
            }
            return bool;
        }
    });

我认为您应该能够使用hasStarted((和hasEnded((方法来确定您的动画当前是否正在进行。有关更多,请参阅文档

像这样的一些if语句可能有效:

if((fireAnimation1.hasStarted() == false) || (fireAnimation1.hasEnded == true()){
  startAnimation(fireAnimation1, fireAnimation2);
}

我想你可能还需要在播放完成后使用reset(),以便下次触摸时返回正确值。

编辑:AnimationDrawable有一个isRunning()方法,它甚至比View动画更容易。

if(fireAnimation1.isRunning() == false){
  startAnimation(fireAnimation1, fireAnimation2);
}

最新更新