Search code examples
androidimageanimationimageviewrotateanimation

RotateAnimation.setFillAfter(true) and .setFillEnabled(true) does not work


I'm trying to rotate 3 imageViews with a rotateAnimation. I calculate the degrees to rotate every 30ms. If the angle changed, I create the rotateAnimation:

rpmAnim=new RotateAnimation((float)Rpmcurrentdegree, (float)Rpmdegree, ivNadel.getWidth()/2, ivNadel.getHeight()/2);
rpmAnim.setFillEnabled(true);
rpmAnim.setFillAfter(true);

...and then I start the animation of the imageView:

 ivNadel.startAnimation(rpmAnim);

The rotation works fine, but when the degrees do not change, it jumps back to its starting position. Does anyone know why?


Solution

  • set the setAnimationListener on rpmAnim:

    rpmAnim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
    
              // here rotate the image
    
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {}
            @Override
            public void onAnimationStart(Animation animation) {}
        });
    

    to rotate the image you can

    1. extends ImageView in order to create your custom image view
    2. override onDraw
    3. set your rotation angle
    4. invalidate your custom image view

    the overridden onDraw could look like:

     protected void onDraw(Canvas canvas) {
         canvas.save()
         canvas.rotate(your rotation angle...)
         super.onDraw(canvas)
         canvas.restore()
     }