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 7056835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:54:51+00:00 2026-05-28T03:54:51+00:00

So, my question is somewhat similar to these two questions: Custom View onDraw is

  • 0

So, my question is somewhat similar to these two questions:

Custom View onDraw is constantly called

android: onDraw is called constantly

I have a custom class extending ImageView which I apply a RotateAnimation to. The animation uses input x- and y-coordinates to perform a rotation from the last angle to the next so the user can turn the ImageView as wanted, from -360 degrees to 360 degrees. When I use this code for onDraw() everything looks perfect on screen (the animation is set as in the code further below):

@Override
protected void onDraw(Canvas canvas) {
    Log.d(TAG, "It is drawn again!");
    this.setAnimation(anim);
    super.onDraw(canvas);
}

The problem with this is that, in the same manner as in the other posts mentioned, the animation calls onDraw which calls animation and so forth, probably through invalidate() in the RotateAnimation class. Is this correctly observed? The output is perfect since the ImageView always stays at the current calculated angle, but the animation calulations thus keep on going, consuming lots of power and capacity.

To fix this I tried to move the this.setAnimation(anim) in the method where the parameters for the animation are calculated (please ignore isClockWise(), calculateAngleToMove() and other non-android stuff, they work as intended):

private void turnWheel(){
    float angle = 0;

    if ( isClockWise() ){
        angle = calculateAngleToMove();         
        anim = new RotateAnimation(current_angle, angle, center_x, center_y);
        anim.setFillAfter(true);
        anim.setFillEnabled(true);
        current_angle += angle; 
    }
    else{
        angle = - calculateAngleToMove();
        anim = new RotateAnimation(current_angle, angle, center_x, center_y);
        anim.setFillAfter(true);
        anim.setFillEnabled(true);
        current_angle += angle;
    }

    if ( current_angle > 360 ){
        current_angle = current_angle - 360;
    }
    if ( current_angle < -360 ){
        current_angle = current_angle + 360;
    }

    this.setAnimation(anim);
    this.invalidate();  //Calls onDraw()

}

This solves the problem with onDraw constantly being called, but it creates another: when the user pushes, holds and turns the ImageView, it snaps back and forth between zero angle and the current angle. When the user then lets go of the ImageView, it snaps back to zero angle. It is wanted to always have the ImageView rotated at the variable current_angle, also when the user doesn’t provide input.

I have tried different versions of anim.setFillAfter(true), anim.setFillEnabled(true), invalidate() and this.startAnimation(anim), but they never seem to have any effect on this problem.

Where is the best place to call this.setAnimation(anim)?

  • 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-28T03:54:52+00:00Added an answer on May 28, 2026 at 3:54 am

    I might recommend not using the Animation framework to apply static transformations to views so that you don’t have to deal with the issues caused by resetting when you apply a new Animation each time.

    If the contents you wish to rotate just includes imagery, consider wrapping your image(s) in a RotateDrawable and set that as the content of your ImageView. With this you can control how the drawable rotates by setting its level value (either Drawable.setLevel() or ImageView.setImageLevel() should do the trick) and the transformation will stick. If you have a need to automate the animation for a period, you can simply call the level setting code inside of a Handler that posts the updates every so often.

    Another option would be to create a custom ViewGroup and use the getChildStaticTransformation() method to apply the rotation to any child view (be sure to enable it or override a subclass that already has it enabled). Transformation is what the Animation framework uses to modify view appearance as well. In this case, you would likely still need to call invalidate() when the user input changed in order to force the re-draw. The same Handler rule would apply if you wanted to automate the changes periodically.

    RotateDrawable Example

    Let’s say the image you are wanting to rotate is at res/drawable/wheel.png, create a simple XML file at res/drawable/wheel_rotate.xml:

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/wheel"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%" />
    

    To wrap your image into a RotateDrawable. Then, in your Java code set that drawable as the content of an ImageView:

    ImageView wheel;
    //You only do this once
    wheel.setImageResource(R.drawable.wheel_rotate);
    
    //Adjust the progress any time you need to by adjusting the drawable's level
    wheel.setImageLevel(500);
    

    By default, a level ranges from 0 to 10,000. So that maps to 0 level = fromDegrees and 10,000 level = toDegrees.

    HTH

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

Sidebar

Related Questions

I have a question about Django, unixODBC, FreeTDS, Apache2, mod_wsgi, that is somewhat similar
I have seen many somewhat similar questions, but nothing quite what I'm looking for.
Somewhat similar to this question , except we haven't decided that we're going with
I've a somewhat silly question, if i have a series of processes that are
My question is somewhat similar to this one. We want to know if there's
Someone else has already asked a somewhat similar question: Validate an Xml file against
so I know there are somewhat similar questions on here, but I haven't been
I noticed there was a question somewhat similar to mine, only with c#: link
I referred to this somewhat similar question . However here the scenario is different:
My question is somewhat similar to Group by every N records in T-SQL however

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.