zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android提高之手游转电视游戏的模拟操控

Android游戏模拟 提高 电视 操控 之手
2023-06-13 09:15:41 时间

目前智能电视终端(智能电视和智能电视盒子)已经越来越火,过去主打视频功能,如今的智能电视终端不仅会继续完善视频功能,还会加入电视游戏功能,同时这也赶上了“电视游戏机解禁”的时机。

当今的大部分Android手游都能够在Android系统的电视终端上运行,其中有少数手游是原生支持手柄(例如MOGA手柄),这部分游戏可以作为电视游戏。但其他手游(射击,赛车,动作等游戏)若要在电视上玩,就需要修改操控模式,把触摸屏操控改为手柄实体键操控。

程序运行结果如下图所示:

 

本文核心RootCommand.java的代码如下,不建议把代码浓缩成全局静态方法,这里保持process和os这2个变量的生命周期直到app结束,可以减去多次初始化/释放的耗时。具体代码如下:

packagecom.hellogv.slinput;
importjava.io.DataOutputStream;
importjava.io.IOException;
importandroid.util.Log;
/**
*调用su执行input命令
*全局只调用一次init()和exit(),多次调用run()。
*@authorhellogv
*
*/
publicclassRootCommand{
privateStringTAG="RootCommand";
privateProcessprocess=null;
privateDataOutputStreamos=null;
publicvoidinit(){
try{
process=Runtime.getRuntime().exec("su");
os=newDataOutputStream(process.getOutputStream());
}catch(IOExceptione){
Log.e(TAG,getExceptionMessage(e));
}
}
/**
*模仿shell来执行命令,必须先root再使用
*
*@paramcommand
*@return
*/
publicbooleanrun(Stringcommand){
try{
os.writeBytes(command+"\n");
os.flush();
}catch(Exceptione){
Log.e(TAG,getExceptionMessage(e));
returnfalse;
}
returntrue;
}
/**
*模仿shell来执行命令,必须先root再使用
*
*@paramcommand
*@return
*/
publicvoidrelease(){
try{
os.writeBytes("exit\n");
os.flush();
process.waitFor();
}catch(Exceptione){
Log.e(TAG,getExceptionMessage(e));
}finally{
try{
if(os!=null){
os.close();
}
process.destroy();
}catch(Exceptione){
}
}
}
privatestaticStringgetExceptionMessage(Exceptionex){
Stringresult="";
StackTraceElement[]stes=ex.getStackTrace();
for(inti=0;i<stes.length;i++){
result=result+stes[i].getClassName()
+"."+stes[i].getMethodName()
+""+stes[i].getLineNumber()+"line"
+"\r\n";
}
returnresult;
}
}

调用RootCommand的代码如下,input命令的使用格式详见代码:

publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootCommand.init();
//模拟按下Home键
btnTestKey=(Button)this.findViewById(R.id.btnTestKey);
btnTestKey.setOnClickListener(newOnClickListener(){

@Override
publicvoidonClick(Viewv){
//命令格式:inputkeyeventkeycode
rootCommand.run("/system/bin/inputkeyevent"+KeyEvent.KEYCODE_HOME);
}
});
//模拟滑动触摸屏
btnTestSwipe=(Button)this.findViewById(R.id.btnTestSwipe);
btnTestSwipe.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
intx2=MainActivity.this.getWindow().getDecorView().getWidth()-10;
//先去到桌面
rootCommand.run("/system/bin/inputkeyevent"+KeyEvent.KEYCODE_HOME);
//滑动桌面,命令格式:inputswipex1y1x2y2
for(inti=0;i<4;i++){
rootCommand.run("/system/bin/inputswipe10300"+x2+"400");
rootCommand.run("/system/bin/inputswipe"+x2+"30010400");
}
}
});
//模拟点击触摸屏
btnTestTap=(Button)this.findViewById(R.id.btnTestTap);
btnTestTap.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
int[]location=newint[2];
btnTestSwipe.getLocationOnScreen(location);
intx=location[0]+btnTestSwipe.getWidth()/2;
inty=location[1]+btnTestSwipe.getHeight()/2;
//模拟点击btnTestTap
rootCommand.run("/system/bin/inputtap"+x+""+y);
}
});
//退出程序
btnExit=(Button)this.findViewById(R.id.btnExit);
btnExit.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
rootCommand.release();
MainActivity.this.finish();
}
});
//判断是否root过,没root过不可用
if(RootTools.isRootAvailable()==false){
Toast.makeText(this,"本程序需要使用ROOT权限。",Toast.LENGTH_SHORT).show();
this.finish();
}
}

感兴趣的朋友可以下载本实例的完整代码加以调试运行,相信会对大家的Android程序设计有很大的帮助。