【问题标题】:Rotatin onTouh events along with Map in android在android中与地图一起旋转onTouch事件
【发布时间】:2012-07-21 07:35:52
【问题描述】:

我发现下面的代码可以旋转 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 必须更改上面的代码才能工作。

【问题讨论】:

    标签: android map rotation


    【解决方案1】:

    试试这个

    @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;
        }
    

    【讨论】:

    • 在计算 sin 和 cos 时,我的 IDE 提示我使用 FloatMath 而不是 Math。
    • @koti:如果视图旋转到非 180 度的倍数,则无法调度 ontouch() 来更正坐标!例如:50,90,270 等,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    相关资源
    最近更新 更多