Android Multi touch zooming

前端 未结 3 1030
野性不改
野性不改 2020-12-05 16:58

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

相关标签:
3条回答
  • 2020-12-05 17:31

    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;
    

    ...

    0 讨论(0)
  • 2020-12-05 17:44

    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);
    
        }
    ....        
    
    0 讨论(0)
  • 2020-12-05 17:56

    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/

    0 讨论(0)
提交回复
热议问题