2016-11-11 5 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; 
      } 
     }); 

時々私はPlayerをタッチするが、ImageViewのonClicListenerはアニメーション中に呼び出されない/起動されないmethod touchedPlayer()それが何であるか知っていますか?

答えて

5

XYをアニメーション化する代わりにObjectAnimatorを使用してみてください。このような問題は一度も起こりませんでした。ここで行っていることに似ています。

はこれを試してみて、それが動作するかどうかを確認:

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

ありがとうございました。明らかに彼は私の問題を解決しました。なぜアニメーションの種類の違いか教えてください。 –

関連する問題