zoom out in OpenGL in android

前端 未结 3 1661
执笔经年
执笔经年 2021-02-09 21:47

I m using 3D object and rendering it and showing it by extends GLSurfaceView implementing Renderer, the problem is that how to do zoom out-in with pinch-in and pinch-out.

3条回答
  •  生来不讨喜
    2021-02-09 22:35

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

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

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

提交回复
热议问题