简体   繁体   English

Android多点触控缩放

[英]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 . 我试图通过http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-链接实现多点触控缩放功能。 pinch-zoom-gesture / 1847 ,它实际上是一个很好但我需要控制放大和缩小 ie 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. 还要提前感谢调查这个问题。

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. 在我对缩放图像视图的搜索和编码中,我遇到了一个小部件,它完全是Gallery app缩放功能的副本,易于使用。 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 虽然我已经设计了自己的变焦Iamgeview但后来我发现重新发明下面的轮子并不是很好的事情是链接

Courtesy: Alessandro Crugnola 礼貌:Alessandro Crugnola

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

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: 并且还为您的活动添加变量mCurrentScale以记住当前比例,如下所示:

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;

... ...

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. 0.7和2.0是最小和最大缩放级别。

....
} 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);

    }
....        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM