Lucas Santos Lucas Santos - 22 days ago 8
Android Question

OnTouchListener on ImageView during animation, not fired/called sometimes

I have


  1. Layout Game (Relative Layout)

  2. ImageView (The Player)

  3. ViewPropertyAnimator (The Animation)



My Code is:

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;
}
});


Sometimes I touch on the Player but onClicListener on ImageView is not called/fired
method touchedPlayer()
during animation. Do you know what it can be?

Answer

Try using an ObjectAnimator instead to animate X and Y for your view, I never experienced such problems and I use it a lot for stuff similar to what you're doing here.

Try this and see if it works:

    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();
Comments