xbox android app,How to implement Xbox controller in Android app?

I’ve been working on a few Android games in my spare time. With the

rollout of Honeycomb to my Logitech Revue, I’ve been able to finally

test these games on it. One of the things I noticed was that the Revue

has USB Host support and (wired) Xbox 360 controllers are simply plug

and play. Using them is quite simple as well; the button presses are

mostly mapped to the key event. The left and right triggers, right

directional stick, and analog inputs are not mapped through this

method however. The left analog stick and directional pads have their

inputs mapped to direction buttons.

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

switch(keyCode) {

case KeyEvent.KEYCODE_BUTTON_L1:

case KeyEvent.KEYCODE_BUTTON_R1:

case KeyEvent.KEYCODE_BUTTON_THUMBR:

case KeyEvent.KEYCODE_BUTTON_THUMBL:

case KeyEvent.KEYCODE_DPAD_LEFT:

case KeyEvent.KEYCODE_DPAD_RIGHT:

case KeyEvent.KEYCODE_DPAD_UP:

case KeyEvent.KEYCODE_DPAD_DOWN:

case KeyEvent.KEYCODE_BUTTON_START:

case KeyEvent.KEYCODE_BUTTON_MODE://Big button in the middle

case KeyEvent.KEYCODE_BUTTON_B:

case KeyEvent.KEYCODE_BUTTON_A:

case KeyEvent.KEYCODE_BUTTON_X:

case KeyEvent.KEYCODE_BUTTON_Y:

default:

return super.onKeyDown(keyCode, keyEvent);

}

}

Of special note, KeyEvent.KEYCODE_BUTTON_B goes back and

KeyEvent.KEYCODE_BUTTON_START pulls up home bar in the default

implementation. Also, the “A” button has special behaviors which I

don’t fully understand. For the analog controls however, we must

override the method onGenericMotionEvent as follows:

@Override

public boolean onGenericMotionEvent(MotionEvent event) {

Log.d("Right Trigger Value", event.getAxisValue(MotionEvent.AXIS_RTRIGGER) + "");

Log.d("Left Trigger Value", event.getAxisValue(MotionEvent.AXIS_LTRIGGER) + "");

Log.d("Left Stick X", event.getX() + "");

Log.d("Left Stick Y", event.getY() + "");

Log.d("Right Stick Y", event.getAxisValue(MotionEvent.AXIS_RZ) + "");

Log.d("Right Stick X", event.getAxisValue(MotionEvent.AXIS_Z) + "");

return super.onGenericMotionEvent(event);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值