2016-11-11 51 views
1

OnTouchListener在動畫過程中ImageView的,沒有被解僱/被叫有時

  1. 佈局遊戲(相對佈局)
  2. ImageView的(玩家)
  3. ViewPropertyAnimator(動畫)

我的代碼是:

final Vibrator vibe = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); 
    final ImageView playerImageView = (ImageView) layoutInflater.inflate(R.layout.player, null); 
     playerImageView.setLayoutParams(new RelativeLayout.LayoutParams(playerSizeX, playerSizeY)); 
     playerImageView.setImageDrawable(playerDrawable); 
     playerImageView.setX(playerVisualPositionX); 
     playerImageView.setY(playerVisualPositionY); 
     playerImageView.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
        vibe.vibrate(100); 
        touchedPlayer(); 
       } 

       return true; 
      } 
     }); 

     layoutGame.addView(playerImageView); 
     ViewPropertyAnimator viewPropertyAnimator = playerImageView.animate(); 
     viewPropertyAnimator.x(positionAnimateX); // The view will be animated such that it moves to positionAnimateX. 
     viewPropertyAnimator.y(positionAnimateY); // The view will be animated such that it moves to positionAnimateY. 
     viewPropertyAnimator.setDuration(animationTime); 
     viewPropertyAnimator.setListener(new AnimatorListenerAdapter() { 
      @Override 
      public void onAnimationEnd(Animator animation) { 
       //super.onAnimationEnd(animation); 
       layoutGame.removeView(playerImageView); 

       if (!gameOver && !gamePaused) { 
        addNewPlayer(); 
       } 
      } 

      @Override 
      public void onAnimationStart(Animator animation) { 
       //super.onAnimationStart(animation); 
       currentAnimation = animation; 
      } 
     }); 

有時我碰到播放器,但在動畫期間,ImageView上的onClicListener不會被調用/觸發method touchedPlayer()。你知道它會是什麼嗎?

回答

5

嘗試使用ObjectAnimator代替爲您的視圖製作XY的動畫,我從未遇到過這樣的問題,並且我將它用於許多類似於您在此處所做的事情。

試試這個,看看它是否工作:

layoutGame.addView(playerImageView); 
    PropertyValuesHolder xHolder = PropertyValuesHolder.ofFloat("X", playerImageView.getX(), positionAnimateX); // The view will be animated such that it moves to positionAnimateX. 
    PropertyValuesHolder yHolder = PropertyValuesHolder.ofFloat("Y", playerImageView.getY(), positionAnimateY); // The view will be animated such that it moves to positionAnimateY. 
    ValueAnimator valueAnimator = ObjectAnimator.ofPropertyValuesHolder(playerImageView, xHolder, yHolder); 
    valueAnimator.setDuration(animationTime); 
    valueAnimator.addListener(new Animator.AnimatorListener() { 
     @Override 
     public void onAnimationStart(Animator animation) { 
      //super.onAnimationStart(animation); 
      currentAnimation = animation; 
     } 

     @Override 
     public void onAnimationEnd(Animator animation) { 
      //super.onAnimationEnd(animation); 
      layoutGame.removeView(playerImageView); 

      if (!gameOver && !gamePaused) { 
       addNewPlayer(); 
      } 
     } 

     @Override 
     public void onAnimationCancel(Animator animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animator animation) { 

     } 
    }); 
    valueAnimator.start(); 
+1

謝謝。顯然他解決了我的問題。你能告訴我爲什麼這種動畫類型之間的區別? –