简体   繁体   English

canvas.scale 与枢轴,鼠标坐标

[英]canvas.scale with pivot, mouse coordinates

I've been trying to get the mouse coordinates from scaled canvas.我一直在尝试从缩放的画布中获取鼠标坐标。 The canvas.scale(scale, scale) gives me the correct coordinates but if i use canvas.scale(scale, scale, pivotX, pivotY) the coordinates are wrong. canvas.scale(scale, scale) 给了我正确的坐标但是如果我使用 canvas.scale(scale, scale, pivotX, pivotY) 坐标是错误的。

//onDraw
canvas.scale(scaleFactor, scaleFactor); //default pivot x & y = 0, 0
//canvas.scale(scaleFactor, scaleFactor, pivotX, pivotY);

private float pixelWidth = 480;
private float pixelHeight = 320; 

//onTouchEvent
touchX = (int) event.getX() * pixelWidth / getWidth();
touchX = (int) event.getY() * pixelHeight / getHeight();

worldX = touchX / scaleFactor;
worldY = touchY / scaleFactor;

What do i need to do to get the correct coordinates from canvas.scale with different pivot other than default 0, 0?我需要做什么才能从 canvas.scale 获得正确的坐标,并使用默认值 0, 0 以外的不同枢轴? I've read & tried about the matrix too but no luck.我也读过并尝试过矩阵,但没有运气。

solved it using matrix and this code from https://stackoverflow.com/a/20011005/7334711使用矩阵和来自https://stackoverflow.com/a/20011005/7334711 的代码解决了它

Matrix matrix = new Matrix();

public void render(Canvas canvas) {
    canvas.save();

    matrix.setScale(mScaleFactor, mScaleFactor, pivotX, pivotY);
    canvas.setMatrix(matrix);
...

float[] mv = new float[9];

@Override
public boolean onTouchEvent(MotionEvent event) {

    // Get the values from the matrix into the float array
    matrix.getValues(mv);

    float touchX = (event.getX()*(1/mv[4]) - (mv[2]/mv[4]));
    float touchY = (event.getY()*(1/mv[4]) - (mv[5]/mv[4]));
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM