title: Using Seek Bar to zoom android Canvas
body: <p>this is my first question in stackoverflow. any help would be very much appreciated.
  I am trying zoom a canvas using a seek bar, so depend on the progress of the seekbar, the canvas will zoom in and zoom out.</p>
  
  <p>here is my code in the main activity:</p>
  
  <pre><code>@Override
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      // TODO Auto-generated method stub
      Toast.makeText(MainActivity.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
      CPaintView paint  =  (CPaintView) this.findViewById(R.id.cPaintView1);
  
      paint.zoomX = (float)progress;
      paint.zoomY = (float)progress;
      paint.invalidate();
  }
  </code></pre>
  
  <p>then it will pass the progress value to the CPaintView class which contain the canvas:</p>
  
  <pre><code>public class CPaintView extends View implements OnTouchListener {
  
      final Paint paint = new Paint();
      int color = Color.parseColor("#FF0000");
  
      Bitmap offScreenBitmap;
      Canvas offScreenCanvas;
  
      final int CIRCLE = 0;
      final int TRIANGLE = 1;
      final int SQUARE = 2;
      int shape = CIRCLE;
      private int mActivePointerId;
      float zoomX = 0;
      float zoomY = 0;
  
      public CPaintView(Context context) {
          super(context);
  
          setup();
      }
  
      public CPaintView(Context context, AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
  
          setup();
      }
  
      public CPaintView(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          setup();
      }
  
      public void setup()
      {
          setOnTouchListener(this); // define event listener and start intercepting events 
      }
  
  
      @Override
      protected void onDraw(Canvas canvas) {
          // TODO Auto-generated method stub
          // draw the off screen bitmap
          if(zoomX &gt; 0)
          {
              canvas.save();
              canvas.scale(zoomX, zoomY);
              canvas.drawBitmap(offScreenBitmap, 0, 0, paint);
              canvas.restore();
          }
          else
              canvas.drawBitmap(offScreenBitmap, 0, 0, paint);
      }
  
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          // get the x,y coordinates of the MotionEvent.ACTION_MOVE event
  
          int pointerIndex = 0;
          for(int i = 0; i &lt; event.getPointerCount(); i++)
          {
              mActivePointerId = event.getPointerId(i);
              pointerIndex = event.findPointerIndex(mActivePointerId);
              float x = event.getX(pointerIndex);
              float y = event.getY(pointerIndex);
  
              float y1 = y + 20;
              float x2 = x + 20;
              float y2 = y1 - 10;
              paint.setColor(color);
  
              float verts[] = {x, y, x, y1, x2, y2};
              int[] vertsColors = {color, color, color, color, color, color};
  
  
              switch(shape)
              {
                  case CIRCLE:
                      offScreenCanvas.drawCircle(x, y, 10, paint); break;// draw a red circle at the x,y coordinates specified by the user
                  case TRIANGLE:
                      offScreenCanvas.drawVertices(Canvas.VertexMode.TRIANGLES, verts.length, verts, 0, null, 0, vertsColors, 0, null, 0, 0, paint);break;
                  case SQUARE:
                      offScreenCanvas.drawRect(x-10, y-10, x+10, y+10, paint); break; 
              }
  
              invalidate();
          }
                  return true;
      }
  
  
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
          // create / re-create the off screen bitmap to capture the state of our drawing
          // this operation will reset the user's drawing
              offScreenBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
              offScreenCanvas = new Canvas(offScreenBitmap);
      }
  }
  </code></pre>
  
  <p>I am having problem with the onDraw method. The problem is, when I increase the seekbar progress, the whole canvas disappear and I try to draw on it, it seems like it doesn't draw. But when I decrease the seekbar progress back to 0, all the drawing comes back and I can see it again, including the drawing that seems like not working.</p>
  
  <p>I am sorry for my bad english.</p>
  
  <p>Thank you very much.</p>