我有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添加为子视图?
答案 0 :(得分:20)
AndroidApplication类(扩展活动)有一个名为initializeForView(ApplicationListener, AndroidApplicationConfiguration)
的方法,它将返回您可以添加到布局中的View
。
因此,您的Test类可以扩展AndroidApplication而不是Activity,以便您可以调用该方法并将View添加到您的布局中。
如果这不是一个选项,出于某种原因,请看看AndroidApplication source code做了什么,并模仿它。
答案 1 :(得分:10)
我使用Android Studio 2.1为片段中的libgdx创建了Hello World program on github。它遵循the instructions on the official libgdx wiki。
AndroidLauncher类:
listen_addresses = '*'
port = 5432
max_connections = 100
shared_buffers = 128MB
work_mem = 8MB
log_destination = 'stderr'
logging_collector = on
log_line_prefix = '%t '
datestyle = 'iso, mdy'
GameFragment类:
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() {
}
}
layout.xml:
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());
}
}
MyGdxGame课程:
<?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>
确保您已添加以下内容:
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();
}
}
到&#34;依赖项{。}&#34;中的项目gradle脚本项目内部(&#34;:android&#34;)部分。
答案 2 :(得分:6)
现在,在libgdx wiki, implemented as a Fragment(现代Android应用程序的最佳实践)中,使用libgdx项目作为Android应用程序内部的视图进行了清晰记录,并附带示例代码:
- 如果您还没有添加Android V4支持库,请将其添加到-android项目及其构建路径中。这是为了稍后扩展FragmentActivity所必需的
- 更改AndroidLauncher活动以扩展FragmentActivity,而非AndroidApplication
- 在AndroidLauncher活动上实施AndroidFragmentApplication.Callbacks
- 创建一个扩展AndroidFragmentApplication的类,它是Libgdx的Fragment实现。
- 在Fragment的onCreateView方法中添加initializeForView()代码。
- 最后,将AndroidLauncher活动内容替换为Libgdx Fragment。
醇>
答案 3 :(得分: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));
}