我已经将这种行为缩小到Matrix
aView
的画布的原件已经被视图的位置翻译了。Matrix
但是,如果您使用 usingCanvas.getMatrix()
或,这并不明显View.getMatrix()
。您将从这些调用中获得单位矩阵。
View
您看到的画布偏移量很可能与' 从屏幕顶部的偏移量(状态栏、标题栏等)的高度完全相同。
在这个用例和大多数用例中使用canvas.concat(matrix)
而不是使用是正确的。canvas.setMatrix(matrix)
如果你真的需要原始矩阵,我在调试时做了,你必须通过View
自己的翻译手动转换它Window
:
int[] viewLocation = new int[2];
mView.getLocationInWindow(viewLocation);
mOriginalMatrix.setTranslate(viewLocation[0], viewLocation[1]);
编辑以回答评论中的附加问题:
要将触摸坐标(或任何屏幕坐标)转换为与 a 的坐标相匹配Canvas
,只需将所有转换改为 a Matrix
,并Canvas.concat()
在绘制之前的每一帧都使用该矩阵。Canvas
(或者您可以像现在一样继续直接进行所有转换,并Canvas.getMatrix(mMyMatrix)
在每次绘制后用于检索矩阵。它已被弃用但它有效。)
然后可以使用该矩阵将您的原始网格边界转换为在屏幕上绘制的那些。本质上,您所做的事情与绘制网格时所做的完全相同Canvas
,将网格的角点转换为屏幕坐标。网格现在将与您的触摸事件在同一坐标系中:
private final Matrix mMyMatrix = new Matrix();
// Assumes that the grid covers the whole View.
private final float[] mOriginalGridCorners = new float[] {
0, 0, // top left (x, y)
getWidth(), getHeight() // bottom right (x, y)
};
private final float[] mTransformedGridCorners = new float[4];
@Override
public boolean onTouchEvent(MotionEvent event) {
if (/* User pans the screen */) {
mMyMatrix.postTranslate(deltaX, deltaY);
}
if (/* User zooms the screen */) {
mMyMatrix.postScale(deltaScale, deltaScale);
}
if (/* User taps the grid */) {
// Transform the original grid corners to where they
// are on the screen (panned and zoomed).
mMyMatrix.mapPoints(mTransformedGridCorners, mOriginalGridCorners);
float gridWidth = mTransformedGridCorners[2] - mTransformedGridCorners[0];
float gridHeight = mTransformedGridCorners[3] - mTransformedGridCorners[1];
// Get the x and y coordinate of the tap inside the
// grid, between 0 and 1.
float x = (event.getX() - mTransformedGridCorners[0]) / gridWidth;
float y = (event.getY() - mTransformedGridCorners[1]) / gridHeight;
// To get the tapped grid cell.
int column = (int)(x * nbrColumns);
int row = (int)(y * nbrRows);
// Or to get the tapped exact pixel in the original grid.
int pixelX = (int)(x * getWidth());
int pixelY = (int)(y * getHeight());
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// Each frame, transform your canvas with the matrix.
canvas.save();
canvas.concat(mMyMatrix);
// Draw grid.
grid.draw(canvas);
canvas.restore();
}
或者不推荐使用的获取矩阵的方法,它仍然有效,并且可能需要较少的更改:
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
// Transform canvas and draw the grid.
grid.draw(canvas);
// Get the matrix from canvas. Can be used to transform
// corners on the next touch event.
canvas.getMatrix(mMyMatrix);
canvas.restore();
}