我一直在尝试从缩放后的画布上获取鼠标坐标。 canvas.scale(scale,scale)给我正确的坐标,但是如果我使用canvas.scale(scale,scale,ivotX,ivotY),则坐标是错误的。
//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;
我需要怎么做才能从canvas.scale获取具有除默认0、0以外的其他枢轴的正确坐标?我也阅读并尝试过有关矩阵的方法,但是没有运气。
答案 0 :(得分:0)
使用矩阵和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]));
...