动画期间在ImageView上的OnTouchListener,有时不会被触发/调用

Luc*_*tos 1 android imageview android-animation android-layout

我有

  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;
            }
        });
Run Code Online (Sandbox Code Playgroud)

有时我会触摸播放器,但method touchedPlayer()在动画期间不会调用/触发ImageView上的onClicListener .你知道它是什么吗?

Joe*_*alu 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();
Run Code Online (Sandbox Code Playgroud)