在活动中使用GlSurfaceview

发布于 2024-12-26 13:03:21 字数 404 浏览 5 评论 0原文

我有一个 Activity,并且我已将 Activity 的内容视图设置为“R.layout.main.xml”。我还有另一个类,其中包含使用 openGL 创建的动画。现在我需要在活动的背景中使用这个动画。

代码是这样的

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_pixie);

    mGLView = new ClearGLSurfaceView(this);
    setContentView(mGLView);
 }

但是我的应用程序崩溃了..我该如何解决这个问题。

I have an Activity and i had set activity's content view as "R.layout.main.xml".And i have an another class which contains animation created using openGL. Now i need to use this animation in the background of the Activity.

The code is like this

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_pixie);

    mGLView = new ClearGLSurfaceView(this);
    setContentView(mGLView);
 }

But my app is Crashing ..How can i solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2025-01-02 13:03:21

当您第二次调用 setContentView() 时,您将替换第一次设置的内容,只留下背景。崩溃很可能是因为您依赖于主布局中的元素,而这些元素已被删除。

您应该在主布局中包含 GLSurfaceView,而不是调用 setContentView() 两次。下面是如何完成此操作的示例:

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent>
    <your.application.package.ClearGLSurfaceView
         android:layout_width="match_parent"
         android:layout_width="match_parent"/>
    <!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie-->
</FrameLayout>

然后您可以像往常一样在 onCreate() 中加载此布局(main_pixie_new 指的是上面的 xml,我只是给它起了这个名称以保持清晰尽可能):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_pixie_new);
 }

When you call the setContentView() a second time, you replace what had been set the first time, leaving you with only the background. The crash is most likely because you depend on the elements in the main layout, which is removed.

Rather than calling setContentView() twice, you should include the GLSurfaceView in the main layout. Below is an example of how this can be done:

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent>
    <your.application.package.ClearGLSurfaceView
         android:layout_width="match_parent"
         android:layout_width="match_parent"/>
    <!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie-->
</FrameLayout>

Then you can load this layout in your onCreate() as usual (main_pixie_new refers to the above xml, I just gave it that name to keep things as clear as possible):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_pixie_new);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文