【问题标题】:How to add libgdx as a sub view in android如何在android中将libgdx添加为子视图
【发布时间】:2012-06-09 07:27:53
【问题描述】:

我的 main.xml 如下:

  <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>

而我的主要活动类如下:

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。

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 作为子视图?

【问题讨论】:

    标签: android libgdx


    【解决方案1】:

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

    因此,您的测试类可以扩展 AndroidApplication 而不是 Activity,以便您可以调用该方法并将视图添加到您的布局中。

    如果由于某种原因这不是一个选项,请查看 AndroidApplication source code 的作用,并模仿它。

    【讨论】:

    • 谢谢@Matsemann。但它仍然没有解释如何实现它。能简单介绍一下吗?
    【解决方案2】:

    我使用 Android Studio 2.1 为在片段中运行的 libgdx 创建了一个 Hello World program on github。它遵循the instructions on the official libgdx wiki

    AndroidLauncher 类:

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import com.badlogic.gdx.backends.android.AndroidFragmentApplication;
    
    public class AndroidLauncher extends FragmentActivity implements  AndroidFragmentApplication.Callbacks {
        @Override
        public void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.layout);
    
            // Create libgdx fragment
            GameFragment libgdxFragment = new GameFragment();
    
            // Put it inside the framelayout (which is defined in the layout.xml file).
            getSupportFragmentManager().beginTransaction().
                    add(R.id.content_framelayout, libgdxFragment).
                    commit();
        }
    
        @Override
        public void exit() {
    
        }
    
    
    }
    

    GameFragment 类:

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import com.badlogic.gdx.backends.android.AndroidFragmentApplication;
    
    public class GameFragment extends AndroidFragmentApplication{
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // return the GLSurfaceView on which libgdx is drawing game stuff
            return initializeForView(new MyGdxGame());
        }
    }
    

    layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
        android:id="@+id/content_framelayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2">
        </FrameLayout>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#FF0000"
            android:textColor="#00FF00"
            android:textSize="40dp"
            android:text="I'm just a TextView here with red background :("/>
    
    </LinearLayout>
    

    MyGdxGame 类:

    import com.badlogic.gdx.ApplicationAdapter;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.Color;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.BitmapFont;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    
    public class MyGdxGame extends ApplicationAdapter {
        SpriteBatch batch;
        Texture img;
        private BitmapFont font;
    
    
        @Override
        public void create () {
            batch = new SpriteBatch();
            img = new Texture("badlogic.jpg");
            font = new BitmapFont();
            font.setColor(Color.BLUE);
        }
    
        @Override
        public void render () {
            Gdx.gl.glClearColor(0, 0, 0, 0);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
            batch.begin();
    
            //batch.draw(img, 0, 0);
            font.getData().setScale(6.0f);
            font.draw(batch, "Hello World from libgdx running in a fragment! :)", 100, 300);
    
            batch.end();
        }
    
        @Override
        public void dispose () {
            batch.dispose();
            img.dispose();
        }
    }
    

    确保您已添加以下内容:

    compile "com.android.support:support-v4:24.1.1"
    

    到项目 (":android") 部分内的 "dependencies {.}" 部分中的项目 gradle 脚本。

    【讨论】:

      【解决方案3】:

      现在在 libgdx wiki, implemented as a Fragment(现代 Android 应用的最佳实践)中清楚地记录了使用 libgdx 项目作为 Android 应用中的视图,并附有示例代码:

      1. 将 Android V4 支持库添加到 -android 项目及其构建路径(如果尚未添加)。这是以后扩展 FragmentActivity 所必需的
      2. 更改 AndroidLauncher 活动以扩展 FragmentActivity,而不是 AndroidApplication
      3. 在 AndroidLauncher 活动上实现 AndroidFragmentApplication.Callbacks
      4. 创建一个扩展 AndroidFragmentApplication 的类,AndroidFragmentApplication 是 Libgdx 的 Fragment 实现。
      5. 在 Fragment 的 onCreateView 方法中添加 initializeForView() 代码。
      6. 最后,将 AndroidLauncher 活动内容替换为 Libgdx 片段。

      【讨论】:

      【解决方案4】:
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
          cfg.useGL20 = false;
          //initialize(new LoveHearts(), cfg);
          LinearLayout lg=(LinearLayout)findViewById(R.id.game);
          lg.addView(initializeForView(new LoveHearts(), cfg));
      }
      

      【讨论】:

      • 简单快捷的方法。
      猜你喜欢
      • 1970-01-01
      • 2011-10-02
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      相关资源
      最近更新 更多