且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

缩小在OpenGL的机器人

更新时间:2023-11-08 12:01:34

您需要实现OnScaleGestureDetector创造ScaleGestureDetector听捏在和尖灭事件。

You need to implement OnScaleGestureDetector and create ScaleGestureDetector to listen pinch-in and pinch-out events.

例如我用它作为一个内部类GLSurfaceView的:

For example I use it as an inner class of GLSurfaceView:

...

private float sizeCoef = 1;

...

private class ScaleDetectorListener implements ScaleGestureDetector.OnScaleGestureListener{

    float scaleFocusX = 0;
    float scaleFocusY = 0;

    public boolean onScale(ScaleGestureDetector arg0) {
        float scale = arg0.getScaleFactor() * sizeCoef;

        sizeCoef = scale;

        requestRender();

        return true;
    }

    public boolean onScaleBegin(ScaleGestureDetector arg0) {
        invalidate();

        scaleFocusX = arg0.getFocusX();
        scaleFocusY = arg0.getFocusY();

        return true;
    }

    public void onScaleEnd(ScaleGestureDetector arg0) {
        scaleFocusX = 0;
        scaleFocusY = 0;
    }
} 

你也需要增加一些code到onDrawFrame()方法:

Also You have to add some code to onDrawFrame() method:

public void onDrawFrame(GL10 gl) {
    //Clear Screen And Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
    gl.glLoadIdentity();                    
    gl.glEnable(GL10.GL_LIGHTING);
    gl.glTranslatef(0.0f, -1.2f, -z);   //Move down 1.2 Unit And Into The Screen 6.0
    gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);   //X
    gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);   //Y

    gl.glScalef(sizeCoef, sizeCoef, 0);  // if You have a 3d object put sizeCoef as all parameters

    model.draw(gl);                     //Draw the square
    gl.glLoadIdentity();        
    xrot += xspeed;
    yrot += yspeed;

}