23

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

4

4 回答 4

21

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

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

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

于 2012-06-05T15:17:52.773 回答
14

在 github 上创建了一个 Hello World 程序,用于使用 Android Studio 2.1 在片段中运行的 libgdx。它遵循官方 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());
    }
}

布局.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 脚本。

于 2016-08-09T17:18:54.457 回答
7

使用 libgdx 项目作为 Android 应用程序中的视图现在已清楚地记录在libgdx wiki 中的示例代码,作为片段实现(现代 Android 应用程序的最佳实践):

  1. 将 Android V4 支持库添加到 -android 项目及其构建路径(如果尚未添加)。这是为了以后扩展 FragmentActivity 所必需的
  2. 更改 AndroidLauncher 活动以扩展 FragmentActivity,而不是 AndroidApplication
  3. 在 AndroidLauncher 活动上实现 AndroidFragmentApplication.Callbacks
  4. 创建一个扩展 AndroidFragmentApplication 的类,它是 Libgdx 的 Fragment 实现。
  5. 在 Fragment 的 onCreateView 方法中添加 initializeForView() 代码。
  6. 最后,将 AndroidLauncher 活动内容替换为 Libgdx 片段。
于 2014-12-04T14:54:48.180 回答
6
@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));
}
于 2014-01-29T14:06:46.553 回答