Basil
Basil

Reputation: 2173

Android Multi touch zooming

I am trying to implement multi touch zoom in and zoom out functionality to my application. My application is to view an slideshow of images. I tried to implement multi touch zoom functionalities from the link following http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/1847 ,its actually a good one but I need to control the zoom in and zoom out. i.e I want to implement an maximum and minimum limit for zoom. Can anybody help me to solve this issue. Also thanks in advance to looking into this issue.

Upvotes: 3

Views: 13284

Answers (3)

Arjun
Arjun

Reputation: 371

This fixed the problem for me. After the image is zoomed in/out to the limit, it just stops going further. It's also smooth and there are no lags. 0.7 and 2.0 are the 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);

    }
....        

Upvotes: 1

ingsaurabh
ingsaurabh

Reputation: 15269

In my search and coding for the zooming imageview I came across a widget that is exactly copy of Gallery app zooming feature its easy to use. Although I have devised my own zoom Iamgeview but later I find its not agood thing to re- invent the wheel below is the link

Courtesy: Alessandro Crugnola

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

Upvotes: 9

Joseph Earl
Joseph Earl

Reputation: 23432

Sure, you'll 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 mCurrentScale to your activity to remember to current scale like so:

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;

...

Upvotes: 5

Related Questions