【问题标题】:OnTouchListener on ImageView during animation, not fired/called sometimes动画期间 ImageView 上的 OnTouchListener,有时不会触发/调用
【发布时间】:2016-11-11 09:25:11
【问题描述】:

我有

  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()。你知道它可以是什么吗?

【问题讨论】:

    标签: android android-layout imageview android-animation


    【解决方案1】:

    尝试使用ObjectAnimator 代替动画X 和Y 以供您查看,我从未遇到过此类问题,并且我经常将它用于与您在这里所做的类似的事情。

    试试这个,看看它是否有效:

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

    【讨论】:

    • 谢谢。显然他解决了我的问题。你能告诉我为什么动画类型之间会有这种差异吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多