我目前正在使用具有客户端/服务器系统的游戏手柄/游戏控制器功能的android应用程序,其中客户端为android,服务器为PC中的Java桌面应用程序,并以蓝牙作为连接。到目前为止,我的应用程序只能用一根手指(按住或单击)工作,现在我正在事件保持按钮A +单击按钮B或按住按钮A +保持按钮B的情况下,用两根手指使用多点触摸。 t了解如何在android上执行操作的想法,使用mCommandService(BluetoothCommandService.valueOfButon)将事件发送给java,读入Java表示按住按钮A时单击了按钮B,并使用robot.keyPress执行值表示。

我实现了来自该博客http://luugiathuy.com/2011/02/android-java-bluetooth/的示例,用于实现android和java之间的蓝牙连接。这是我在Android上的onTouch方法

public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
    case R.id.tombolL1:
        cmd = BluetoothCommandService.aksi_l1;
    break;

    case R.id.tombolL2:
        cmd = BluetoothCommandService.aksi_l2;
    break;

    case R.id.tombolR1:
        cmd = BluetoothCommandService.aksi_r1;
    break;

    case R.id.tombolR2:
        cmd = BluetoothCommandService.aksi_r2;
    break;

    case R.id.tombolAtas:
        cmd = BluetoothCommandService.arah_atas;
    break;

    case R.id.tombolBawah:
        cmd = BluetoothCommandService.arah_bawah;
    break;

    case R.id.tombolKiri:
        cmd = BluetoothCommandService.arah_kiri;
    break;

    case R.id.tombolKanan:
        cmd = BluetoothCommandService.arah_kanan;
    break;

    case R.id.tombolSegitiga:
        cmd = BluetoothCommandService.aksi_segitiga;
    break;

    case R.id.tombolBulat:
        cmd = BluetoothCommandService.aksi_bulat;
    break;

    case R.id.tombolSilang:
        cmd = BluetoothCommandService.aksi_silang;
    break;

    case R.id.tombolKotak:
        cmd = BluetoothCommandService.aksi_kotak;
    break;

}

switch(action){
    case MotionEvent.ACTION_DOWN:
        handler1.removeCallbacks(mUpdateTask);
        handler1.postAtTime(mUpdateTask, 200);
    break;

    case MotionEvent.ACTION_UP:
        handler1.removeCallbacks(mUpdateTask);
    break;
}
return false;
}

private Runnable mUpdateTask = new Runnable() {

    @Override
    public void run() {
        mCommandService.write(cmd);
        handler1.postAtTime(this, SystemClock.uptimeMillis() + 100);
        //handler1.postAtTime(this, 200);
    }
};


还有Java中的processCommand方法

private void processCommand(int command) {
    try {
        Robot robot = new Robot();
        int key = 0;
        switch(command){
            case arah_atas:
                key = KeyEvent.VK_UP;
            break;
            case arah_bawah:
                key = KeyEvent.VK_DOWN;
            break;
            case arah_kiri:
                key = KeyEvent.VK_LEFT;
            break;
            case arah_kanan:
                key = KeyEvent.VK_RIGHT;
            break;
            case aksi_segitiga:
                key = KeyEvent.VK_A;
            break;
            case aksi_bulat:
                key = KeyEvent.VK_X;
            break;
            case aksi_silang:
                key = KeyEvent.VK_Z;
            break;
            case aksi_kotak:
                key = KeyEvent.VK_S;
            break;
            case aksi_l1:
                key = KeyEvent.VK_Q;
            break;
            case aksi_l2:
                key = KeyEvent.VK_W;
            break;
            case aksi_r1:
                key = KeyEvent.VK_R;
            break;
            case aksi_r2:
                key = KeyEvent.VK_E;
            break;
            case aksi_select:
                key = KeyEvent.VK_SPACE;
            break;
            case aksi_start:
                key = KeyEvent.VK_M;
            break;
        }

        robot.keyPress(key);
        Thread.sleep(50);
        robot.keyRelease(key);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


请让我知道我该怎么做?

最佳答案

作为精通Android中的多点触控的人,我建议使用此this库。它抽象了处理多点触控的烦恼的烦恼。您可以缩放,旋转等。它为多点触控有问题的手机提供支持,并提供一些演示项目。而且(您之前已经做过),您可以提取转换矩阵并将此信息存储在数据库中,然后将其重新应用于图形,以便正确初始化它们。

10-05 21:17
查看更多