分享:libgdx调用平台原生UI提示信息
2016-02-29 18:03
405 查看
libgdx的中文支持需要开发者做一些额外的工作,所以在不追求UI风格统一的情况下(或偷懒),我会选择调用平台的原生UI。例如加载游戏完毕后,需要提示玩家“游戏加载完成,点击屏幕进入游戏”,虽然中文字数不多,但为了偷懒,我不去打开hero生成fnt,也不用导入freetype库,甚至不用libgdx的UI库去创建一个窗口,我只是写了个接口,用接口去调用android的Toast.makeText(String massage)就行鸟。
所以我需要一个在core工程的接口类,然后在android工程实现这个类,至于html,desktop工程是类似的。值得一提的是,libgdx本身也提供了调用平台UI功能的一个接口:Gdx.input.getTextInput,这个接口功能是弹出平台UI的一个输入框。我们自己实现的UI调用接口正是参考此处。
UI调用抽象接口:
http://blog.sina.com.cn/s/blog_923db0a30102vssg.html
而callPay接口是为支付服务的,这要看开发者使用的是怎样的第三方支付包。libgdx官方有gdx-pay开源库(https://github.com/libgdx/gdx-pay/wiki)专门处理支付服务,它连这方面的接口都帮你完成了,而且仍旧跨平台(android,ios,html,lwjgl),它所用的支付平台是诸如googleplay的国际平台,如果想接入国内的支付宝,微信支付,你得自己再做些工作。gdx-pay的开发文档并不复杂,找天写个支付的博文,翻译它一下。
这里是PlatformResolver的实现类:
需要注意的是线程问题,注意handler的正确使用,Gdx.app.postRunable接口是切入libgdx的游戏主线程。如果调试中出现线程缺少looper的日志,可以将Handler hd如下创建new Handler(_context.getMainLooper())就会保险点。
关于平台接口创建后如何使用很简单,libgdx官方wiki有技术文档,你最后可以再去看看:https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code
还有一篇官方wiki也写得很好,里面有一小节讲怎么在android中使用Handler在游戏中控制第三方广告开启与暂停https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx#control:
所以我需要一个在core工程的接口类,然后在android工程实现这个类,至于html,desktop工程是类似的。值得一提的是,libgdx本身也提供了调用平台UI功能的一个接口:Gdx.input.getTextInput,这个接口功能是弹出平台UI的一个输入框。我们自己实现的UI调用接口正是参考此处。
UI调用抽象接口:
package gdx.platform; import com.badlogic.gdx.graphics.g2d.BitmapFont; public interface PlatformResolver { /** * 返回网络是否连接 * @return */ public boolean isNetEnable(); /** * 弹出文字信息 * @param context 要显示的信息 */ public void showQuickTip(String context); /** * 弹出一个带按钮的窗口 * @param title 窗口标题文字 * @param context 窗口内容文字 * @param callback 按钮按下的在游戏线程中的回调, */ public void showQucikDialog(String title,String context,Runnable callback); public BitmapFont getFont(String characters); public void callPay(String payee,int money); }最后的2个接口还没实现好,读者可以自己去试试。getFont接口是为了用android平台中的系统字库来生成中文的BitmapFont,这方面可以参考一位开发者的博文:
http://blog.sina.com.cn/s/blog_923db0a30102vssg.html
而callPay接口是为支付服务的,这要看开发者使用的是怎样的第三方支付包。libgdx官方有gdx-pay开源库(https://github.com/libgdx/gdx-pay/wiki)专门处理支付服务,它连这方面的接口都帮你完成了,而且仍旧跨平台(android,ios,html,lwjgl),它所用的支付平台是诸如googleplay的国际平台,如果想接入国内的支付宝,微信支付,你得自己再做些工作。gdx-pay的开发文档并不复杂,找天写个支付的博文,翻译它一下。
这里是PlatformResolver的实现类:
package dawn.game; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.widget.Toast; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.BitmapFont; import hif.game.PlatformResolver; public class AndroidPr implements PlatformResolver { Context _context; Handler hd; public AndroidPr(Context context){ _context=context; hd=new Handler(); } public boolean isNetEnable() { // TODO Auto-generated method stub ConnectivityManager connectivityManager = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager == null) { return false; } else { NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo(); if (networkInfo != null && networkInfo.length > 0) { for (int i = 0; i < networkInfo.length; i++) { System.out.println(i + "==net state==" + networkInfo[i].getState()); System.out.println(i + "===net type===" + networkInfo[i].getTypeName()); if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } public void showQuickTip(final String context) { // TODO Auto-generated method stub hd.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Toast.makeText(_context, context, Toast.LENGTH_LONG).show(); } }); } public BitmapFont getFont(String characters) { // TODO Auto-generated method stub return null; } @Override public void callPay(final String payee,final int money) { // TODO Auto-generated method stub hd.post(new Runnable() { public void run() { Intent intent=new Intent(_context, ShoppingCartActivity.class); Bundle bundle=new Bundle(); bundle.putString("payee",payee); bundle.putInt("money", money); intent.putExtras(bundle); _context.startActivity(intent); } }); } @Override public void showQucikDialog(final String title, final String context, final Runnable callback) { // TODO Auto-generated method stub hd.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub new AlertDialog.Builder(_context) .setTitle(title ) .setMessage(context) .setPositiveButton("确定" ,new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Gdx.app.postRunable(callback); } }) .show(); } }); } }
需要注意的是线程问题,注意handler的正确使用,Gdx.app.postRunable接口是切入libgdx的游戏主线程。如果调试中出现线程缺少looper的日志,可以将Handler hd如下创建new Handler(_context.getMainLooper())就会保险点。
关于平台接口创建后如何使用很简单,libgdx官方wiki有技术文档,你最后可以再去看看:https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code
还有一篇官方wiki也写得很好,里面有一小节讲怎么在android中使用Handler在游戏中控制第三方广告开启与暂停https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx#control:
相关文章推荐
- Activity UI Handler ProgressBar AsyncTask
- openwrt 使用quilt制作patch
- RequestContext中的buffered与lazy-commit服务
- miui7 apktool反编译
- ios UIScrollView不能响应TouchesBegin:的事件的解决办法
- UITableView缩短自带的分割线
- Google推出iOS功能性UI测试框架EarlGrey
- 获取UINavigationbar背景图片
- 17.UITextView
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Creating a Fragment) (一)
- PAT 1101. Quick Sort (25)
- Maven skip unit test and build to directory
- iOS之一句话去除UITableView底部多余行及分割线
- Android 线程本地变量<二> ThreadLocal Values源码解析
- 58.The PART_CODE column in the SPARES table contains the following list of values:
- IOS开发限制UITextField只可以输入数字
- iOS开关-UISwitch
- 18.写在二月份的尾巴,UIView animation 的奇怪需求
- 使用UIBezierPath和CAShapeLayer画各种图形
- iOS 给UILabel文字加下划线