title: How do you animate a drawn circle in java
body: <p>Im making an app that displays multiple random circles on the screen. I want to know if i can expand the radius WHILE it is displaying the circle then disappears.
  So the code below works there is just one major problem. 100's of circles are drawn in the given one second that it has to draw them. If I remove the cool.postDelayed it only draws once. </p>
  
  <pre><code>public class SplashLaunch extends View{
      Handler cool = new Handler();
      DrawingView v;
      Paint newPaint = new Paint();
      int randomWidthOne = 0;
      int randomHeightOne = 0;
      private static int radiusOne = 300;
      final int redColorOne = Color.RED;
      final int greenColorOne = Color.GREEN;
      private static int lastColorOne;
      ObjectAnimator radiusAnimator;
      private final Random theRandom = new Random();
      public SplashLaunch(Context context) {
          super(context);
          // TODO Auto-generated constructor stub
      }
  
      private final Runnable circleUpdater = new Runnable() {
          @Override 
          public void run() {
              lastColorOne = theRandom.nextInt(2) == 1 ? redColorOne : greenColorOne;
              newPaint.setColor(lastColorOne);           
              startAnimation(500);
              cool.postDelayed(this, 1000);
              invalidate();
          }
      };
  
      @Override
      protected void onAttachedToWindow(){
          super.onAttachedToWindow();
          cool.post(circleUpdater);
      }
      protected void onDetachedFromWindow(){
          super.onDetachedFromWindow();
          cool.removeCallbacks(circleUpdater);
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          // TODO Auto-generated method stub
          super.onDraw(canvas);
  
          if(theRandom == null){
              randomWidthOne =(int) (theRandom.nextInt(Math.abs(getWidth()-radiusOne/2)) + radiusOne/2f);
              randomHeightOne = (theRandom.nextInt((int)Math.abs((getHeight()-radiusOne/2 + radiusOne/2f))));
          }else {
              randomWidthOne =(int) (theRandom.nextInt(Math.abs(getWidth()-radiusOne/2)) + radiusOne/2f);
              randomHeightOne = (theRandom.nextInt((int)Math.abs((getHeight()-radiusOne/2 + radiusOne/2f))));
          }
          canvas.drawCircle(randomWidthOne, randomHeightOne, radiusOne, newPaint);
      }
  
  
  
      public void setRadius(float value){
          this.radiusOne = (int) value;
          invalidate();
      }
  
  
      public void startAnimation(int animationDuration) {
  
          if (radiusAnimator == null || !radiusAnimator.isRunning()) {
  
              // Define what value the radius is supposed to have at specific time values
              Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
              Keyframe kf2 = Keyframe.ofFloat(0.5f, 180f);
              Keyframe kf1 = Keyframe.ofFloat(1f, 360f);
  
              // If you pass in the radius, it will be calling setRadius method, so make sure you have it!!!!!
              PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("radius", kf0, kf1, kf2);
              radiusAnimator = ObjectAnimator.ofPropertyValuesHolder(this, pvhRotation);
              radiusAnimator.setInterpolator(new LinearInterpolator());
              radiusAnimator.setDuration(animationDuration);
              radiusAnimator.start();
          }
          else {
              Log.d("Circle", "I am already running!");
          }
      }
  
      public void stopAnimation() {
          if (radiusAnimator != null) {
              radiusAnimator.cancel();
              radiusAnimator = null;
          }
      }
  
      public boolean getAnimationRunning() {
          return radiusAnimator != null &amp;&amp; radiusAnimator.isRunning();
      }
  
  }
  </code></pre>