Is it possible to get real time coordinates of an ImageView while it is in Translate animation?

前端 未结 3 718
失恋的感觉
失恋的感觉 2021-01-11 14:18

I have an image of a bullet in an ImageView that does Translate animation.

I need to show real time coordinates to show how far it is from target in real time.

3条回答
  •  一整个雨季
    2021-01-11 14:45

    Here's a complete example based on what user3249477 and Vikram said:

        final TextView positionTextView = (TextView)findViewById(R.id.positionTextView);
        ImageView myimage = (ImageView)findViewById(R.id.imageView);    
    
        ObjectAnimator translateXAnimation= ObjectAnimator.ofFloat(myimage, "translationX", 0f, 100f);
        ObjectAnimator translateYAnimation= ObjectAnimator.ofFloat(myimage, "translationY", 0f, 100f);     
        translateXAnimation.setRepeatCount(ValueAnimator.INFINITE);      
        translateYAnimation.setRepeatCount(ValueAnimator.INFINITE);
    
        AnimatorSet set = new AnimatorSet();
        set.setDuration(1000);
        set.playTogether(translateXAnimation, translateYAnimation);
        set.start();
    
        translateXAnimation.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                imageXPosition = (Float)animation.getAnimatedValue();
            }
        });
    
        translateYAnimation.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                imageYPosition = (Float)animation.getAnimatedValue();
                String position = String.format("X:%d Y:%d", (int)imageXPosition, (int)imageYPosition);
                positionTextView.setText(position);
            }
        });
    

提交回复
热议问题