libgdx 退出,及获取系统时间与显示,及接口的使用
2014-01-26 15:20
337 查看
libgdx 代码工程:
package com.test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.test.testinterface.testInterface;
public class TiledMapTest implements ApplicationListener, InputProcessor {
TiledMapTest tiledMapTest;
TiledMap map;
TileAtlas atlas;
TileMapRenderer render;
OrthographicCamera cam;
Image button;
Texture texture;
Stage stage;
testInterface testInterface;
SpriteBatch batch;
BitmapFont font;
public TiledMapTest() {
// TODO Auto-generated constructor stub
}
@Override
public void create() {
button();// 按钮
map = TiledLoader.createMap(Gdx.files.internal("data/1.tmx"));
atlas = new TileAtlas(map, Gdx.files.internal("data/"));
render = new TileMapRenderer(map, atlas, 10, 10);
cam = new OrthographicCamera();
cam.setToOrtho(false, 800, 480);
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
dataTime();
render.render(cam);
}
@Override
public void resize(int width, int height) {
stage.setViewport(480, 320, true);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public boolean keyDown(int keycode) {
return true;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
public void button() {
tiledMapTest = this;
texture = new Texture(Gdx.files.internal("button/button.png"));
button = new Image(texture);
stage = new Stage();
Gdx.input.setInputProcessor(stage);
stage.addActor(button);
button.setPosition(200, 200);
button.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
// TODO Auto-generated method stub
System.out.println("输出button监听事件");
testInterface.Test();
return super.touchDown(event, x, y, pointer, button);
}
});
}
//获取系统时间并显示
public void dataTime() {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
String str = formatter.format(curDate);
font = new BitmapFont(Gdx.files.internal("font/time.fnt"),
Gdx.files.internal("font/time.png"), false);
font.setColor(252, 185, 153, 0);// 设置颜色
font.setScale(0.7f);// 字体比例大小
batch = new SpriteBatch();
batch.begin();
font.draw(batch, str, 100, 300);
batch.end();
System.out.println("yyyy年MM月dd日 HH:mm:ss ===" + str);
}
}
//接口
package com.test.testinterface;
public interface testInterface {
public void Test();
}
android 工程代码:
package com.test;
imp
c29e
ort android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.potato.R;
import com.test.TiledMapTest;
public class MainActivity extends AndroidApplication {
static MainActivity me;
public static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
me = this;
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
context = MainActivity.this;
TiledMapTest tiledMapTest = new TiledMapTest();
tiledMapTest.testInterface = new Test(this);
System.out.println("初始化成功");
initialize(tiledMapTest, cfg);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new Builder(this);
builder.setMessage("确定要退出吗?");
builder.setTitle("提示");
builder.setIcon(R.drawable.exclamation);
builder.setPositiveButton("确认",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Tools.h
dialog.dismiss();
System.exit(0);
}
});
builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(context, "取消成功", Toast.LENGTH_LONG).show();
}
});
builder.create().show();
}
public static Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
Test();
}
}
};
private static void Test() {
// TODO Auto-generated method stub
Toast.makeText(context, "回调成功", Toast.LENGTH_LONG).show();
System.out.println("回调成功");
}
}
//实现接口:
package com.test;
import com.test.testinterface.testInterface;
import android.content.Context;
import android.widget.Toast;
public class Test implements testInterface {
public Context context;
public Test(Context context) {
super();
this.context = context;
}
@Override
public void Test() {
// TODO Auto-generated method stub
// Toast.makeText(activity, "", Toast.LENGTH_SHORT).show();
System.out.println("调用自定义接口已实现");
MainActivity.handler.sendMessage(MainActivity.handler.obtainMessage(0));
}
}
package com.test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.test.testinterface.testInterface;
public class TiledMapTest implements ApplicationListener, InputProcessor {
TiledMapTest tiledMapTest;
TiledMap map;
TileAtlas atlas;
TileMapRenderer render;
OrthographicCamera cam;
Image button;
Texture texture;
Stage stage;
testInterface testInterface;
SpriteBatch batch;
BitmapFont font;
public TiledMapTest() {
// TODO Auto-generated constructor stub
}
@Override
public void create() {
button();// 按钮
map = TiledLoader.createMap(Gdx.files.internal("data/1.tmx"));
atlas = new TileAtlas(map, Gdx.files.internal("data/"));
render = new TileMapRenderer(map, atlas, 10, 10);
cam = new OrthographicCamera();
cam.setToOrtho(false, 800, 480);
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
dataTime();
render.render(cam);
}
@Override
public void resize(int width, int height) {
stage.setViewport(480, 320, true);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public boolean keyDown(int keycode) {
return true;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
public void button() {
tiledMapTest = this;
texture = new Texture(Gdx.files.internal("button/button.png"));
button = new Image(texture);
stage = new Stage();
Gdx.input.setInputProcessor(stage);
stage.addActor(button);
button.setPosition(200, 200);
button.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
// TODO Auto-generated method stub
System.out.println("输出button监听事件");
testInterface.Test();
return super.touchDown(event, x, y, pointer, button);
}
});
}
//获取系统时间并显示
public void dataTime() {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
String str = formatter.format(curDate);
font = new BitmapFont(Gdx.files.internal("font/time.fnt"),
Gdx.files.internal("font/time.png"), false);
font.setColor(252, 185, 153, 0);// 设置颜色
font.setScale(0.7f);// 字体比例大小
batch = new SpriteBatch();
batch.begin();
font.draw(batch, str, 100, 300);
batch.end();
System.out.println("yyyy年MM月dd日 HH:mm:ss ===" + str);
}
}
//接口
package com.test.testinterface;
public interface testInterface {
public void Test();
}
android 工程代码:
package com.test;
imp
c29e
ort android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.potato.R;
import com.test.TiledMapTest;
public class MainActivity extends AndroidApplication {
static MainActivity me;
public static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
me = this;
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
context = MainActivity.this;
TiledMapTest tiledMapTest = new TiledMapTest();
tiledMapTest.testInterface = new Test(this);
System.out.println("初始化成功");
initialize(tiledMapTest, cfg);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new Builder(this);
builder.setMessage("确定要退出吗?");
builder.setTitle("提示");
builder.setIcon(R.drawable.exclamation);
builder.setPositiveButton("确认",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Tools.h
dialog.dismiss();
System.exit(0);
}
});
builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(context, "取消成功", Toast.LENGTH_LONG).show();
}
});
builder.create().show();
}
public static Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
Test();
}
}
};
private static void Test() {
// TODO Auto-generated method stub
Toast.makeText(context, "回调成功", Toast.LENGTH_LONG).show();
System.out.println("回调成功");
}
}
//实现接口:
package com.test;
import com.test.testinterface.testInterface;
import android.content.Context;
import android.widget.Toast;
public class Test implements testInterface {
public Context context;
public Test(Context context) {
super();
this.context = context;
}
@Override
public void Test() {
// TODO Auto-generated method stub
// Toast.makeText(activity, "", Toast.LENGTH_SHORT).show();
System.out.println("调用自定义接口已实现");
MainActivity.handler.sendMessage(MainActivity.handler.obtainMessage(0));
}
}
相关文章推荐
- 使用js获取系统当前时间并在页面显示
- 获取系统时间几种方法 和 使用CPU时间戳进行高精度计时收藏
- 使用js时,如何获取系统当前时间并且得到格式为"yyyy年MM月"的日期
- Android使用UncaughtExceptionHandler捕获全局异常 android获取手机信息大全Java追加文件内容使用JavaMail发送邮件OkHttp获取系统时间
- 我使用过的Linux命令之date - 显示、修改系统日期时间
- 使用date对象显示系统当前时间并自动更新
- j2me中如何使用Calendar获取系统时间并转换成字符串格式
- 获取系统时间几种方法 和 使用CPU时间戳进行高精度计时收藏
- 使用JavaScript获取系统时间
- 使用C语言中的time函数获取系统时间
- Cocos2d-2.x_使用系统时间来获取随机数
- [置顶] Android开发—使用系统控件获取当前时间
- 使用JavaScript显示当前系统时间
- VC++获取系统当前时间并显示
- 获取系统时间几种方法 和 使用CPU时间戳进行高精度计时
- 数据库获取系统时间与使用
- 使用CTime类,获取系统时间C/MFC
- js实现动态获取系统时间,显示到页面上
- JS获取系统时间,并在前台显示
- 使用线程 在shell上同步动态显示当前系统时间