有没有一种方法可以在一个Activity中同时使用多个Libgdx View?

时间:2019-04-08 06:07:12

标签: android opengl-es libgdx android-graphics

我正在尝试在单个Activity中添加两个AndroidFragmentApplication。但是只有最后添加的片段实际上在屏幕上呈现。其他片段未显示任何内容。我需要怎么做才能绘制两个视图?

我首先尝试使用AndroidApplication类初始化视图。但这导致应用程序崩溃。然后,我添加了多个片段来扩展AndroidFragmentApplication。现在,该应用程序不会崩溃,而是仅在屏幕上绘制最后添加的片段。

这是我要添加片段的地方-

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <fragment
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:id="@+id/alpha_fragment"
        android:name="com.sample.libgdx_final.AlphaFragment" />

    <fragment
        android:id="@+id/coin_fragment"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:name="com.sample.libgdx_final.CoinFragment"
        />
</FrameLayout>

这是我的Fragment和ApplicationListener -

public class CoinFragment extends AndroidFragmentApplication implements AndroidFragmentApplication.Callbacks{
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initializeForView(new CoinTranslateLibgdx());
    }
}

CoinTranslateLibgdx.java

class CoinTranslateLibgdx extends ApplicationAdapter {
    private SpriteBatch batch;
    private Texture texture;
    private Sprite sprite;
    private float deltaTime = 0f;
    private boolean increasing = true;

    @Override
    public void create() {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("token.png"));
        sprite = new Sprite(texture);
        sprite.setCenterX(Gdx.graphics.getWidth() / 2f);
        sprite.setCenterY(Gdx.graphics.getHeight());
    }
    @Override
    public void render() {
        Gdx.gl20.glClearColor(0, 0, 0, 0);
        batch.begin();
        if (deltaTime <= 1 && increasing) {
            deltaTime += Gdx.graphics.getDeltaTime() * 1f;
        } else if (deltaTime >= 0.09) {
            increasing = false;
            deltaTime -= Gdx.graphics.getDeltaTime() * 1f;
        } else {
            increasing = true;
        }
        batch.setColor(1, 1, 1, deltaTime);
        batch.draw(sprite, 0, 0);
        batch.end();
    }
    @Override
    public void dispose() {
        batch.dispose();
        texture.dispose();
    }
}

0 个答案:

没有答案