title: Canvas SurfaceView
body: <p>I have custom SurfaceView, setting a background in onDraw method, then drawing canvas, but see only background. How can I draw with canvas on a background?
  Tried to add a background with canvas, it works, but at top left 2-3dp was deleted, and redrawing every time background will eat a lot of memory.
  So can it be done with 1st method or 2nd without empty spaces on top left?</p>
  
  <p>Edit...</p>
  
  <p>GameView.java</p>
  
  <pre><code>public class GameView extends SurfaceView {
      private Bitmap background;
      private SurfaceHolder holder;
      private GameManager gameLoopThread;
  
      public GameView(Context context, AttributeSet attrs) {
          super(context, attrs, 0);
          setWillNotDraw(false);
          gameLoopThread = new GameManager(this);
          holder = getHolder();
          holder.addCallback(new SurfaceHolder.Callback() {
              @Override
              public void surfaceDestroyed(SurfaceHolder holder) {
                  boolean retry = true;
                  gameLoopThread.setRunning(false);
                  while (retry) {
                      try {
                          gameLoopThread.join();
                          retry = false;
                      } catch (InterruptedException e) {
                      }
                  }
              }
  
              @Override
              public void surfaceCreated(SurfaceHolder holder) {
                  gameLoopThread.setRunning(true);
                  gameLoopThread.start();
              }
  
              @Override
              public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
              }
          });
  
          background = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
      }
  
      public void onDraw(Canvas canvas) {
          canvas.drawBitmap(background, 0, 0, null);
      }
  
      public void drawCanvas(Canvas canvas) {
          if (x &lt; getWidth() - 10) {
              x += 10;
          } else {
              gameLoopThread.setRunning(false);
          }
          canvas.drawLine(0, 200, x, 200, paint);
      }
  }
  </code></pre>
  
  <p>GameManager.java</p>
  
  <pre><code>public class GameManager extends Thread {
      private GameView view;
      private boolean running = false;
  
      public GameManager(GameView view) {
          this.view = view;
      }
  
      public void setRunning(boolean run) {
          running = run;
      }
  
      @Override
      public void run() {
          while (running) {
              Canvas c = null;
              try {
                  c = view.getHolder().lockCanvas();
                  synchronized (view.getHolder()) {
                      view.drawCanvas(c);
                  }
              } finally {
                  if (c != null) {
                      view.getHolder().unlockCanvasAndPost(c);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.main);
          LinearLayout ll = (LinearLayout) findViewById(R.id.main);
          ll.setLayoutParams(new LinearLayout.LayoutParams(640, 400));
      }
  }
  </code></pre>
  
  <p>Main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:gravity="center_horizontal|top"&gt;
  
      &lt;LinearLayout
          android:id="@+id/main"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"&gt;
  
          &lt;com.android.GameView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The result is <a href="http://oi57.tinypic.com/15znrpy.jpg" rel="nofollow">http://oi57.tinypic.com/15znrpy.jpg</a></p>
  
  <p>If I comment GameView onDraw method and add canvas.drawBitmap(background, 0, 0, null); to drawCanvas the result will be
  <a href="http://oi60.tinypic.com/t0g802.jpg" rel="nofollow">http://oi60.tinypic.com/t0g802.jpg</a></p>