RotateAnimation Get Current angle of image

☆樱花仙子☆ 提交于 2020-01-25 10:09:49

问题


I have a question of RotationAnimation.

First.
Is there a Listener to during animation?
start, start animation
repeat, repeat animation
stop, stop animation.

but there are no animation listener to check the on going.

second,
is any other get a current rotate angle of image?

I think, ImageView is rotate by rotationAnimation function.
so I made a timer thread and run 1second

'''
timer = new Timer();
       timerTask = new TimerTask() {
       public void run(){
       Log.e("LOG",  " [angle]: " + String.format("%3.1f",  rotateImage.getRotation());
    }
};
timer.schedule(timerTask, 0, 1000);

'''

but, I can't see the changed value during rotate.

how can I get the current angle during rotate?

thanks.


回答1:


For Rotation Animation, Yes There is as shown below:

RotateAnimation rotateAnimation = new RotateAnimation();
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
  @Override
  public void onAnimationStart(Animation animation) {

  }

  @Override
  public void onAnimationEnd(Animation animation) {

  }

  @Override
  public void onAnimationRepeat(Animation animation) {

  }
});

You can also use Object Animators to animate Rotation:

ObjectAnimator rotateAnimation = ObjectAnimator.ofFloat(targetView, View.ROTATION, startAngle, endAngle);
rotateAnimation.addListener(new Animator.AnimatorListener() {
  @Override
  public void onAnimationStart(Animator animation) {

  }

  @Override
  public void onAnimationEnd(Animator animation) {

  }

  @Override
  public void onAnimationCancel(Animator animation) {

  }

  @Override
  public void onAnimationRepeat(Animator animation) {

  }
});

To get the angle of your ImageView simply use imageView.getRotation(); this will give you an int value of the current angle of rotation.

You also don't need Timer because both ObjectAnimator and rotateAnimator provide time control for you:

rotateAnimation.setDuration(1000); // run animation for 1000 milliseconds or 1 second
rotateAnimation.setStartDelay(1000); // delay animation for 1000 milliseconds or 1 second

Finally, To get rotation Angle DURING the time animation is running there is a listener method called addUpdateListener :

rotateAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
    int value = (int) animation.getAnimatedValue(); // dynamic value of angle
  }
});


来源:https://stackoverflow.com/questions/58007384/rotateanimation-get-current-angle-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!