0

メソッドで Bluetooth ゲームパッド コントローラーから軸の位置を受け取りdispatchGenericMotionEvent(android.view. MotionEvent)ます。私の方法:

    @Override
public boolean dispatchGenericMotionEvent(final MotionEvent event) {
    if( mPadListener==null ||
            (event.getSource()&InputDeviceCompat.SOURCE_JOYSTICK)!=InputDeviceCompat.SOURCE_JOYSTICK ){
        return super.dispatchGenericMotionEvent(event);
    }

    int historySize = event.getHistorySize();
    for (int i = 0; i < historySize; i++) {
        // Process the event at historical position i
        Log.d("JOYSTICKMOVE",event.getHistoricalAxisValue(MotionEvent.AXIS_Y,i)+"  "+event.getHistoricalAxisValue(MotionEvent.AXIS_Z,i));
    }
    // Process current position
    Log.d("JOYSTICKMOVE",event.getAxisValue(MotionEvent.AXIS_Y)+" "+event.getAxisValue(MotionEvent.AXIS_Z));

    return true;
}

問題は、すべてのジョイスティックの軸を離したときに、ログに最後の軸の値 (0,0) が表示されないことです。たとえば (0.23,0.11) で停止し、適切な値が次の移動イベントの後にのみ logcat に表示されます。しかも、通常のボタンを押しても状況は同じ(ボタンイベントは全く別の方法でキャッチdispatchKeyEvent(android.view.KeyEvent)

どうしたの ?

4

1 に答える 1

0

ゼロ位置の MotionEvent.ACTION_MOVE イベントを取得しますが、受け取る値は必ずしもゼロではありません。ジョイスティックのフラット レンジを取得する必要があります。これにより、ジョイスティックが静止していると見なす値が得られます (つまり、フラット レンジを下回っている場合は、ゼロの位置にあります)。フラット範囲を修正する getCenteredAxis を参照してください ( https://developer.android.com/training/game-controllers/controller-input.html ):

private static float getCenteredAxis(MotionEvent event,
        InputDevice device, int axis, int historyPos) {
    final InputDevice.MotionRange range =
            device.getMotionRange(axis, event.getSource());

    // A joystick at rest does not always report an absolute position of
    // (0,0). Use the getFlat() method to determine the range of values
    // bounding the joystick axis center.
    if (range != null) {
        final float flat = range.getFlat();
        final float value =
                historyPos < 0 ? event.getAxisValue(axis):
                event.getHistoricalAxisValue(axis, historyPos);

        // Ignore axis values that are within the 'flat' region of the
        // joystick axis center.
        if (Math.abs(value) > flat) {
            return value;
        }
    }
    return 0;
}
于 2016-12-02T20:32:08.593 に答える