title: Android: onDraw constantly called or animation snaps back to original position
body: <p>So, my question is somewhat similar to these two questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/8169081/custom-view-ondraw-is-constantly-called">Custom View onDraw is constantly called</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7814502/android-ondraw-is-called-constantly">android: onDraw is called constantly</a></p>
  
  <p>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):</p>
  
  <pre><code>@Override
  protected void onDraw(Canvas canvas) {
      Log.d(TAG, "It is drawn again!");
      this.setAnimation(anim);
      super.onDraw(canvas);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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):</p>
  
  <pre><code>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 &gt; 360 ){
          current_angle = current_angle - 360;
      }
      if ( current_angle &lt; -360 ){
          current_angle = current_angle + 360;
      }
  
      this.setAnimation(anim);
      this.invalidate();  //Calls onDraw()
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Where is the best place to call this.setAnimation(anim)?</p>