你已经很清楚地看到了市场上那些非常便宜的安卓电视机箱。它们的后面通常有一个遥控器,它具有一些功能,如点击、滑动或向左、向右上下滑动。
最近,我制作了一个应用程序,并试图使用遥控器导航它。我在这个项目中有一些手势方法。我试着向左和右滑动,但是当我在有屏幕的手机上尝试时,这个应用没有做任何事情,得到了这个手势,并做了它应该做的事情。比如打开导航抽屉等。
现在我的问题是:是否需要使用特殊的方法?有什么规则值得注意吗?
编辑,,这就是我到目前为止所做的。我创建了一个定义操作的类:它来自google.developer
public class Dpad {
public final static int UP = 0;
public final static int LEFT = 1;
public final static int RIGHT = 2;
public final static int DOWN = 3;
public final static int CENTER = 4;
int directionPressed = -1; // initialized to -1
public int getDirectionPressed(InputEvent event) {
if (!isDpadDevice(event)) {
return -1;
}
// If the input event is a MotionEvent, check its hat axis values.
if (event instanceof MotionEvent) {
// Use the hat axis value to find the D-pad direction
MotionEvent motionEvent = (MotionEvent) event;
float xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
float yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);
// Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
// LEFT and RIGHT direction accordingly.
if (Float.compare(xaxis, -1.0f) == 0) {
directionPressed = Dpad.LEFT;
} else if (Float.compare(xaxis, 1.0f) == 0) {
directionPressed = Dpad.RIGHT;
}
// Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
// UP and DOWN direction accordingly.
else if (Float.compare(yaxis, -1.0f) == 0) {
directionPressed = Dpad.UP;
} else if (Float.compare(yaxis, 1.0f) == 0) {
directionPressed = Dpad.DOWN;
}
}
// If the input event is a KeyEvent, check its key code.
else if (event instanceof KeyEvent) {
// Use the key code to find the D-pad direction.
KeyEvent keyEvent = (KeyEvent) event;
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
directionPressed = Dpad.LEFT;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
directionPressed = Dpad.RIGHT;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
directionPressed = Dpad.UP;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
directionPressed = Dpad.DOWN;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
directionPressed = Dpad.CENTER;
}
}
return directionPressed;
}
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
!= InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}
}在我的MainActivity里我有一个导航抽屉。当远程控制D-pad想要的时候,我想打开和关闭
mDrawerLayout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
@Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
if (Dpad.isDpadDevice(motionEvent)) {
int press = mDpad.getDirectionPressed(motionEvent);
switch (press) {
case Dpad.RIGHT:
// Do something for UP direction press Open the drawer
mDrawerLayout.openDrawer(Gravity.START);
return true;
case Dpad.LEFT:
mDrawerLayout.closeDrawer(Gravity.START);
return true;
}
}
return false;
}
});在其中一个片段中,我有一个媒体播放器,使用D向上和D向下,我改变了视频。
v.setOnGenericMotionListener(new View.OnGenericMotionListener() {
@Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
if (Dpad.isDpadDevice(motionEvent)) {
int press = mDpad.getDirectionPressed(motionEvent);
switch (press) {
case Dpad.UP:
// Do something for UP direction press
UP(); // Change the video to next
return true;
case Dpad.DOWN:
DOWN(); // Change the video the earlier on
return true;
}
}
return false;
}
});编辑它现在成为一个问题,因为它不响应任何运动。我在一个带有物理键盘的仿真器上试了试,没有一个动作发生。如果有人给我暗示,我会同意的。我指的是下面图片中的这种遥控器

提前感谢
发布于 2017-01-19 09:32:57
我认为Android开发人员的代码是错误的:
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
!= InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}条件应该是"==“而不是"!=",因此这是正确的:
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
== InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}https://stackoverflow.com/questions/32729378
复制相似问题