Use libgdx on Android
Libgdx is an excellent 2D/3D cross-platform physical engine. Today we will introduce how to use it on Android:
1.
In
2.
Decompress gdx-helloworld-0.9.2.zip, and thenImport GDX-helloworld directly in eclipse,Run as Java application directly, A dialog box is displayed,We can select helloworlddesktop., As follows:
Program:
Next we will introduce how to port to Android:
1.
Create an android project for libgdx as follows:
2.
Create a new libs folder under the root directory of the project, copy the armeabi and armeabi-v7a folders under libgdx to libs, and copy GDX. jar and gdx-backend-android.jar to libsThe result is as follows:
3.
Right-click the libgdx project, select properties, and click "add jars" in libraries of Java build path to addGDX. jar and gdx-backend-android.jar, As follows:
4.
Modify the libgdxactivity. Java file as follows:
package com.test.libgdx;import android.app.Activity;import android.os.Bundle;import com.badlogic.gdx.backends.android.AndroidApplication;public class LibgdxActivity extends AndroidApplication { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initialize(new FirstGame(), false); }}
5.
Create a firstgame class, As follows:
Package COM. test. libgdx; import COM. badlogic. GDX. applicationlistener; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. graphics. gl10; import COM. badlogic. GDX. graphics. g2d. spritebatch; public class firstgame implements applicationlistener {/* spritebatch for plotting */private spritebatch batch; @ override public void create () {batch = new spritebatch (); // instantiate} @ override public void dispose () {// todo auto-generated method stub} @ override public void pause () {// todo auto-generated method stub} @ override public void render () {GDX. GL. glclear (gl10.gl _ color_buffer_bit); // clear screen batch. begin (); batch. end () ;}@ override public void resize (INT width, int height) {// todo auto-generated method stub} @ override public void resume () {// todo auto-generated method stub }}
6.
Run as Android Application in eclipse, as shown below:
Since we have not drawn any image, the result is a black screen, but at least the libgdx configuration is correct.
Done!