libgdx android的控件,How to add libgdx as a sub view in android

I have created a Hello World program on github for libgdx running in a fragment using Android Studio 2.1. It follows the instructions on the official libgdx wiki.

5i3gU.png

AndroidLauncher class:

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() {

}

}

The GameFragment class:

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());

}

}

layout.xml:

android:id="@+id/main_layout"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/content_framelayout"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="2">

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 :("/>

MyGdxGame class:

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();

}

}

Make sure you've added the following:

compile "com.android.support:support-v4:24.1.1"

To the project gradle script in the "dependencies {.}" section inside project (":android") section.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值