2012-06-05 16 views
16

子視圖我的main.xml如下:如何libgdx添加爲Android的

<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作爲子視圖?

回答

19

AndroidApplication類(它擴展了活動)具有一個名爲initializeForView(ApplicationListener, AndroidApplicationConfiguration)的方法,該方法將返回可添加到佈局的View

因此,您的Test-class可以擴展AndroidApplication而不是Activity,以便您可以調用該方法並將View添加到佈局。

如果這不是一個選項,出於某種原因,看看AndroidApplication source code做什麼,並模仿它。

+0

Thanks @Matsemann。但它仍然沒有解釋如何實現它。你能簡單地告訴一下嗎? –

+0

它是如此工作? –

+0

謝謝,它工作。 –

3
@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)); 
} 
6

使用libgdx項目作爲一個Android應用程序內部的觀點已記錄清楚,與示例代碼,在libgdx wiki, implemented as a Fragment(最佳實踐爲現代的Android應用程序):

  1. 添加的Android V4 Support Library添加到-android項目及其構建路徑(如果尚未添加它)。這是必要的,以延長FragmentActivity後
  2. 變化AndroidLauncher活動延長FragmentActivity,不AndroidApplication
  3. 在AndroidLauncher活動
  4. 實現AndroidFragmentApplication.Callbacks創建擴展AndroidFragmentApplication這是Libgdx分片實施類。
  5. 在Fragment的onCreateView方法中添加initializeForView()代碼。
  6. 最後,用Libgdx Fragment替換AndroidLauncher活動內容。
+0

我創建了一個使用Android Studio 2.1的片段運行libgdx的Hello Word程序 - > https://github.com/kbigdelysh/libgdx-in-fragment/archive/master.zip –

+0

我希望發佈SO問題和解答包括自動鏈接到Github項目的例子代碼。 – Matthew

8

我已經使用過Android Studio 2.1中創建了一個片段運行libgdx一個Hello World program on github。它遵循the instructions on the official libgdx wiki

enter image description here

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" 

要將項目gradle這個腳本在板塊內項目(「:android」)部分。

相關問題