Android SDK - Not Scaling at Centre of Touch

我们两清 提交于 2019-12-04 03:06:01
Veger

In your ScaleListener class you only adjust mScaleFactor so, indeed, you image will indeed not zoom around the center of you multi-touch gesture!

Additionally, you need to update mPosX and mPosY to use a different center of scaling.

For one of my own applications, I used something like this:

final float scale = detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor * scale, 5.0f));

if (mScaleFactor < 5f) {
   // 1 Grabbing
   final float centerX = detector.getFocusX();
   final float centerY = detector.getFocusY();
   // 2 Calculating difference
   float diffX = centerX - mPosX;
   float diffY = centerY - mPosY;
   // 3 Scaling difference
   diffX = diffX * scale - diffX;
   diffY = diffY * scale - diffY;
   // 4 Updating image origin
   mPosX -= diffX;
   mPosY -= diffY;
}

Basically, it calculates the relative change of the origin of your image by

  1. grabbing the origin of your multi-touch gesture (getFocus())
  2. calculating the difference between your image origin (diffX and diffY)
  3. applying the scale factor
  4. and updating the origin of the image by using the scaled difference

This (still) works for my situation. I do not know whether we both use(d) the same coordinate systems, but something like this should help you in properly scaling the image at the center of the multi-touch gesture.

niksi

It took me really long to get this working.. however thanks for almost working code! I'm not sure was I just messing around or what but with this changes in code I got my zooming work like I wanted! Hopefully this will safe someones time.

mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.5f, Math.min(mScaleFactor, 3.0f));
scale = mScaleFactor / scaleAtm


mTranslateMatrix.preTranslate(diffX, diffY);
mTranslateMatrix.invert(mTranslateMatrixInverse);
mPosX += diffX;
mPosY += diffY;
scaleAtm = mScaleFactor;

At the beginning scaleAtm = 1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!