I am a 2D novice so please forgive this question for being so simple, but at the same time no one seems to be able to give me an answer that works either so maybe it isn't so simple?
All I want to know is given a point on an image that you pan and zoom, what would that point be on the image had it not been panned and zoomed?
I have code that zooms and pans that looks like this. All I want to know is what needs to be done to get the new point (mState.touchX) projected back to what it would be in the original image.
Someone mentioned a formula that I tried to use (see the Log statements for details on it) but that didn't do it so I am asking here in the hope that someone knows how to do this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| private void calculateAspectQuotient() { if (mBitmap != null) { mAspectQuotient = (((float)mBitmap.getWidth()) / mBitmap.getHeight()) / (((float)getWidth()) / getHeight()); } }
@Override protected void onDraw(Canvas canvas) { if (mBitmap != null && mState != null) { final int viewWidth = getWidth(); final int viewHeight = getHeight(); final int bitmapWidth = mBitmap.getWidth(); final int bitmapHeight = mBitmap.getHeight(); final float panX = mState.getPanX(); final float panY = mState.getPanY(); final float zoomX = mState.getZoomX(mAspectQuotient) * viewWidth / bitmapWidth; final float zoomY = mState.getZoomY(mAspectQuotient) * viewHeight / bitmapHeight;
mRectSrc.left = (int)(panX * bitmapWidth - viewWidth / (zoomX * 2)); mRectSrc.top = (int)(panY * bitmapHeight - viewHeight / (zoomY * 2)); mRectSrc.right = (int)(mRectSrc.left + viewWidth / zoomX); mRectSrc.bottom = (int)(mRectSrc.top + viewHeight / zoomY); mRectDst.left = getLeft(); mRectDst.top = getTop(); mRectDst.right = getRight(); mRectDst.bottom = getBottom();
if (mRectSrc.left < 0) { mRectDst.left += -mRectSrc.left * zoomX; mRectSrc.left = 0; } if (mRectSrc.right > bitmapWidth) { mRectDst.right -= (mRectSrc.right - bitmapWidth) * zoomX; mRectSrc.right = bitmapWidth; } if (mRectSrc.top < 0) { mRectDst.top += -mRectSrc.top * zoomY; mRectSrc.top = 0; } if (mRectSrc.bottom > bitmapHeight) { mRectDst.bottom -= (mRectSrc.bottom - bitmapHeight) * zoomY; mRectSrc.bottom = bitmapHeight; }
if(DebugOptions.DEBUG_ENABLED) { Log.d(TAG, "View width: " + viewWidth + " View height: " + viewHeight); Log.d(TAG, "Bmap width: " + bitmapWidth + " Bmap height: " + bitmapHeight); Log.d(TAG, "panX: " + panX + " panY: " + panY);
Log.d(TAG, "mRectSrc.left: " + mRectSrc.left); Log.d(TAG, "mRectSrc.top: " + mRectSrc.top); Log.d(TAG, "mRectSrc.right: " + mRectSrc.right); Log.d(TAG, "mRectSrc.bottom: " + mRectSrc.bottom);
Log.d(TAG, "zoomX: " + zoomX); Log.d(TAG, "zoomY: " + zoomY); double dx = 0.5 * viewWidth - panX * bitmapWidth * zoomX;
Log.d(TAG, "dx = 0.5 * viewWidth - panX * bitmapWidth * zoomX");
Log.d(TAG, "dx = 0.5 * " + viewWidth + " - " + panX + " * " + bitmapWidth + " * " + zoomX + " = " + dx);
Log.d(TAG, "xo = (xt - dx) / zoomX");
double xo = (mState.getTouchX() - dx) / zoomX;
Log.d(TAG, "xo = (" + mState.getTouchX() + " - " + dx + ")" + "/" + zoomX + " = " + xo);
Log.d(TAG, "TouchX: " + mState.getTouchX() + " Translated: " + xo);
} canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint); } } |