【问题标题】:Android. OpenGl ES. panning/scrolling安卓。 OpenGL ES。平移/滚动
【发布时间】:2016-12-21 16:36:53
【问题描述】:

我正在编写一个显示地图的应用程序。用户可以缩放和平移。地图根据磁力计的值旋转(地图以与设备旋转相反的方向旋转)。

对于缩放,我使用 ScaleGestureDetector 并将比例因子传递给 Matrix.scaleM。

我正在使用以下代码进行平移:

GlSurfaceView 侧:

private void handlePanAndZoom(MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);
    // Get the index of the pointer associated with the action.
    int index = MotionEventCompat.getActionIndex(event);
    int xPos = (int) MotionEventCompat.getX(event, index);
    int yPos = (int) MotionEventCompat.getY(event, index);

    mScaleDetector.onTouchEvent(event);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mRenderer.handleStartPan(xPos, yPos);
            break;
        case MotionEvent.ACTION_MOVE:
            if (!mScaleDetector.isInProgress()) {
                mRenderer.handlePan(xPos, yPos);
            }
            break;
    }
}

渲染端:

private static final PointF mPanStart = new PointF();
public void handleStartPan(final int x, final int y) {
    runOnGlThread(new Runnable() {
        @Override
        public void run() {
            windowToWorld(x, y, mPanStart);
        }
    });
}

private static final PointF mCurrentPan = new PointF();
public void handlePan(final int x, final int y) {
    runOnGlThread(new Runnable() {
        @Override
        public void run() {
            windowToWorld(x, y, mCurrentPan);
            float dx = mCurrentPan.x - mPanStart.x;
            float dy = mCurrentPan.y - mPanStart.y;
            mOffsetX += dx;
            mOffsetY += dy;
            updateModelMatrix();
            mPanStart.set(mCurrentPan);
        }
    });
}

windowToWorld 函数使用 gluUnProject 并且可以工作,因为我将它用于许多其他任务。更新模型矩阵:

private void updateModelMatrix() {
    Matrix.setIdentityM(mScaleMatrix,0);
    Matrix.scaleM(mScaleMatrix, 0, mScale, mScale, mScale);

    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, 1.0f);

    Matrix.setIdentityM(mTranslationMatrix,0);
    Matrix.translateM(mTranslationMatrix, 0, mOffsetX, mOffsetY, 0);

    // Model = Scale * Rotate * Translate
    Matrix.multiplyMM(mIntermediateMatrix, 0, mScaleMatrix, 0, mRotationMatrix, 0);
    Matrix.multiplyMM(mModelMatrix, 0, mIntermediateMatrix, 0, mTranslationMatrix, 0);
}

windowToWorld函数的gluUnproject中使用相同的mModelMatrix进行点平移。

所以我的问题有两个:

  1. 平移速度比手指在设备屏幕上的移动慢两倍
  2. 在某个时刻,当连续平移几秒钟(例如,在屏幕上画圈)时,地图开始“摇晃”。这种震动的幅度越来越大。看起来在 handlePan 迭代中增加了一些值并导致了这种效果。

知道为什么会这样吗?

提前谢谢你,格雷格。

【问题讨论】:

    标签: android scroll opengl-es panning


    【解决方案1】:

    好吧,我的代码的问题是这一行:

            mPanStart.set(mCurrentPan);
    

    只是因为我在世界坐标中拖动,并更新了偏移量,但当前位置保持不变。这是我的错误。

    删除此行将解决所有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2010-09-18
      • 2016-11-19
      相关资源
      最近更新 更多