【问题标题】:How to get the trigger Buttons of a PS4 Controller into Android?如何将 PS4 控制器的触发按钮输入 Android?
【发布时间】:2018-03-03 17:46:46
【问题描述】:

我正在尝试将我的 PS4 控制器作为模型导入 Android 应用程序。 Androids Developer Page(操纵杆和数字按钮)的示例对我来说很好用。我用 Log.e 输出检查了这一点。我不明白如何获得 R2 和 L2 的值。在开发者页面上是这样描述的:

处理肩部触发按压(但提供替代输入法)。一些控制器具有左右肩触发器。如果存在这些触发器,Android 将左触发器按下报告为 AXIS_LTRIGGER 事件,将右触发器按下报告为 AXIS_RTRIGGER 事件。在 Android 4.3(API 级别 18)上,生成 AXIS_LTRIGGER 的控制器还报告 AXIS_BRAKE 轴的相同值。 AXIS_RTRIGGER 和 AXIS_GAS 也是如此。 Android 报告所有模拟触发按下,标准化值从 0.0(释放)到 1.0(完全按下)。并非所有控制器都有触发器,因此请考虑允许玩家使用其他按钮执行这些游戏操作。

添加这一行:

float rTrigger = historyPos

到 processJoystickInput():float rtrigger 总是 0.0

真的很期待把事情做好。

谢谢

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean handled = false;
        if ((event.getSource() & InputDevice.SOURCE_GAMEPAD)
                == InputDevice.SOURCE_GAMEPAD) {
            if (event.getRepeatCount() == 0) {

                if (keyCode == 96)
                    Log.e("Taste:", "Square pressed");

                if (keyCode == 97)
                    Log.e("Taste:", "Cross pressed");

                if (keyCode == 98)
                    Log.e("Taste:", "Circle pressed");
            }
            if (handled) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        boolean handled = false;
        if ((event.getSource() & InputDevice.SOURCE_GAMEPAD)
                == InputDevice.SOURCE_GAMEPAD) {
            if (event.getRepeatCount() == 0) {

                if (keyCode == 96)
                    Log.e("Taste:", "Square released");

                if (keyCode == 97)
                    Log.e("Taste:", "X released");

                if (keyCode == 98)
                    Log.e("Taste:", "Circle released");
            }
            if (handled) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {


        // Check that the event came from a game controller
        if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) ==
                InputDevice.SOURCE_JOYSTICK &&
                event.getAction() == MotionEvent.ACTION_MOVE) {


            // Process all historical movement samples in the batch
            final int historySize = event.getHistorySize();

            // Process the movements starting from the
            // earliest historical position in the batch
            for (int i = 0; i < historySize; i++) {
                // Process the event at historical position i
                processJoystickInput(event, i);
            }

            // Process the current movement sample in the batch (position -1)
            processJoystickInput(event, -1);
            return true;
        }
        return super.onGenericMotionEvent(event);
    }


    private void processJoystickInput(MotionEvent event,
                                      int historyPos) {

        InputDevice mInputDevice = event.getDevice();

        // Calculate the horizontal distance to move by
        // using the input value from one of these physical controls:
        // the left control stick, hat axis, or the right control stick.
        float lx = getCenteredAxis(event, mInputDevice,
                MotionEvent.AXIS_X, historyPos);

        float rx = getCenteredAxis(event, mInputDevice,
                MotionEvent.AXIS_Z, historyPos);

        float ly = getCenteredAxis(event, mInputDevice,
                MotionEvent.AXIS_Y, historyPos);

        float ry = getCenteredAxis(event, mInputDevice,
                MotionEvent.AXIS_RZ, historyPos);


        Log.e("LX:", lx + "");
        Log.e("LY:", ly + "");
        Log.e("RX:", rx + "");
        Log.e("RY:", ry + "");
    }

    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;
    }
}

【问题讨论】:

  • 如果我没看错,你会和他们演示操纵杆处理的方式非常相似,只是不需要考虑平坦区域。只需修改他们的示例,您将在 processJoystickInput() 中为每个触发器添加另一个值。例如,正确的触发值类似于:float rTrigger = historyPos &lt; 0 ? event.getAxisValue(MotionEvent.AXIS_RTRIGGER) : event.getHistoricalAxisValue(MotionEvent.AXIS_RTRIGGER, historyPos);。
  • 这正是问题所在。将此行添加到 processJoystickInput(): float rtrigger is alwas 0.0
  • 你需要把它放在问题中。
  • 不。我没有办法测试,atm。如果我知道这是问题所在,我就不会说什么了。 :-)

标签: android triggers controller axis ps4


【解决方案1】:

自己解决了这个问题。触发按钮与 Axis_RX 和 Axis_RY 匹配

【讨论】:

    猜你喜欢
    • 2014-05-18
    • 2011-06-13
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多