原文链接:https://developer.android.com/training/graphics/opengl/environment.html
示例代码
In order to draw graphics with OpenGL ES in your Android application, you must create a view container for them. One of the more straight-forward ways to do this is to implement both a GLSurfaceView and a GLSurfaceView.Renderer. A GLSurfaceView is a view container for graphics drawn with OpenGL and GLSurfaceView.Renderer controls what is drawn within that view. For more information about these classes, see the OpenGL ES developer guide.
为了在Android应用中使用OpenGL ES进行绘图,你必须建立一个view容器。最直接的一个方法就是直接实现GLSurfaceView和GLSurfaceView.Renderer这两个类。GLSurfaceView是一个OpenGL画图所使用的view容器,而GLSurfaceView.Renderer是控制在这个view里面画什么。你可以查看OpenGL ES 开发指导获取更多关于这些类的信息。
GLSurfaceView is just one way to incorporate OpenGL ES graphics into your application. For a full-screen or near-full screen graphics view, it is a reasonable choice. Developers who want to incorporate OpenGL ES graphics in a small portion of their layouts should take a look at TextureView. For real, do-it-yourself developers, it is also possible to build up an OpenGL ES view using SurfaceView, but this requires writing quite a bit of additional code.
在你的应用中使用OpenGL ES画图,GLSurfaceView只是其中的一种方式。绘制一个全屏或近乎全屏的图像,采用GLSurfaceView是合理的选择。如果开发者想使用OpenGL ES在一小部分布局中绘制图像,那么应该考虑TextureView。其实,你可以在SurfaceView的基础上手动实现一个OpenGL ES view, 但是这需要写许多额外的代码。
This lesson explains how to complete a minimal implementation of GLSurfaceView and GLSurfaceView.Renderer in a simple application activity.
这一课主要是讲在一个简单的activity里面,以小量的代码实现GLSurfaceView and GLSurfaceView这两个类。
Declare OpenGL ES Use in the Manifest
在AndroidManifest.xml中声明OpenGL ES的使用
In order for your application to use the OpenGL ES 2.0 API, you must add the following declaration to your manifest:
为了在你的应用中使用OpenGL ES 2.0 API,你必须把这个声明加到你的AndroidManifest.xml.
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
If your application uses texture compression, you must also declare which compression formats your app supports, so that it is only installed on compatible devices.
为你的应用中使用了texture压缩,你必须声明你的应用支持的压缩格式,以便于你的应用只在支持这些格式的手机上才能安装。
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />
For more information about texture compression formats, see the OpenGL developer guide.
查看OpenGL开发文档,获取更多与texture压缩格式相关的信息。
Create an Activity for OpenGL ES Graphics
建立一个Activity
Android applications that use OpenGL ES have activities just like any other application that has a user interface. The main difference from other applications is what you put in the layout for your activity. While in many applications you might use TextView, Button and ListView, in an app that uses OpenGL ES, you can also add a GLSurfaceView.
一个使用OpenGL ES的应用和其他应用一样有activities和用户界面。与其他应用的主要不同是你在布局里的使用的控件。在其他的(非OpenGL ES)应用的布局里,你可能会使用TextView, Button and ListView。在一个使用OpenGL ES的应用的布局中,你还可以使用GLSurfaceView。
The following code example shows a minimal implementation of an activity that uses a GLSurfaceView as its primary view:
接下的示例代码展示了用少量必要的代码实现在一个activity里面使用GLSurfaceView作为主要的view。
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);
}
}
-
Note: OpenGL ES 2.0 requires Android 2.2 (API Level 8) or higher, so make sure your Android project targets that API or higher.
-
注意:OpenGL ES 2.0 需要Android 2.2(API Level 8)及以上,所以确保你的应用minSdkVersion在8 及以上。
Build a GLSurfaceView Object
创建一个GLSurfaceView对象
A GLSurfaceView is a specialized view where you can draw OpenGL ES graphics. It does not do much by itself. The actual drawing of objects is controlled in the GLSurfaceView.Renderer that you set on this view. In fact, the code for this object is so thin, you may be tempted to skip extending it and just create an unmodified GLSurfaceView instance, but don’t do that. You need to extend this class in order to capture touch events, which is covered in the Responding to Touch Events lesson.
GLSurfaceView是一个专门用来绘制OpenGL ES图画的view. 这个类本身并不做太多绘制相关的工作。你为GLSurfaceView设置的GLSurfaceView.Renderer这个类才是实际上负责绘制物体的类。实际上,与GLSurfaceView相关的代码很少,以至于你不想继承这个类,只想创建一个没有任何修改的GLSurfaceView实例,但是不要这么做!如果你想截获点击事件(在点击事件响应的课程里讲过),你需要继承(extend) GLSurfaceView这个类。
The essential code for a GLSurfaceView is minimal, so for a quick implementation, it is common to just create an inner class in the activity that uses it:
实现继承GLSurfaceView的代码量很少,快速的实现方式是在使用GLSurfaceView的activity里面建立一个继承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);
}
}
One other optional addition to your GLSurfaceView implementation is to set the render mode to only draw the view when there is a change to your drawing data using the GLSurfaceView.RENDERMODE_WHEN_DIRTY setting:
另外,你可以设render模式为GLSurfaceView.RENDERMODE_WHEN_DIRTY ,只有绘制数据有改变时才绘制这个view.
// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
This setting prevents the GLSurfaceView frame from being redrawn until you call requestRender(), which is more efficient for this sample app.
这种设置可以避免GLSurfaceView自动重新绘制帧(frame), 除非你调用方法requestRender(), 对于这个示例应用来说,这样更加高效。
Build a Renderer Class
创建一个Renderer类
The implementation of the GLSurfaceView.Renderer class, or renderer, within an application that uses OpenGL ES is where things start to get interesting. This class controls what gets drawn on the GLSurfaceView with which it is associated. There are three methods in a renderer that are called by the Android system in order to figure out what and how to draw on a GLSurfaceView:
让一个使用OpenGL ES的应用变得有趣地方在于GLSurfaceView.Renderer类的实现。GLSurfaceView.Renderer这个类控制在与它相关的GLSurfaceView上绘制什么内容。为了弄清楚需要绘制什么以及怎么样绘制,android系统会调用GLSurfaceView.Renderer类的三个方法。
-
onSurfaceCreated() - Called once to set up the view's OpenGL ES environment.
onSurfaceCreated() - 在设置view的OpenGL ES 环境时会调用一次。 -
onDrawFrame() - Called for each redraw of the view.
onDrawFrame() - 每次重新绘制时会调用到。 -
onSurfaceChanged() - Called if the geometry of the view changes, for example when the device's screen orientation changes.
onSurfaceChanged() - 在view的形状改变时会被调用,比如:手机屏幕旋转后。
Here is a very basic implementation of an OpenGL ES renderer, that does nothing more than draw a black background in the GLSurfaceView:
这里有一个基本的 OpenGL ES renderer实现,作用是只在GLSurfaceView里画一个黑色的背景。
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);
}
}
That’s all there is to it! The code examples above create a simple Android application that displays a black screen using OpenGL. While this code does not do anything very interesting, by creating these classes, you have laid the foundation you need to start drawing graphic elements with OpenGL.
结束!上面这些示例代码创建了一个简单的应用,在这个应用里面,采用OpenGL显示了一个画色的背景。虽然,这些代码没实现什么有意思的事情,但是,通过实现这些类,你打下了进一步使用OpenGL绘制图画的基础。
- Note: You may wonder why these methods have a GL10 parameter, when you are using the OpengGL ES 2.0 APIs. These method signatures are simply reused for the 2.0 APIs to keep the Android framework code simpler.
注意:你可能会奇怪为什么在使用OpengGL ES 2.0 API时会有一些GL10的参数。为了使Android 框架的代码更简洁,在OpengGL ES 2.0中重用了1.0中的方法签名。
If you are familiar with the OpenGL ES APIs, you should now be able to set up a OpenGL ES environment in your app and start drawing graphics. However, if you need a bit more help getting started with OpenGL, head on to the next lessons for a few more hints.
如果你熟悉 OpenGL ES API,现在你就可以在你的应用中设置一个 OpenGL ES环境,开始绘图了。如果你需要更多有关OpenGL入门的帮助,可以看下一课。
网友评论