我在自定义 View 的 onDraw
方法中为我的 Canvas 设置了一个矩阵
canvas.setMatrix(matrix);
然后我只使用预定义的颜料绘制一个网格:
canvas.drawRect(0,0,viewWidth,viewHeight, background);
for(int i=0; i <= nRows; i++){
canvas.drawLine(0,i*cellHeight, nCols*cellWidth,i*cellHeight,lines);
canvas.drawLine(i*cellWidth, 0, i*cellWidth, nRows*cellHeight, lines);
if(i != nRows)
canvas.drawText(""+i, i*cellWidth, (i+1)*cellHeight, text);
}
并且出于某种原因,整个 Canvas 偏移了单元格高度的大约 1.5 倍。知道为什么会发生这种情况或如何解决它吗?矩阵未初始化,根据文档,顺便说一句,因此应该是恒等式。
非常感谢!
最佳答案
我已将这种行为缩小到 View
的 Canvas 的原始 Matrix
已经被 View 的位置平移了。但是,如果您使用 Canvas.getMatrix()
或 View.getMatrix()
获取 Matrix
,这并不明显。您将从这些调用中获得单位矩阵。
您看到的 Canvas 偏移很可能与 View
从屏幕顶部(状态栏、标题栏等)的偏移高度完全相同。
在这个用例和大多数用例中,您使用 canvas.concat(matrix)
而不是 canvas.setMatrix(matrix)
是正确的。如果你真的需要原始矩阵,我在调试时做了,你必须通过 View
在它自己的 Window
中的翻译手动转换它:
int[] viewLocation = new int[2];
mView.getLocationInWindow(viewLocation);
mOriginalMatrix.setTranslate(viewLocation[0], viewLocation[1]);
编辑以回答评论中的附加问题:
要转换触摸坐标(或任何屏幕坐标)以匹配 Canvas
的坐标,只需将所有转换改为 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();
}
关于java - 通过在自定义 View 的 onDraw 中设置单位矩阵来抵消 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17100355/