【问题标题】:Android Multitouch on 2.3.x2.3.x 上的 Android 多点触控
【发布时间】:2013-11-18 22:50:01
【问题描述】:

我正在开发一款游戏,多人游戏的基本要求是多点触控(同时按下两个按钮)。没有它,游戏毫无意义。所以我试图让它正常工作几天,我得出了一些结论。

我将展示我的测试类,如果我让它在那里工作,那么在我的游戏中实现它就是小菜一碟。用户@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;
    }
}

布局:

<?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>

我已经在几台设备上对此进行了测试。 它适用于:Htc One (4.2.2)、三星 Galaxy S4 (4.3)、三星 Galaxy S3(4.1.2.) (当我按下两个按钮时 - > 都按下了) 不适用于:Sony Xperia Arc S(2.3.4.) 和 Alcatel One Touch(2.3.6.) (当我按下两个按钮时 - >只按下第一个按钮) 所以我得出的结论是,开始支持多点触控的android版本在2.4和4.1之间,但事实并非如此,因为它是在2.2中添加的。

我有 24 小时的时间来解决这个问题。或者,我可以让我的应用程序仅与 4.0+ 兼容,但我真的很想让它在大多数 Android 手机上运行。我的问题是:如何使此代码在 2.3-4.1 上工作,因为我确信它可以工作(其他应用程序在这些手机上具有功能多点触控)。完全重写我的代码也是一种选择。我曾尝试使用 ACTION_POINTER_DOWN,但在“低版本”手机上也失败了。提前致谢。

【问题讨论】:

    标签: java android


    【解决方案1】:

    第一个,您需要检查您的设备是否支持多点触控,这非常重要并且配置了功能。 你可以红this article.

    您可以在代码中检查多点触控的可用性:

    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
    }
    

    您可以在不使用上下文的情况下从您的活动(服​​务)中获取 PackageManager:PackageManager pm = getPackageManager();

    您可以检查三种类型的多点触控。

    [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.
    

    【讨论】:

    • 我现在不在家,我试试看。但是有多点触控的应用程序可以在 Arc S 上运行,所以我想这意味着 Arc S 支持它,我不需要检查? (例如 Play 商店中的《巫师战争》)
    猜你喜欢
    • 1970-01-01
    • 2015-07-11
    • 2012-08-11
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多