2012-07-28 65 views
0

我向我的MapView添加了旋轉,除了觸摸之外它運行良好。Android:旋轉的地圖視圖 - >計數器旋轉觸摸 - > weired問題

我按照此代碼link旋轉視圖和計數器旋轉觸摸。

現在我的問題。

即使當前沒有旋轉,觸摸位置計算也是錯誤的。 例如,duraing roation = 0,y座標向下移動。

原因是, canvas.getMatrix()。invert(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(); 
} 

日誌說,矩陣爲:(不旋轉期間) 矩陣{[1.0,-0.0,0.0] [ - 0.0,1.0,-38.0] [0.0,0.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); 
    }