一.GLSurfaceView介绍

学习Android视频水印,我们使用GLSurfaceView,比SurfaceView绘制效率高,而且内部实现了GLThread,绘制直接用OpenGL来进行,效率高。

GLSurfaceView的使用

可以在Activity中新建一个SurfaceView,然后使用findViewById然后使用;但是本代码直接将GlSurfaceView作为setContentView的参数;

  • 直接在整个Activity上绘制

代码如下:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGLSurfaceView = new GLSurfaceView(this);
        mGLSurfaceView.setEGLContextClientVersion(2);
        mGLSurfaceView.setRenderer(new MyRender(this));
        //setContentView(R.layout.activity_main);
        setContentView(mGLSurfaceView);
    }

 

  • 单独封装一个类MyRender用来控制绘制逻辑
public class MyRender implements GLSurfaceView.Renderer {
    private Context mContext;
    public MyRender(Context context) {
        this.mContext = context;
    }

    public void onSurfaceCreated(GL10 gl10, EGLConfig config) {
        //设定颜色RGBA
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

    }

    public void onSurfaceChanged(GL10 gl10, int width, int height) {
        //设置区域,x, y, width, height
        glViewport(0, 0, width, height);
    }

    public void onDrawFrame(GL10 gl10) {
        //清空屏幕,擦除屏幕上所有的颜色,用glClearColor定义的颜色填充
        glClear(GL_COLOR_BUFFER_BIT);
    }
}

 

  • 注意GLSurfaceView的生成周期

    @Override
    protected void onPause() {
        super.onPause();
        mGLSurfaceView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLSurfaceView.onResume();
    }

 

  • 最终显示效果为:

 

以上就为OpenGLES绘制做好了基础工作;

代码地址为:https://download.csdn.net/download/gaojun1146/10696504

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值