[英]Android OpenGL hello world
By following the short tutorial http://developer.android.com/training/graphics/opengl/environment.html#glsurfaceview . 通过遵循简短的教程http://developer.android.com/training/graphics/opengl/environment.html#glsurfaceview 。 My example does not work in the emulator.
我的示例在模拟器中不起作用。 I wish to have a basic opengl example working in the emulator but it continues to be a problem and even the instructions by the developers fail to work.
我希望在模拟器中有一个基本的opengl示例,但这仍然是一个问题,甚至开发人员的指令也无法正常工作。
I have three classes: 我有三节课:
package com.test.flushrummy;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
private GLSurfaceView m_GlView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m_GlView = new MyGLSurfaceView(this);
setContentView(m_GlView);
}
}
package com.test.flushrummy;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
public class MyGLRenderer implements Renderer {
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);
}
@Override
public void onSurfaceCreated(GL10 gl,
javax.microedition.khronos.egl.EGLConfig config) {
// TODO Auto-generated method stub
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
}
package com.test.flushrummy;
import android.content.Context;
import android.opengl.GLSurfaceView;
class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView(Context context) {
super(context);
setRenderer(new MyGLRenderer());
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
}
I have in my manifest. 我有我的清单。
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
project.properties: project.properties:
target=android-19
"use gpu host" is snabled in avd. “ use gpu host”在avd中启用。
add the GPU emulation hardware property and set its value to yes in your emulation. 添加GPU仿真硬件属性,然后在仿真中将其值设置为yes。 Add them and try it.
添加它们并尝试。
when you create a new virtual device there is a hardware section . 当您创建新的虚拟设备时,会有一个硬件部分。 add new to it .
添加新的。 there is a option of GPU emulation add this.
有一个GPU仿真的选项添加它。
setEGLContextClientVersion(2);
setRenderer (new MyGLRenderer());
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
It appears the order of method invokes is important. 看来方法调用的顺序很重要。
I had this same problem, and this is what was wrong, so it might be your problem too: 我遇到了同样的问题,这是错误的,因此也可能是您的问题:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//COMMENT OUT THIS COMMAND
//setContentView(R.layout.activity_main);
/*if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
*/
//Setup surface view in this activity
mView = new GLSurfaceView(this);
mView.setEGLContextClientVersion(2);
mView.setRenderer(new GraphicRenderer());
mView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
setContentView(mView);
}
The template code setContentView() needs to be commented out. 模板代码setContentView()需要被注释掉。 then it won't crash on loading.
那么它在加载时不会崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.