我正在研究一个将向下移动的网格移动棋子(类似于国际象棋)的游戏。我希望用户能够平移和缩放地图。我一直在对矩阵进行转换,然后将这些更改连接到画布矩阵。虽然这主要是工作,我有一个奇怪的错误:导致跳跃位置的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