通过在自定义视图的onDraw中设置标识矩阵来偏移画布

通过在自定义视图的onDraw中设置标识矩阵来偏移画布,第1张

通过在自定义视图的onDraw中设置标识矩阵来偏移画布

我已经收窄这种行为降低到原来的

Matrix
一个的
View
的帆布已经被视图的位置转换。但是,如果您
Matrix
使用
Canvas.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];@Overridepublic boolean onTouchEvent(MotionEvent event) {    if () {        mMyMatrix.postTranslate(deltaX, deltaY);    }    if () {        mMyMatrix.postScale(deltaScale, deltaScale);    }    if () {        // 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;}@Overrideprotected void onDraw(Canvas canvas) {    // Each frame, transform your canvas with the matrix.    canvas.save();    canvas.concat(mMyMatrix);    // Draw grid.    grid.draw(canvas);    canvas.restore();}

或者不推荐使用的方法来获取矩阵,该方法仍然有效,并且可能需要较少的更改:

@Overrideprotected 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();}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5439800.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-11
下一篇2022-12-11

发表评论

登录后才能评论

评论列表(0条)

    保存