title: Android draw using SurfaceView and Thread
body: <p>I am trying to draw a ball to my screen using 3 classes. I have read a little about this and I found a code snippet that works using the 3 classes on one page, <a href="https://web.archive.org/web/20121204020009/http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html" rel="nofollow noreferrer">Playing with graphics in Android</a></p>
  
  <p>I altered the code so that I have a ball that is moving and shifts direction when hitting the wall like the picture below <strong>(this is using the code in the link)</strong>.</p>
  
  <p><img src="https://i.stack.imgur.com/SPy4V.png" alt="moving ball screenshot"></p>
  
  <p>Now I like to separate the classes into 3 different pages for not making everything so crowded, everything is set up the same way.</p>
  
  <p><strong>Here are the 3 classes I have.</strong></p>
  
  <ol>
  <li>BallActivity.java</li>
  <li>Ball.java</li>
  <li>BallThread.java</li>
  </ol>
  
  <hr>
  
  <pre><code>package com.brick.breaker;
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Window;
  import android.view.WindowManager;
  
  
  public class BallActivity extends Activity {
  
  private Ball ball;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
  
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
      ball = new Ball(this);
      setContentView(ball);
  }
  
  @Override
  protected void onPause() {
  
      super.onPause();
  
      setContentView(null);
      ball = null;
  
      finish();
  }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package com.brick.breaker;
  
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  
  public class Ball extends SurfaceView implements SurfaceHolder.Callback {
  
  private BallThread ballThread = null;
  
  private Bitmap bitmap;
  
  private float x, y;
  private float vx, vy;
  
  public Ball(Context context) {
      super(context);
  
      bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
  
      x = 50.0f;
      y = 50.0f;
  
      vx = 10.0f;
      vy = 10.0f;
  
      getHolder().addCallback(this);
      ballThread = new BallThread(getHolder(), this);
  }
  
  protected void onDraw(Canvas canvas) {
  
      update(canvas);
  
      canvas.drawBitmap(bitmap, x, y, null);
  }
  
  public void update(Canvas canvas) {
  
      checkCollisions(canvas);
  
      x += vx;
      y += vy;
  }
  
  public void checkCollisions(Canvas canvas) {
  
      if(x - vx &lt; 0) {
  
          vx = Math.abs(vx);
  
      } else if(x + vx &gt; canvas.getWidth() - getBitmapWidth()) {
  
          vx = -Math.abs(vx);
      }
  
      if(y - vy &lt; 0) {
  
          vy = Math.abs(vy);
  
      } else if(y + vy &gt; canvas.getHeight() - getBitmapHeight()) {
  
          vy = -Math.abs(vy);
      }
  }
  
  public int getBitmapWidth() {
  
      if(bitmap != null) {
  
          return bitmap.getWidth();
  
      } else {
  
          return 0;
      }
  }
  
  public int getBitmapHeight() {
  
      if(bitmap != null) {
  
          return bitmap.getHeight();
  
      } else {
  
          return 0;
      }
  }
  
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
          int height) {
  
  }
  
  public void surfaceCreated(SurfaceHolder holder) {
  
      ballThread.setRunnable(true);
      ballThread.start();
  
  }
  
  public void surfaceDestroyed(SurfaceHolder holder) {
  
      boolean retry = true;
      ballThread.setRunnable(false);
  
      while(retry) {
  
          try {
  
              ballThread.join();
              retry = false;
  
          } catch(InterruptedException ie) {
  
              //Try again and again and again
          }
  
          break;
      }
  
      ballThread = null;
  
  }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package com.brick.breaker;
  
  import android.graphics.Canvas;
  import android.view.SurfaceHolder;
  
  public class BallThread extends Thread {
  
  private SurfaceHolder sh;
  private Ball ball;
  
  private Canvas canvas;
  
  private boolean run = false;
  
  public BallThread(SurfaceHolder _holder,Ball _ball) {
  
      sh = _holder;
      ball = _ball;
  }
  
  public void setRunnable(boolean _run) {
  
      run = _run;
  }
  
  public void run() {
  
      while(run) {
  
          canvas = null;
  
          try {
  
              canvas = sh.lockCanvas(null);
  
              synchronized(sh) {
  
                  ball.onDraw(canvas);
              }
  
          } finally {
  
              if(canvas != null) {
  
                  sh.unlockCanvasAndPost(canvas);
              }
  
          }
  
      }
  }
  
  public Canvas getCanvas() {
  
      if(canvas != null) {
  
          return canvas;
  
      } else {
  
          return null;
      }
  }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Here is a picture that shows the outcome of these classes.</strong></p>
  
  <p><img src="https://i.stack.imgur.com/FVLXB.png" alt="enter image description here"></p>
  
  <p>I've tried to figure this out but since I am pretty new to Android development I thought I could ask for help.</p>
  
  <p>Does any one know what is causing the ball to be draw like that?
  The code is pretty much the same as the one in the link and I have tried to experiment to find a solution but no luck.</p>