How to rotate a drawable by ObjectAnimator?
Asked Answered
G

2

22

Alphaing a drawable work well like this:

if(mAlphaAnimation == null){
        mAlphaAnimation = ObjectAnimator.ofFloat(this, "alpha", 0.0f,1.0f).setDuration(TARGET_ANIM_ALPHA_DURATION);
        mAlphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        mAlphaAnimation.setStartDelay(TARGET_ANIM_ALPHA_DELAY_BASE*power);
        mAlphaAnimation.setRepeatCount(ValueAnimator.INFINITE);
        mAlphaAnimation.setRepeatMode(ValueAnimator.REVERSE);
        mAlphaAnimation.addUpdateListener(this);
 }

But if I want rotate a drawable like below , it don's work.

private void createRotateAnim(float fromDegress,float toDegress,int duration){
    if(mRotateAnimation == null){
        mRotateAnimation = ObjectAnimator.ofFloat(this, "rotation",fromDegress,toDegress).setDuration(duration);
        mRotateAnimation.setStartDelay(100);
        mRotateAnimation.setInterpolator(new AccelerateInterpolator());
        mRotateAnimation.addUpdateListener(this);
    }
}

Anyone can help me to fix this issue, or these is any other way to create a rotation drawable animation .

I am sorry to my poor English.

Gemina answered 26/12, 2012 at 10:28 Comment(2)
I wanna change a drawable status through touch event,the drawable is a single drawable,not a drawable list.For example, change a drawable alpha value according to moving distance on screen. And this function have finished. Then, i wanna change a drawable direction according to moving angle. In other words, rotate the drawable according to the angle. These drawables will be draw on the same view. I wanna to know a ObjectAnimator object with rotation property can rotate a drawable or not. perhaps, it can rotate,because a imageview can rotate a drawable used to background drawable.Gemina
Possible duplicate of Android: Using ObjectAnimator to translate a View with fractional values of the View's dimensionMaiduguri
T
19

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

Timeworn answered 26/12, 2012 at 10:59 Comment(2)
Thank you. but I don't wanna put the drawble into other view,like ImageView and so on, but wanna draw it on a subclass of view(NOT ViewGroup).Gemina
@Gemina I think your mistaken, ImageView is simply a subclass of View and not ViewGroup (Which itself is a subclass of View). You could create your own subclass of View and render the drawable onto a canvas, however this would do the same job as an ImageView.Bagwell
D
77

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.

Dichroite answered 6/4, 2015 at 5:13 Comment(0)
T
19

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

Timeworn answered 26/12, 2012 at 10:59 Comment(2)
Thank you. but I don't wanna put the drawble into other view,like ImageView and so on, but wanna draw it on a subclass of view(NOT ViewGroup).Gemina
@Gemina I think your mistaken, ImageView is simply a subclass of View and not ViewGroup (Which itself is a subclass of View). You could create your own subclass of View and render the drawable onto a canvas, however this would do the same job as an ImageView.Bagwell

© 2022 - 2024 — McMap. All rights reserved.