2012-07-21 28 views
3

我發現下面的代碼旋轉onTouch事件與地圖與pivotote是中心。Rotatin onTouh事件隨着地圖在android中

@Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
     float[] coords = new float[] { 
       event.getX(), event.getY() 
     }; 
     adjustCoords(coords, getRotation()); 
     MotionEvent evt = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), event 
       .getAction(), coords[0], coords[1], event.getPressure(), event.getSize(), event 
       .getMetaState(), event.getXPrecision(), event.getYPrecision(), event.getDeviceId(), 
       event.getEdgeFlags()); 
     return super.dispatchTouchEvent(evt); 
    } 
    protected void adjustCoords(float[] coords, float deg) { 
     float x = coords[0]; 
     float y = coords[1]; 
     int centerX = getWidth()/2; 
     int centerY = getHeight()/2; 
     // convert to radians 
     float rad = (float) ((deg * Math.PI)/180F); 
     float s = (float) Math.sin(rad); 
     float c = (float) Math.cos(rad); 
     // translate point back to origin: 
     x -= centerX; 
     y -= centerY; 
     // apply rotation 
     float tmpX = x * c - y * s; 
     float tmpY = x * s + y * c; 
     x = tmpX; 
     y = tmpY; 
     // translate point back: 
     x += centerX; 
     y += centerY; 
     coords[0] = x; 
     coords[1] = y; 
    } 

但我的要求,而不是中心到底部center.where必須改變在上面的代碼工作。

回答

4

試試這個

@Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
     float[] coords = new float[] { 
       event.getX(), event.getY() 
     }; 
     adjustCoords(coords, -getRotation()); 
     MotionEvent evt = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), event 
       .getAction(), coords[0], coords[1], event.getPressure(), event.getSize(), event 
       .getMetaState(), event.getXPrecision(), event.getYPrecision(), event.getDeviceId(), 
       event.getEdgeFlags()); 
     return super.dispatchTouchEvent(evt); 
    } 
    protected void adjustCoords(float[] coords, float deg) { 
     float x = coords[0]; 
     float y = coords[1]; 
     int centerX = getWidth()/2; 
     int centerY = getHeight(); 
     // convert to radians 
     float rad = (float) ((deg * Math.PI)/180F); 
     float s = (float) Math.sin(rad); 
     float c = (float) Math.cos(rad); 
     // translate point back to origin: 
     x -= centerX; 
     y -= centerY; 
     // apply rotation 
     float tmpX = x * c - y * s; 
     float tmpY = x * s + y * c; 
     x = tmpX; 
     y = tmpY; 
     // translate point back: 
     x += centerX; 
     y += centerY; 
     coords[0] = x; 
     coords[1] = y; 
    } 
+0

我的IDE促使我做正弦和餘弦時使用的,而不是數學FloatMath。 – John 2012-08-09 16:44:41

+0

@ koti:如果將視圖旋轉到180度的非倍數,則無法將ontouch()調度爲正確座標!例如:50,90,270等。, – Mani 2012-10-01 11:29:58