Search code examples
androidrotationdrawableobjectanimator

How to rotate a drawable by ObjectAnimator?


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.


Solution

  • 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