【发布时间】:2016-02-05 03:56:37
【问题描述】:
我正在开发一款游戏,该游戏将俯视一个移动棋子的网格(类似于国际象棋)。我希望用户能够平移和缩放地图。我一直在对矩阵进行转换,然后将这些更改连接到画布矩阵。虽然它大部分都在工作,但我有一个奇怪的错误:
在初始缩放之后,如果我再次捏缩放,屏幕跳跃(就像在其他地方滑动并立即移动到那里一样)然后平滑缩放。我已将此行为范围缩小到 scale 函数的 scaleFocus 变量
canvas.scale(scaleFactor, scaleFactor, scaleFocusX, scaleFocusY);
如果我将 scaleFocusX 和 scaleFocusY 设置为 0,则始终使用原点作为缩放焦点,并且不会发生跳跃。但是,当您滚动远离它时,使用原点是不切实际的。这是我的代码摘要。
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;
}
【问题讨论】:
标签: android android-canvas scale