简体   繁体   English

2.3.x上的Android Multitouch

[英]Android Multitouch on 2.3.x

I'm working on a game and a basic requirement for multiplayer is multitouch (two buttons being pressed at the same time) . 我正在开发游戏,并且多人游戏的基本要求是多点触控(同时按下两个按钮)。 Without it, the game makes no sense. 没有它,游戏就毫无意义。 So I am trying to make it work properly for a few days and I came to some conclusions. 因此,我试图使其正常工作几天,然后得出了一些结论。

I will show my test class, if I make it work there, implementing that in my game is a piece of cake. 我将展示我的测试课程,如果我在那里进行测试的话,在我的游戏中实现这一点是小菜一碟。 User @jboi wrote me this piece of code: 用户@jboi给我写了这段代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d("TEST", "Begin onCreate");
    super.onCreate(savedInstanceState);
    Log.d("TEST", "End   onCreate");

    setContentView(R.layout.test);
    findViewById(R.id.upperTouchable).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.lowerTouchable).setOnTouchListener(new MyTouchListener());
}

private boolean lowerIsTouched = false;
private boolean upperIsTouched = false;

private void setInfo() {
    if(lowerIsTouched && upperIsTouched)
        ((TextView) findViewById(R.id.info)).setText("both touched");
    else if(lowerIsTouched)
        ((TextView) findViewById(R.id.info)).setText("only lower is touched");
    else if(upperIsTouched)
        ((TextView) findViewById(R.id.info)).setText("only upper is touched");
    else
        ((TextView) findViewById(R.id.info)).setText("non is touched");

    ((TextView) findViewById(R.id.lowerTouchable)).setText(lowerIsTouched? "touched":"not touched");
    ((TextView) findViewById(R.id.upperTouchable)).setText(upperIsTouched? "touched":"not touched");
}

private class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            if(v.getId() == R.id.lowerTouchable)
                lowerIsTouched = true;
            else if(v.getId() == R.id.upperTouchable)
                upperIsTouched = true;
            setInfo();
        }
        else if(event.getAction() == MotionEvent.ACTION_UP) {
            if(v.getId() == R.id.lowerTouchable)
                lowerIsTouched = false;
            else if(v.getId() == R.id.upperTouchable)
                upperIsTouched = false;
            setInfo();
        }
        return true;
    }
}

Layout: 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/info"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#FFFFFFFF"
android:text="Non touched"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/upperTouchable"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:text="Not touched"
    android:background="#FFF0F0F0"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/lowerTouchable"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:text="Not touched"
    android:background="#FFFFFFFF"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

I have tested this on several devices. 我已经在几种设备上进行了测试。 It works on: Htc One (4.2.2), Samsung Galaxy S4 (4.3), Samsung Galaxy S3(4.1.2.) (when i press both buttons -> both are pressed) It doesn't work on:Sony Xperia Arc S(2.3.4.) and Alcatel One Touch(2.3.6.) (when i press both buttons -> only the first button is pressed) So I came to conclusion that the android version where multitouch support starts is between 2.4 and 4.1, but that's not true, because it's been added in 2.2. 它适用于:Htc One(4.2.2),Samsung Galaxy S4(4.3),Samsung Galaxy S3(4.1.2。)(当我同时按下两个按钮->都按下时)不适用于:Sony Xperia Arc S(2.3.4。)和阿尔卡特One Touch(2.3.6。)(当我同时按下两个按钮->仅按下第一个按钮时),所以我得出的结论是,开始支持多点触控的android版本在2.4和4.1之间,但事实并非如此,因为它是在2.2中添加的。

I have 24 hours to solve this. 我有24小时来解决这个问题。 Optionally, I could make my app compatibile only with 4.0+, but I would really like to make it work on most Android phones. (可选)我可以使我的应用仅与4.0+兼容,但是我真的很想使其能够在大多数Android手机上运行。 My question is: How do I make this code work on 2.3-4.1, because I'm sure it can work (other apps have functional multitouch on those phones). 我的问题是:我如何使此代码在2.3-4.1上正常工作,因为我确定它可以正常工作(其他应用在这些手机上具有多点触控功能)。 Completely rewriting my code is also an option. 也可以完全重写我的代码。 I have tried working with ACTION_POINTER_DOWN, that also failed on "lower version" phones. 我尝试使用ACTION_POINTER_DOWN,但在“较低版本”的电话上也失败了。 Thanks in advance. 提前致谢。

first one, you need check if your device have support for multitouch, that is very important and the features configured. 首先,您需要检查您的设备是否支持多点触控,这非常重要,并且功能已配置。 You can red this article. 您可以将本文改成红色

You can check availability of the multitouch in the code: 您可以在代码中检查多点触控的可用性:

if (Integer.parseInt(Build.VERSION.SDK) >= 7) {
    PackageManager pm = context.getPackageManager();
    boolean hasMultitouch = 
        pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
    if (hasMultitouch) {
        // set multitouch event listeners
    } else {
        // set zoom buttons
    }
} else {
    // set zoom buttons
}

You can get PackageManager from your activity (service) without using context: PackageManager pm = getPackageManager(); 您可以从活动(服务)中获取PackageManager,而无需使用上下文:PackageManager pm = getPackageManager();

There are three types of multitouch you can check. 您可以检查三种类型的多点触控。

[FEATURE_TOUCHSCREEN_MULTITOUCH][2] - basic two-finger gesture detection.
[FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT][3] - tracking two or more fingers fully independently.
[FEATURE_TOUCHSCREEN_MULTITOUCH_JAZZHAND][4] - tracking a full hand of fingers fully independently - that is, 5 or more simultaneous independent pointers.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM