我正在研究一個將向下移動的網格移動棋子(類似於國際象棋)的遊戲。我希望用戶能夠平移和縮放地圖。我一直在對矩陣進行轉換,然後將這些更改連接到畫布矩陣。雖然這主要是工作,我有一個奇怪的錯誤:導致跳躍位置的Android Canvas ScaleFocus
初始規模後,如果我捏再次縮放,屏幕跳躍(就好像一個刷卡別的地方,它會立即搬到那裏),然後順利地磅秤。我已經縮小這種行爲倒在尺度函數
canvas.scale(scaleFactor, scaleFactor, scaleFocusX, scaleFocusY);
如果我設置scaleFocusX和scaleFocusY爲0的scaleFocus變量,則始終使用原點爲重點的規模和跳躍不會發生。但是,使用原點並不實用,因爲您向前滾動距離較遠。這是我的代碼的總結。
public void onDraw(Canvas canvas) {
...
canvas.translate(-mPosX, -mPosY);
canvas.scale(scaleFactor, scaleFactor, scaleFocusX, scaleFocusY);
//Create an inverse of the tranformation matrix, to be used when determining click location.
canvas.getMatrix().invert(canvasMatrix);
... }
public class MyOnScaleGestureListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
scaleFactor = Math.max(MIN_SCALE, Math.min(scaleFactor, MAX_SCALE));
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
Point scaleFocus = calculateClickAbsoluteLocation
(detector.getFocusX(), detector.getFocusY());
scaleFocusX = scaleFocus.x;
scaleFocusY = scaleFocus.y;
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
//This method will take point on the phone screen, and convert it to be
//a point on the canvas (since the canvas area is larger than the screen).
public Point calculateClickAbsoluteLocation(float x, float y) {
Point result = new Point();
float[] coordinates = {x, y};
//MapPoints will essentially convert the click coordinates from "screen pixels" into
//"canvas pixels", so you can determine what tile was clicked.
canvasMatrix.mapPoints(coordinates);
result.set((int)coordinates[0], (int)coordinates[1]);
return result;
}
的東西包圍「你的問題是,在縮放時你仍然可以操作觸摸事件(所以觸摸的處理兩次)。」只是幫助我很多問題相同的問題。謝謝。 – Kotsu
非常歡迎您! :) – Timur