如何设置包含 OpenGL View 的 xml 布局?正如我现在所做的那样,将 OpenGL View 设置为使用 setContentView() 的唯一 View 。但我想创建一个包含 OpenGL View 的 xml 布局。假设我想主要使用 OpenGL View ,底部有一个小的 TextView。
这可能吗?或者 OpenGL View 只能是唯一的 View 吗?
最佳答案
这就是我为粒子发射器所做的:扩展 GLSurfaceView 并使其成为我布局的一部分。注意:实现“ParticleRenderer”类来实现你想做的任何 openGL 事情
我的自定义 View :
public class OpenGLView extends GLSurfaceView
{
//programmatic instantiation
public OpenGLView(Context context)
{
this(context, null);
}
//XML inflation/instantiation
public OpenGLView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public OpenGLView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs);
// Tell EGL to use a ES 2.0 Context
setEGLContextClientVersion(2);
// Set the renderer
setRenderer(new ParticleRenderer(context));
}
}
在布局中...
<com.hello.glworld.particlesystem.OpenGLView
android:id="@+id/visualizer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
粒子渲染是直接的......一些示例代码,参见:https://code.google.com/p/opengles-book-samples/source/browse/trunk/Android/Ch13_ParticleSystem/src/com/openglesbook/particlesystem/ParticleSystemRenderer.java
public class ParticleRenderer implements GLSurfaceView.Renderer
{
public ParticleRenderer(Context context)
{
mContext = context;
}
@Override
public void onDrawFrame(GL10 gl)
{
//DO STUFF
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
//DO STUFF
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
//DO STUFF
}
}
关于android - 布局内的 OpenGL View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4126353/