title: Animating bitmap ball - hit the wall and change direction effect
body: <p>I am trying to animate a bitmap. I have a ball, and move it. When it reaches to the boundaries of the screen, i want it to change its direction like a mirror effect, i mean like this, think of the ball as the dots below, it comes down, hits the wall and changes its direction.</p>
  
  <pre><code>.          .
   .       .
    .    .
  ____._._______
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>public class DrawShapes extends View{
  
  Bitmap ball;
  int x,y;
  
  public DrawShapes(Context context) {
      super(context);
      ball=BitmapFactory.decodeResource(getResources(),R.drawable.ball);
      x=0;
      y=0;
  }
  
  @Override
  protected void onDraw(Canvas canvas) {      
      super.onDraw(canvas);       
  
      if(x&lt;canvas.getWidth())
          x+=5;
      else
          x-=5;
      if(y&lt;canvas.getHeight())
          y+=5;
      else
          y-=5;
      canvas.drawBitmap(ball, x, y, new Paint());
      invalidate();
  
  }
  </code></pre>
  
  <p>The problem is, after the ball reaches to a boundary, it keeps going and goes out of screen and never comes back. Can anyone help me with this?</p>
  
  <p>Thanks</p>