如何在android中将libgdx添加为子视图

How to add libgdx as a sub view in android(如何在android中将libgdx添加为子视图)

本文介绍了如何在android中将libgdx添加为子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 main.xml 如下:

I have main.xml as follows:

  <RelativeLayout>
     ...
     <FrameLayout
                    android:id="@+id/panel_sheet"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

        <com.libgdx.Sheet3DViewGdx 
                android:id="@+id/m3D"
                android:layout_width="1000dp"
                android:layout_height="600dp"
        />

    </FrameLayout>

...
</RelativeLayout>

而我的主要活动类如下:

And my main activity class is as follows:

public class Test extends Activity {

    MainActivity  m3DActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

我的 GDX 类如下,它扩展了 ApplicationListener 类而不是 View.

My GDX class is as follows which extend ApplicationListener class rather than View.

public class Sheet3DViewGdx implements ApplicationListener{

    @Override
    public void create() {
        InputStream in = Gdx.files.internal("data/obj/Human3DModel.obj").read();
        model =  ObjLoader.loadObj(in);
    }

    @Override
    public void dispose() {
    }

    @Override
    public void pause() {
    }


    @Override
    public void render() {
        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        model.render(GL10.GL_TRIANGLES);
    }

    @Override
    public void resize(int arg0, int arg1) {
        float aspectRatio = (float) arg0 / (float) arg1;
    }

    @Override
    public void resume() {
    }
}

现在,我应该如何在主布局中添加 Sheet3DViewGdx 作为子视图?

Now, how should I add Sheet3DViewGdx as a subview in my main layout?

推荐答案

AndroidApplication 类(它扩展了活动)有一个名为 initializeForView(ApplicationListener, AndroidApplicationConfiguration) 的方法,该方法将返回一个 View 你可以添加到你的布局中.

The AndroidApplication class (which extends activity) has a method named initializeForView(ApplicationListener, AndroidApplicationConfiguration) that will return a View you can add to your layout.

因此,您的 Test-class 可以扩展 AndroidApplication 而不是 Activity,以便您可以调用该方法并将 View 添加到您的布局中.

So your Test-class can extend AndroidApplication instead of Activity so that you can call that method and add the View to your layout.

如果由于某种原因这不是一个选项,请查看 AndroidApplication 源代码可以,并模仿它.

If that's not an option, for some reason, take a look at what AndroidApplication source code does, and mimic that.

这篇关于如何在android中将libgdx添加为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在android中将libgdx添加为子视图

基础教程推荐