Android multi touch zoom

I am trying to implement multiple click zoom in and out functions for my application. My application is viewing a slide show of images. I tried to implement multi-touch scaling functions at the link http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom -gesture / 1847 , its really good, but I need to control the increase and zoom out . ie I want to implement a maximum and minimum limit for scaling . Can anyone help me solve this problem. Also consider this issue in advance.

+3
source share
3 answers

In my search and coding for scaling an image, I came across a widget that accurately copies the scaling function of the Gallery application, its ease of use. Although I developed my own zoom Iamgeview, but later I find that his ill-conceived thing to reinvent the wheel below is the link

Courtesy: Alessandro Krugnola

http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/

+9
source

Of course, you will need to change this part:

else if (mode == ZOOM) {
      float newDist = spacing(event);
      Log.d(TAG, "newDist=" + newDist);
      if (newDist > 10f) {
         matrix.set(savedMatrix);
         float scale = newDist / oldDist;
         matrix.postScale(scale, scale, mid.x, mid.y);
      }
}

to

else if (mode == ZOOM) {
      float newDist = spacing(event);
      Log.d(TAG, "newDist=" + newDist);
      if (newDist > 10f) {
         matrix.set(savedMatrix);
         float scale = newDist / oldDist;

         // check scale is not too big or two small
         float newScale = scale*mCurrentScale;
         if (newScale > 10) {
             return false;
         } else if (newScale < 0.1) {
             return false;
         }
         mCurrentScale = newScale;

         matrix.postScale(scale, scale, mid.x, mid.y);
      }
}

and also add a variable mCurrentScaleto your activity to remember the current scale as follows:

public class Touch extends Activity implements OnTouchListener {
   // These matrices will be used to move and zoom image
   Matrix matrix = new Matrix();
   Matrix savedMatrix = new Matrix();

   // this is the new variable added
   float mCurrentScale = 1.0f;

...

+5
source

This fixed the problem for me. After the image is zoomed to the limit, it simply stops moving on. It is also smooth and no lag. 0.7 and 2.0 - minimum and maximum zoom levels.

....
} else if (mode == ZOOM) {
    float[] f = new float[9];

    float newDist = spacing(event);
    if (newDist > 10f) {
            matrix.set(savedMatrix);
            float tScale = newDist / dist;
            matrix.postScale(tScale, tScale, mid.x, mid.y);
    }

    matrix.getValues(f);
    float scaleX = f[Matrix.MSCALE_X];
    float scaleY = f[Matrix.MSCALE_Y];

    if(scaleX <= 0.7f)
            matrix.postScale((0.7f)/scaleX, (0.7f)/scaleY, mid.x, mid.y);
    else if(scaleX >= 2.5f) 
            matrix.postScale((2.5f)/scaleX, (2.5f)/scaleY, mid.x, mid.y);

    }
....        
+1
source

All Articles