title: Pinch Zoom ListView Android
body: <p>I'm trying to implement a pinch to zoom in a listview on Android and I'm facing a problem when I click and drag the zoomed list view.
  First, here's the code:</p>
  
  <pre><code>public class ScaleListView extends ListView {
  
  private ScaleGestureDetector mScaleDetector;
  private float mScaleFactor = 1.f;
  private float x = 0;
  private float y = 0;
  private float startx = 0;
  private float starty = 0;
  private boolean canClick = true;
  private float xtranslation = 0;
  
  public ScaleListView(Context context, AttributeSet attrs) {
      super(context, attrs);
      mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
      if (ev.getAction() == MotionEvent.ACTION_DOWN) {
          startx = ev.getX();
          starty = ev.getY();
      }
  
      if (ev.getPointerCount() &gt; 1) {
          y = ev.getY();
          x = ev.getX();
      } else if (ev.getAction() == MotionEvent.ACTION_UP) {
          canClick = (ev.getX() - startx) == 0 &amp;&amp; (ev.getY() - starty) == 0 &amp;&amp; mScaleFactor == 1;
      } else if(ev.getAction() == MotionEvent.ACTION_MOVE){
          xtranslation = ev.getX() - startx;
      }       
      mScaleDetector.onTouchEvent(ev);
      return super.onTouchEvent(ev);
  }   
  @Override
  public void onDraw(Canvas canvas) {
      canvas.scale(mScaleFactor, mScaleFactor, x , y);
      canvas.translate(xtranslation, 0);
      super.onDraw(canvas);
  }
  private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
      @Override
      public boolean onScale(ScaleGestureDetector detector) {
          canClick = false;
          mScaleFactor *= detector.getScaleFactor();
  
          // Don't let the object get too small or too large.
          mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
  
          if (mScaleFactor &lt;= 1) {
              mScaleFactor = 1;
              canClick = true;
          }
  
          xPos = ScaleListView.this.getWidth() - detector.getFocusX();
  
          ScaleListView.this.invalidate();
          return true;
      }
  }   
  public boolean canClick(){
      return canClick;
  }
  </code></pre>
  
  <p>The problem is with the translation. It goes infinite, I can drag it horizontally and take the list view off the viewport. So I can't face how to put an edge to the translation...
  Thanks for any help.</p>