java – 尝试以编程方式将GLSurfaceView添加到布局中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 尝试以编程方式将GLSurfaceView添加到布局中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6470字,纯文字阅读大概需要10分钟。
内容图文
![java – 尝试以编程方式将GLSurfaceView添加到布局中](/upload/InfoBanner/zyjiaocheng/720/463f028aa12d49359a9d3380337c6cbb.jpg)
好吧,只是一些背景……我有一个功能齐全的游戏,使用画布绘制,我正在尝试学习并将绘图转换为OpenGL ES 1.0. (为了保持兼容性.)
我一直试图做的是,找出一种方法将GLSurfaceView和Renderer合并到程序中,这样我就可以逐个切换绘图. (有很多精灵,我需要经常更新,所以我不想让它花一个月时间来发布下一次更新.)
有人建议我在当前的SurfaceView上使用GLSurfaceView,但是我到了那里但是我遇到了麻烦.
我基本上使用的是LunarLander示例,并尝试添加一个简单的OpenGL渲染器,在一个正方形上绘制纹理并渲染它.目标是在画布上绘制正方形.
以下是活动中的相关代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lunar_layout);
LinearLayout detailListView = (LinearLayout) findViewById(R.id.dets); // Find the layout where you want to add button
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
detailListView.addView(button);//add view to add
mGLSV = new BasicGLSurfaceView(this);
mGLSV.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
detailListView.addView(mGLSV);
//clipped
lunar_layout文件如下所示:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.android.lunarlander.LunarView
android:id="@+id/lunar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/dets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="a14ecf8f2d9ef49"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
</LinearLayout>
<RelativeLayout
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="@string/lunar_layout_text_text"
android:textColor="#88ffffff"
android:textSize="24sp"
android:visibility="visible" />
</RelativeLayout>
</FrameLayout>
非常简单,我正在尝试使用LinearLayout在Surface View上添加内容.注意我还添加了一个临时按钮,只是为了检查概念的工作原理.
我最终得到的是画布,按钮显示正确,但没有GLSurfaceView的迹象.我究竟做错了什么?
解决方法:
我建议您已将BasicGLSurfaceView添加到XML中的framelayout中,并使用findViewById(R.id.basicGLSurfaceView1)来获取它,而不是尝试以编程方式创建它:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.android.lunarlander.BasicGLSurfaceView
android:id="@+id/glSurfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.example.android.lunarlander.BasicGLSurfaceView>
<com.example.android.lunarlander.LunarView
android:id="@+id/lunar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/dets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="a14ecf8f2d9ef49"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
</LinearLayout>
<RelativeLayout
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="@string/lunar_layout_text_text"
android:textColor="#88ffffff"
android:textSize="24sp"
android:visibility="visible" />
</RelativeLayout>
</FrameLayout>
您需要告诉GLSurfaceView开始渲染. You have to call (or even override) GLSurfaceView中的GLSurfaceView.start().或者,setRenderer是公共的,所以如果你调用mGLSV.setRenderer(myRenderer);在您的Activity中,myRenderer是MyRenderer的一个实例,它实现了GLSurfaceView.Renderer,然后它应该自动启动渲染,而不必触摸start()
编辑:实际上,在布局视图时调用start()(即当你setContentView()或将它添加到你设置为内容视图的任何布局时),而不是在调用setRenderer时
它听起来是前置的,但实现Renderer是为了你想要以标准的android / opengl-es方式呈现某些东西,同时扩展GLSurfaceView就是你真的想深入了解并编辑android呈现的方式.事实上,我建议不要在GLSurfaceView.Renderer中尽可能地扩展GLSurfaceView.我编写了我的整个3D游戏而没有扩展GLSurfaceView.我的代码看起来像这样:
public class stackoverflowTest extends Activity {
MyGLSurfaceView glSurface;
MyRenderer myRenderer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myRenderer = new MyRenderer();
//Get hold of our GLSurfaceView
glSurface = (MyGLSurfaceView) findViewById(R.id.graphics_glsurfaceview1);
// optional
glSurface.setEGLConfigChooser(true);
glSurface.setRenderer(myRenderer);
//Set the renderer. setRenderer will call start() and start the GL thread, which then calls onSurfaceCreated, onDraw etc in the Renderer
}
@Override
public void onPause(){
super.onPause();
glSurface.onPause();
}
@Override
public void onResume() {
super.onResume();
glSurface.onResume();
}
}
}
渲染:
public class MyRenderer implements Renderer {
@Override
public void onDrawFrame(GL10 gl) {
// openGL drawing code goes here
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// etc
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// etc
}
}
GLSurfaceView:
public class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView(Context context) {
super(context);
}
public MyGLSurfaceView(Context context, AttributeSet attribs) {
super(context, attribs);
}
@Override
public void onPause(){
super.onPause();
}
@Override
public void onResume(){
super.onResume();
}
}
如果你可以升级到android 2.1(opengl-es 1.1)你可以使用点精灵而不是渲染纹理四边形或其他
内容总结
以上是互联网集市为您收集整理的java – 尝试以编程方式将GLSurfaceView添加到布局中全部内容,希望文章能够帮你解决java – 尝试以编程方式将GLSurfaceView添加到布局中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。