我正在尝试运行 android 示例http://developer.android.com/training/graphics/opengl/index.html
我必须在视图中添加一些控件,如文本和按钮,它们看起来像 
如何在 Android 布局中使用 GLSurfaceView?
主要活动(跳过一些自动生成的代码)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGLSurfaceView = (myGLSurfaceView) findViewById(R.id.glSurfaceViewID); //crashes
}
Run Code Online (Sandbox Code Playgroud)
表面视图
public class myGLSurfaceView extends GLSurfaceView {
private myRenderer mRenderer;
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
public myGLSurfaceView (Context context) {
super(context);
// Create an OpenGL ES 2.0 context.
setEGLContextClientVersion(2);
// Creating a Renderer
setRenderer(new myRenderer(context));
// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
public myGLSurfaceView (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public void setRenderer(myRenderer mRenderer, float density) {
// TODO Auto-generated method stub
}
}
Run Code Online (Sandbox Code Playgroud)
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id= "@+id/linearlayout1" >
<Button
android:id="@+id/buttonID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="A Button" />
<GLSurfaceView
android:id="@+id/glSurfaceViewID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.23" />
Run Code Online (Sandbox Code Playgroud)
GLSurfaceView在将其添加到布局时与其他视图没有任何不同。唯一的考虑是它在使用时主要是子类化的,就像在您的代码中完成的那样,而对于其他类型的视图来说这并不常见(但也不是非常不寻常)。
您尝试中的问题是将其添加到布局时使用的类名:
<GLSurfaceView
...
Run Code Online (Sandbox Code Playgroud)
毫不奇怪,这将GLSurfaceView在布局膨胀时创建类的实例。但这不是你想要的。您想创建派生类的一个实例,即myGLSurfaceView. 一个明显的尝试是:
<myGLSurfaceView
...
Run Code Online (Sandbox Code Playgroud)
这还行不通。类名需要用包名来限定。假设你的myGLSurfaceView类是com.msl.myglexample包的一部分:
package com.msl.myglexample;
public class myGLSurfaceView extends GLSurfaceView {
...
}
Run Code Online (Sandbox Code Playgroud)
然后布局文件中的功能条目将是:
<com.msl.myglexample.myGLSurfaceView
....
Run Code Online (Sandbox Code Playgroud)
这在 OP 的代码中不是问题,但由于这是人们在尝试执行此操作时遇到的常见问题:派生GLSurfaceView具有将 anAttributeSet作为参数的构造函数也很重要。这对于从 XML 膨胀的所有视图都是必需的。
public myGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4518 次 |
| 最近记录: |