添加GLSurfaceView布局 [英] Adding GLSurfaceView in layout
问题描述
我试图运行Android样品 http://developer.android.com /training/graphics/opengl/index.html
I am trying to run android samples http://developer.android.com/training/graphics/opengl/index.html
我要补充一些控件,如文本和按钮的观点,这将看起来像
I have to add some controls like text and buttons to the view which will look like
如何在Android的布局中使用GLSurfaceView?
How to use GLSurfaceView in Android layout?
主要活动(跳过一些自动生成的code)
Main Activity (skipped some auto generated code)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGLSurfaceView = (myGLSurfaceView) findViewById(R.id.glSurfaceViewID); //crashes
}
表面观
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
}
}
布局
<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" />
推荐答案
GLSurfaceView
是从其他视图没有什么不同,当谈到它添加到布局。唯一要考虑的是,它主要是当它的使用,子类,就像它在你的code的完成而不是很常见(也并不十分不寻常的)其他类型的意见。
GLSurfaceView
isn't any different from other views when it comes to adding it to a layout. The only consideration is that it's mostly subclassed when it's used, like it's done in your code, while that's not quite as common (but also not terribly unusual) for other types of views.
在尝试的问题是它增加了布局时使用的类名:
The problem in your attempt is the class name used when adding it to the layout:
<GLSurfaceView
...
毫不奇怪,当布局膨胀,这将创建 GLSurfaceView
类的一个实例。但是,这不是你想要的。你想创建的派生类,这是 myGLSurfaceView
的一个实例。显然企图这样做将是:
Not surprisingly, this would create an instance of the GLSurfaceView
class when the layout is inflated. But that's not what you want. You want to create an instance of your derived class, which is myGLSurfaceView
. An obvious attempt to do this would be:
<myGLSurfaceView
...
这是不会没有工作。类名需要与包名进行限定。说你的 myGLSurfaceView
类是 com.msl.myglexample
包的一部分:
This is not going to work yet. The class name needs to be qualified with the package name. Say your myGLSurfaceView
class is part of the com.msl.myglexample
package:
package com.msl.myglexample;
public class myGLSurfaceView extends GLSurfaceView {
...
}
然后在布局文件功能项将是:
Then the functional entry in the layout file will be:
<com.msl.myglexample.myGLSurfaceView
....
这是不是在OP的code的一个问题,但因为它是一个共同的问题,人们试图做到这一点的时候遇到的问题:这是同样重要的是你的派生 GLSurfaceView
有一个构造函数的的AttributeSet
作为参数。这是需要为获得XML膨胀的所有视图。
This is not a problem in the OP's code, but since it's a common issue people encounter when trying to do this: It's also critical that your derived GLSurfaceView
has a constructor that takes an AttributeSet
as an argument. This is needed for all views that get inflated from XML.
public myGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
这篇关于添加GLSurfaceView布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!