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

时间:2013-02-26 14:55:41

标签: android ontouchlistener

当我触摸屏幕并移动手指时,我会做一些事情(pullanimation1和2),当我释放屏幕时,我会做其他事情(fireanimation1和2)。有时,当pullAnimation或fireAnimation正在运行时,用户可能会触摸屏幕,因为动画会运行几次,我会收到错误。我想确保当用户再次触摸屏幕时,动画不会再运行一次。 NB: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;
        }
    });

1 个答案:

答案 0 :(得分:1)

我认为您应该能够使用hasStarted()和hasEnded()方法来确定您的动画当前是否正在进行。 See the docs for more

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

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

我想你可能还需要在按顺序播放后使用reset(),或者下次触摸时返回正确值的方法。

修改 AnimationDrawable有一个isRunning()方法,这使得它比查看动画更容易。

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