讓我開始說我已閱讀了相當數量的縮放問題並重新計算了此處的座標。但我似乎無法將它們應用於我的情況。使用矩陣縮放焦點周圍的圖像視圖的正確方法
我有一個可以縮放和滾動的自定義ImageView類。我遇到的問題是製造出來的順序如下:
- 拉近點,並利用這一點作爲一個聯絡點(這個工程)
- 滾動,同時還放大
- 嘗試放大或者/縮小中的不同點(不起作用)
這裏是自定義視圖的一個例子。有兩個圈:紅色和綠色。紅色是焦點應該是和綠色是焦點在哪裏它接近實際上是。
當我嘗試放大用橙色圈起來的區域時出現問題。在下面的圖片中,我試圖放大橙色圓圈區域(不是很多,只是爲了顯示焦點計算的位置)。如下圖所示,紅圈正確計算新焦點,但由於某些原因,綠圈實際上是放大/縮小的位置。
我使用下面的代碼來縮放座標與實際ImageView的地圖座標
public boolean onScale(ScaleGestureDetector detector) {
//update the current scale
scaleFactor *= detector.getScaleFactor();
scaleFactor = Math.max(ZOOM_LEVEL_4, Math.min(scaleFactor, ZOOM_LEVEL_0));
//applying the scaleFactor to the focal point as determined in onScaleBegin
transformMatrix.setScale(scaleFactor, scaleFactor,
newFocalPoints[0], newFocalPoints[1]);
//apply the matrix to the child
child.transform(transformMatrix, newFocalPoints[0], newFocalPoints[1],
oldFocalPoints[0], oldFocalPoints[1]);
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector){
//when a new scaling process begins, get the current imageMatrix and
//map the points to account for the current zoom level
//the initial points. based on screen location and current scroll pos
float startX = detector.getFocusX() + getScrollX();
float startY = detector.getFocusY() + getScrollY();
oldFocalPoints = new float[]{startX, startY};
//map oldFocalPoints to coordinates of the imageView based on current zoom
Matrix inverseTransformMatrix = new Matrix();
if(transformMatrix.invert(inverseTransformMatrix))
inverseTransformMatrix.mapPoints(newFocalPoints, oldFocalPoints);
return true;
}
在上面的圖片綠點被設定在{oldCoordinates[0], oldCoordinates[1]}
進行調試,並雖然它並不完全是焦點,但它非常接近。所以看起來,儘管我似乎正確計算了新的焦點(紅色圓圈),但似乎並沒有正確應用。任何人都可以發現錯誤?提前致謝!
非常感謝,我沒有最後一次轉換就拔出我的頭髮... – loadedion
是當前計算點在內部或在其他地方計算? – ShrimpCrackers