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クラスは、ViewではなくApplicationListenerクラスを拡張する次のとおりです。

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 は Activity の代わりに AndroidApplication を拡張できるため、そのメソッドを呼び出して View をレイアウトに追加できます。

それができない場合は、何らかの理由で、AndroidApplication のソース コードの機能を調べて、それを模倣してください。

于 2012-06-05T15:17:52.773 に答える
14

Android Studio 2.1 を使用してフラグメントで実行される libgdx のgithub で Hello World プログラムを作成しました。公式の 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 アクティビティを変更して、AndroidApplication ではなく FragmentActivity を拡張します
  3. AndroidLauncher アクティビティに AndroidFragmentApplication.Callbacks を実装する
  4. Libgdx の Fragment 実装である AndroidFragmentApplication を拡張する Class を作成します。
  5. Fragment の onCreateView メソッドに initializeForView() コードを追加します。
  6. 最後に、AndroidLauncher アクティビティ コンテンツを Libgdx Fragment に置き換えます。
于 2014-12-04T14:54:48.180 に答える