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

前端 未结 3 730
失恋的感觉
失恋的感觉 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条回答
  •  梦毁少年i
    2021-01-11 14:33

    You can use an ObjectAnimator (API 11+):

    ImageView iv = (ImageView)findViewById(R.id.markerRed);
    
    // Create animators for x and y axes
    ObjectAnimator oax = ObjectAnimator.ofFloat(iv, "translationX", 0f, 100f);
    ObjectAnimator oay = ObjectAnimator.ofFloat(iv, "translationY", 0f, 100f);
    oax.setRepeatCount(Animation.INFINITE);
    oay.setRepeatCount(Animation.INFINITE);
    
    // Combine Animators and start them together
    AnimatorSet set = new AnimatorSet();
    set.setDuration(1000);
    set.playTogether(oax, oay);
    set.start();
    

    Then fetch the animation values like this:

    Log.e("TAG", "X: " + oax.getAnimatedValue() + " Y:" + oay.getAnimatedValue());
    

    If you add these values to the initial ImageView's coordinates, you'll get the current location.

提交回复
热议问题