title: Centered zoom on Android
body: <p>I've managed to create a Zoom (extends View) in order to load a PNG into a Drawable object and have it on the screen with zooming and panning function. See the code of the View (Zoom.java) :</p>
  
  <pre><code>public class Zoom extends View {
  
  private Drawable image;
  private int zoomControler = 200;
  private int shiftleft = 0;
  private int shiftup = 0 ;
  public float oldX = 0;
  public float oldY = 0;
  
  public Zoom(Context context) {
      super(context);
      image = context.getResources().getDrawable(R.drawable.androidmarker);
      setFocusable(true);
  
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      image.setBounds(getWidth() / 2 - zoomControler - shiftleft,
               getHeight() / 2 - zoomControler - shiftup,
              getWidth()/ 2 + zoomControler - shiftleft,
              getHeight()/ 2 + zoomControler - shiftup);
      image.draw(canvas);
  }
  
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
  
      if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
          zoomControler += 10;
      if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
          zoomControler -= 10;
      if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) 
      {
  
          shiftleft +=10;
  
      }
      if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
      {
          shiftleft-=10;
      }
  
      if (zoomControler &lt; 10)
          zoomControler = 10;
  
      invalidate();
      return true;
  }
  
  
  @Override
  public boolean onTouchEvent(MotionEvent e) {
  
      int action = e.getAction();
  
      switch (action) {
      case MotionEvent.ACTION_DOWN:
          oldX = e.getX();
          oldY = e.getY();
          break;
      case MotionEvent.ACTION_MOVE:
          shiftleft+=(int) (-(e.getX() - oldX) ) / 2;
          shiftup+=(int) (-(e.getY() - oldY) / 2);
          break;
      case MotionEvent.ACTION_UP:
          break;
      case MotionEvent.ACTION_CANCEL:
          break;
      }
      invalidate();
      return true;
  }
  }
  </code></pre>
  
  <p>The problem is how I get a screen centered zoom ? For the moment, the center of the zoom is always the center of the PNG file...I don't know how to modified setBounds in order to get this behaviour... Any idea ?
  Thanks ;)</p>