Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4627336
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:33:12+00:00 2026-05-22T03:33:12+00:00

I want to rotate an imageview from 30 degrees on each click on a

  • 0

I want to rotate an imageview from 30 degrees on each click on a button.

On the first clic, i can set the animation properly but i can’t succeed to update the imageview position after the animation. When i clicked again on the button, the animation start from the original position of the imageview and not from the final position after the first animation.

Here is my code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    turnImg = (ImageView) findViewById(R.id.imageViewturnImg );
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.imgTurn);
    // Getting width & height of the given image.
    int w = bmp.getWidth();
    int h = bmp.getHeight();

    turnImg .setImageBitmap(bmp);

    Button buttonok = (Button) findViewById(R.id.buttonok);
    buttonok.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            turn();
        }
    });

}

public void turn()
{
    float degrees = 30; 

    RotateAnimation anim = new RotateAnimation(0, degrees,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setDuration(300);
    anim.setFillEnabled(true);

    anim.setFillAfter(true);

    anim.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationEnd(Animation arg0) {

        Matrix mat = turnImg.getImageMatrix();
            mat.postRotate(30,turnImg.getWidth()/2, turnImg.getHeight()/2);
            turnImg.setScaleType(ScaleType.MATRIX);
            turnImg.setImageMatrix(mat);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {}
        @Override
        public void onAnimationStart(Animation animation) {}
    });


    turnImg.startAnimation(anim);

}

I think the problem came from the way i actualize the position in the onAnimationEnd().

ps : sorry for my bad english…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:33:13+00:00Added an answer on May 22, 2026 at 3:33 am

    The problem is that your animation is not affecting the same object as your matrix rotation. That is, you are animating the rotation of the ImageView object, but then you are setting the rotation of the image that is within that ImageView object. So, for example, the first time you rotate it, the ImageView rotates from 0 to 30 degrees, and then remains at a rotation of 30 degrees due to the call to setFillAfter(true). Then the onAnimationEnd() handler runs, which rotates the internal image by 30 degrees. This effectively jumps the image to a rotation of 60 degrees, as seen by the user (30 for the View, 30 for the bitmap). Then the next time the animation runs, it rotates the view from 0 to 30 degrees, with the internal image still at its rotation of 30 degrees. This makes it look to the user like it’s rotating from 30 to 60 degrees. Then you apply another rotation on the internal image when that animation finishes, ending up with the image rotated to 60 degrees and the view rotated (again) to 30 degrees, so the image is now visually rotated to 90 degrees.

    And so on.

    There are different ways to handle this problem, but one simple way is to avoid affecting the two different objects – just work with the ImageView. Instead of rotating from 0 to 30 each time, rotate from whatever the current rotation is to that value plus 30 degrees. You can remove the onAnimationEnd() handler, because you will no longer need to rotate the image inside the view.

    The resulting code for turn() is much simpler:

    public void turn()
    {
        RotateAnimation anim = new RotateAnimation(currentRotation, currentRotation + 30,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
        currentRotation = (currentRotation + 30) % 360;
    
        anim.setInterpolator(new LinearInterpolator());
        anim.setDuration(1000);
        anim.setFillEnabled(true);
    
        anim.setFillAfter(true);
        turnImg.startAnimation(anim);
    }
    

    This code assumes an instance variable of currentRotation, which is used to track the last degrees the view was rotated to.
    Its a bit more involved if you allow the user to click mid-animation, but not too difficult.

    By the way, this is much simpler in the animation system in 3.0; there is now a ‘rotation’ property on View, and you can run an animation on that property and it can track its own current value. But the approach above should work for older releases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want rotate a element with css3 animation and transform property,which from 20 degree
I want to rotate an UIButton at 180 degrees clockwise. But it always rotate
I want to rotate an image using ImageView.setImageMatrix(matrix) but it simply doesn't have any
I want to rotate a UIImageView by roughly 10 degrees left/right but have a
I want to rotate imageview with continuously looping on android.my code rotate is working
I want to rotate the log like in mySQL, but now for an H2
I want to rotate a single word of text by 90 degrees, with cross-browser
I have an object which I first want to rotate (about its own center)
I want to rotate an image like with loader animation GIF. I have <img>
I Want to rotate UIImage in 90 degrees with this method : static inline

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.