Android GLSurfaceView捏缩放

时间:2012-05-27 23:38:19

标签: android opengl-es zoom glsurfaceview pinchzoom

我正在尝试在GLSurfaceview中实现双指缩放,但无法使其正常工作。 我知道如何放大图片的中心,这很容易(我发布的样本已经捏缩放到图像的中心)。但我不知道如何考虑手指坐标。

项目本身是一个图库应用程序,所以我不需要任何花哨的3D旋转。我需要找出如何根据捏合缩放手势放大图像部分。很可能这可能是3-5行代码的问题,但我没有找到正确的方法来做到这一点。

这意味着场景矩阵不仅在Z轴上平移以进行缩放,而且还通过某个deltaX和deltaY ammount进行平移(移动) - 以保持您在其放大的点。

我的场景设置如下:

(渲染模式设置为RENDERMODE_WHEN_DIRTY)

    // Screen size (in screen pixels.
    private int mViewportWidth;
    private int mViewportHeight;
    // Rectangle for render area.
    private RectF mViewRect = new RectF();

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        mViewportWidth = width;
        mViewportHeight = height;

        float ratio = (float) width / height;
        mViewRect.top = 1.0f;
        mViewRect.bottom = -1.0f;
        mViewRect.left = -ratio;
        mViewRect.right = ratio;

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 20f, (float) width / height, .1f, 100f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

绘图区域应该实现捏合缩放

@Override
public synchronized void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();

    //Pinch-To-Zoom transformation should be implemented here
    //code below doesn't work
    gl.glTranslatef(-delta.x,-delta.y, -currentScale);

    //the drawing is done here
    //...
}

我在同一渲染类的方法中获得了捏合手势和缩放之间的中间点:

private float currentScale = 6f;
/**
 * 
 * @param scaleAbsolute - scale value from ScaleGestureListener
 * @param focusX - X coordinate of pinch-to-zoom gesture in screen coordinates
 * @param focusY - Y coordinate of pinch-to-zoom gesture in screen coordinates
 */
public void zoom(float scaleAbsolute, float focusX, float focusY) {
    //the initial value of currentScale is 6f and by zooming in, we are decresing this value
    //currentScale is used as the Z value (so lower number = bigger zoom)
    currentScale = currentScale / scaleAbsolute;

    //Keep track of deltaX/deltaY here? How?

}

我认为应该添加全局deltaX和deltaY值,但我不知道如何根据当前缩放级别计算X,Y偏移。

这是一个将屏幕坐标转换为场景坐标的辅助函数:

/**
 * Translates screen coordinates into view coordinates.
 */
public void translate(PointF pt) {
    pt.x = mViewRect.left + (mViewRect.width() * pt.x / mViewportWidth);
    pt.y = mViewRect.top - (-mViewRect.height() * pt.y / mViewportHeight);
}

0 个答案:

没有答案