How to rotate a drawable by ObjectAnimator?

强颜欢笑 提交于 2019-12-03 22:43:40

Try this simple Rotation Animation applied to a image.

 ImageView imageview = (ImageView)findViewById(R.id.myimage);
 RotateAnimation rotate = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,    
 0.5f,  Animation.RELATIVE_TO_SELF, 0.5f);
  rotate.setDuration(500);
 imageview.startAnimation(rotate);

This answer is just for a sake of question, it is correct that Clickable area will be different than View's current position. Please check this question for making clickable area correct. Button is not clickable after TranslateAnimation

Try with ObjectAnimator instead.

ImageView imageview = (ImageView)findViewById(R.id.image);

ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
                    "rotation", 0f, 360f);
            imageViewObjectAnimator.setDuration(1000); // miliseconds
            imageViewObjectAnimator.start();

EDIT Since this question draw some attention let me to explain why to use ObjectAnimator instead of other Transition animators

The thing about using ObjectAnimator is that it's moving both the visible and the clickable area of the item, if you use another animation method, for example Transition Animation or some other Animators, and let's say if you want to move the Button from the bottom left of the screen to the top left, it will only move the visible area but not the Button itself, the clickable area will still be on the previous position, in this case the clickable area will still be on the bottom left instead of the top left where you moved the button.

If you do the same with ObjectAnimator, both the visible area, and the clickable area will move the the desired location.

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