Android多种方式实现相机圆形预览 看这一篇就够了,吐血整理

  • Typically you will set your viewport here. If your camera

  • is fixed then you could also set your projection matrix here:

  • void onSurfaceChanged(GL10 gl, int width, int height) {

  • gl.glViewport(0, 0, width, height);
    
  • // for a fixed camera, set the projection too
    
  • float ratio = (float) width / height;
    
  • gl.glMatrixMode(GL10.GL_PROJECTION);
    
  • gl.glLoadIdentity();
    
  • gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    
  • }

  • @param gl the GL interface. Use instanceof to

  • test if the interface supports GL11 or higher interfaces.

  • @param width

  • @param height

*/

void onSurfaceChanged(GL10 gl, int width, int height);

/**

  • Called to draw the current frame.

  • This method is responsible for drawing the current frame.

  • The implementation of this method typically looks like this:

  • void onDrawFrame(GL10 gl) {

  • gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    
  • //... other gl calls to render the scene ...
    
  • }

  • @param gl the GL interface. Use instanceof to

  • test if the interface supports GL11 or higher interfaces.

*/

void onDrawFrame(GL10 gl);

}

  • void onSurfaceCreated(GL10 gl, EGLConfig config)

在Surface创建或重建的情况下回调

  • void onSurfaceChanged(GL10 gl, int width, int height)

在Surface的大小发生变化的情况下回调

  • void onDrawFrame(GL10 gl)

在这里实现绘制操作。当我们设置的renderModeRENDERMODE_CONTINUOUSLY时,该函数将不断地执行;

当我们设置的renderModeRENDERMODE_WHEN_DIRTY时,将只在创建完成和调用requestRender后才执行。一般我们选择RENDERMODE_WHEN_DIRTY渲染模式,避免过度绘制。

一般情况下,我们会自己实现一个Renderer,然后为GLSurfaceView设置Renderer,可以说,Renderer的编写是整个流程的核心步骤。以下是在void onSurfaceCreated(GL10 gl, EGLConfig config)进行的初始化操作和在void onDrawFrame(GL10 gl)进行的绘制操作的流程图:

2. 具体实现

  • 坐标系介绍

如图所示,和Android的View坐标系不同,OpenGL的坐标系是笛卡尔坐标系。

Android View的坐标系以左上角为原点,向右x递增,向下y递增

而OpenGL坐标系以中心为原点,向右x递增,向上y递增

  • 着色器编写

/**

  • 顶点着色器

*/

private static String VERTEX_SHADER =

" attribute vec4 attr_position;\n" +

" attribute vec2 attr_tc;\n" +

" varying vec2 tc;\n" +

" void main() {\n" +

" gl_Position = attr_position;\n" +

" tc = attr_tc;\n" +

" }";

/**

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值