title: Rotating an image in a spiral path in android
body: <p>I have use this code to rotate the <code>ImageView</code> in a spiral path. But after rotating the first circle it comes back to the center and start rotating the next circle. How can I avoid moving the image view back to the center? Thank you </p>
  
  <pre><code>public class MainActivity extends ActionBarActivity implements Animation.AnimationListener {
  
  RelativeLayout.LayoutParams par;
  ImageView star;
  TranslateAnimation animation1,animation2,animation3;
  AnimationSet animSet1 = new AnimationSet(false);
  AnimationSet animSet2 = new AnimationSet(true);
  AnimationSet animSet3 = new AnimationSet(true);
  AnimationSet animSet4 = new AnimationSet(true);
  RelativeLayout screen;
  int width = 0;
  int height = 0;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      star = (ImageView)findViewById(R.id.imageView);
      par = (RelativeLayout.LayoutParams)star.getLayoutParams();
      screen = (RelativeLayout) findViewById(R.id.phoneScreen);
  
      DisplayMetrics displaymetrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
      height = displaymetrics.heightPixels;
      width = displaymetrics.widthPixels;
  
  
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  @Override
  public void onAnimationEnd(Animation animation) {
  
  }
  
  @Override
  public void onAnimationRepeat(Animation animation) {
  }
  
  @Override
  public void onAnimationStart(Animation animation) {
  }
  
  public class MyAnimation extends Animation {
  
      private View view;
      private float cx, cy;           // center x,y position of circular path
      private float prevX, prevY;     // previous x,y position of image during animation
      private float r;                // radius of circle
  
  
      /**
       * @param view - View that will be animated
       * @param r - radius of circular path
       */
      public MyAnimation(View view, float r){
          this.view = view;
          this.r = r;
      }
  
      @Override
      public boolean willChangeBounds() {
          return true;
      }
  
      @Override
      public void initialize(int width, int height, int parentWidth, int parentHeight) {
          // calculate position of image center
          int cxImage = width / 2;
          int cyImage = height / 2;
  
          cx = view.getLeft() + cxImage;
          cy = view.getTop() + cyImage;
  
          // set previous position to center
          prevX = cx;
          prevY = cy;
      }
  
      @Override
      protected void applyTransformation(float interpolatedTime, Transformation t) {
          if(interpolatedTime == 0){
              // I ran into some issue where interpolated would be
              return;
          }
  
          float angleDeg = (interpolatedTime * 360f + 90) % 360;
          float angleRad = (float) Math.toRadians(angleDeg);
  
          // r = radius, cx and cy = center point, a = angle (radians)
          float x = (float) (cx + r * Math.cos(angleRad));
          float y = (float) (cy + r * Math.sin(angleRad));
  
  
          float dx = prevX - x;
          float dy = prevY - y;
  
          prevX = x;
          prevY = y;
  
          t.getMatrix().setTranslate(dx, dy);
      }
  }
  public void buttonClick(View v)
  {
      Animation anim = new MyAnimation(star, 150);
      anim.setDuration(4000);
  
      anim.setAnimationListener(new Animation.AnimationListener(){
          @Override
          public void onAnimationStart(Animation arg0) {
          }
          @Override
          public void onAnimationRepeat(Animation arg0) {
  
          }
          @Override
          public void onAnimationEnd(Animation arg0) {
  
              Animation anim2 = new MyAnimation(star, 145);
              anim2.setDuration(4000);
              star.startAnimation(anim2);
          }
      });
      star.startAnimation(anim);
  
  
  }
  }
  </code></pre>