3

我在下面的代码中找到了旋转 onTouch 事件以及以枢轴为中心的地图。

 @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

1 回答 1

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;
    }
于 2012-07-21T07:56:17.490 回答