Building Apps with Graphics 之 Animation Displaying Graphics with OpenGL ES

在application中用openGL ES 来花图的话,必须要创建一个GLSurfaceView 和 GLSurfaceView.Renderer作为容器。
要使用OpenGL es 2.0 API的话,首先要在manifest文件中声明权限.
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
如果使用texture compression的话,也要申请权限.
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />
可以像使用TextView一样来使用GLSurfaceView 
public class OpenGLES20Activity extends Activity {


    private GLSurfaceView mGLView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        mGLView = new MyGLSurfaceView(this);
        setContentView(mGLView);
    }
}
 这个Activity中有new 一个GLSurfaceView 对象,我们看看是如何定义的.
class MyGLSurfaceView extends GLSurfaceView {


    private final MyGLRenderer mRenderer;


    public MyGLSurfaceView(Context context){
        super(context);


        // Create an OpenGL ES 2.0 context
        setEGLContextClientVersion(2);


        mRenderer = new MyGLRenderer();


        // Set the Renderer for drawing on the GLSurfaceView
        setRenderer(mRenderer);
    }
}


我们再来看看MyGLRenderer 的实现.


public class MyGLRenderer implements GLSurfaceView.Renderer {


    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        // Set the background frame color
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }


    public void onDrawFrame(GL10 unused) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    }


    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }
}


onSurfaceCreated() - 调用一次建立view 的OpenGL ES环境
onDrawFrame() - Called for each redraw of the view.
onDrawFrame() -每次重新绘图的时候都调用.
onSurfaceChanged() - 当view的几何图形变化时。例如横屏时.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值