libgdx 退出,及獲取系統時間與顯示,及接口的使用

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;


import 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));
}
}





發佈了23 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章