views:

14

answers:

0
float getNewScrollPoint(float displayPt, float scrollPt, float imageSize, float scaleFactor) {
 float imagePt = (displayPt / _Scale) + scrollPt;
 float nuScale = _Scale * scaleFactor;
 float nuDisplayPt = imagePt - (displayPt / nuScale);
 return nuDisplayPt;
}


public boolean onScale(ScaleGestureDetector detector) {
 final float scale = detector.getScaleFactor();
 _ScrollLeft = getNewScrollPoint(detector.getFocusX(), _ScrollLeft, _Image.getWidth(), scale);
 _ScrollTop = getNewScrollPoint(detector.getFocusY(), _ScrollTop, _Image.getHeight(), scale);
 _Scale *= scale;
 capView();
 invalidate();
 return true;
}

Above code is meant to scale a large image and calculate where to position the image so as to keep the focal point of the scaled image at the same point on the display.

_ScrollLeft, _ScrollTop is the offset image is displayed. _Scale is the scaling factor used. capView() just keeps everything within bounds, i.e. _Scale and _ScrollLeft, _ScrollTop. _Image is a Bitmap.

The matrix to display _Image is calculated by:

_Translate.setTranslate(-_ScrollLeft, -_ScrollTop);
_Translate.preScale(_Scale, _Scale);

Any help here would be much appreciated as I have tried several different methods still to no avail.

==============

Okay it should be:

final float getNewScrollPoint(final float displayPt, final float scrollPt, final float imageSize, final float scaleFactor) {
    if (_Scale==0f)
        return scrollPt;
    final float imagePt = (displayPt + scrollPt) / _Scale;
    final float nuScale = capScale(_Scale * scaleFactor);
    final float nuScrollPt = imagePt * nuScale - displayPt;
    Log.d(TAG, "getNewScrollPoint(" + displayPt + ", " + scrollPt + ", " + imageSize + ", " + scaleFactor + "), Values: IPt=" +
            imagePt + ", New Scale=" + nuScale + ", New Scroll=" + nuScrollPt);
    return nuScrollPt;
}

Added in here in case someone else also has a problem with this simple arithmetic.