Android:旋转地图视图 - >反旋转触摸 - >奇怪的问题

时间:2012-07-28 13:47:04

标签: android rotation android-mapview touches

我在MapView中添加了旋转功能,除了触摸之外它运行良好。

我按照此代码link来旋转视图和计数器旋转触摸。

现在我的问题。

即使当前没有旋转,触摸位置的计算也是错误的。 例如,duraing roation = 0,y坐标被移位。

原因是       。canvas.getMatrix()反转(mMatrix);
即使我不旋转画布,似乎也会出现错误的倒置矩阵。

@Override
protected void dispatchDraw(Canvas canvas)
{
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    //canvas.rotate(rotation, getWidth() * 0.5f, getHeight() * 0.5f);
    canvas.getMatrix().invert(mMatrix);     
    Log.d("matrix", mMatrix.toString());
    super.dispatchDraw(canvas);
    canvas.restore();
}

Log说矩阵是:(在没有旋转的情况下)      矩阵{[1.0,-0.0,0.0] [ - 0.0,1.0,-38.0] [0.0,0.0,1.0]}

问题出在哪里?

1 个答案:

答案 0 :(得分:0)

我要解决的问题如下:

    private Matrix invertedMatrix = new Matrix();
    private float[] tempLocation = new float[2];


    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.rotate(mHeading, getWidth() * 0.5f, getHeight() * 0.5f);
        canvas.getMatrix().invert(invertedMatrix);
        mCanvas.delegate = canvas;
        super.dispatchDraw(mCanvas);
        canvas.restore();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        centeringAllowed = false;

        float[] location = tempLocation;
        location[0] = ev.getX();
        location[1] = ev.getY();
        invertedMatrix.mapPoints(location);
        ev.setLocation(location[0], location[1]);
        return super.dispatchTouchEvent(ev);
    }