title: Draw in bitmap using canvas doesn't work in android view
body: <p>I'm trying to use <code>onDraw</code> method and canvas to draw some items in a bitmap and cache it to draw it again and don't call <code>onDraw</code> again </p>
  
  <p>This is part of my code :</p>
  
  <pre><code>@Override
      protected void onDraw(Canvas canvas) {
  
          super.onDraw(canvas);
  
          if (mCacheDrawing == null) {
              mCacheDrawing = Bitmap.createBitmap(mScrollWidth, mScrollHeight, Config.RGB_565);
              canvas.setBitmap(mCacheDrawing);
  
              for (int i = 0; i &lt; mIcons.size(); i++) {
  
                  prepareItem(canvas, paint, mIcons.get(i));
              }
  
              canvas.save();
          } else {
              canvas.setBitmap(mCacheDrawing);
          }
      }
  </code></pre>
  
  <p>The code is not working and show me an empty screen, can any one help me please?</p>
  
  <p>EDIT : I have found the following post and it help me to solve the problem >> <a href="https://groups.google.com/forum/#!topic/android-beginners/6pO8SJN3CTY" rel="nofollow">https://groups.google.com/forum/#!topic/android-beginners/6pO8SJN3CTY</a></p>
  
  <p>and my working code now is following :</p>
  
  <pre><code>@Override
      protected void onDraw(Canvas canvas) {
  
          super.onDraw(canvas);
  
          if (mCacheDrawing == null) {
  
              mCacheDrawing = Bitmap.createBitmap(mScrollWidth, mScrollHeight, Config.RGB_565);
              mCanvas = new Canvas(mCacheDrawing);
              for (int i = 0; i &lt; mIcons.size(); i++) {
  
                  prepareItem(mCanvas, paint, mIcons.get(i));
              }
          } 
  
          canvas.drawBitmap(mCacheDrawing,new Rect (0,0, mScrollWidth, mScrollHeight), new Rect (0,0, mScrollWidth, mScrollHeight), paint);
      }
  </code></pre>