- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一款游戏,多人游戏的基本要求是多点触控(同时按下两个按钮)。没有它,游戏就没有意义。所以我尝试让它正常工作几天,并得出了一些结论。
我将展示我的测试类,如果我让它在那里工作,在我的游戏中实现它是小菜一碟。用户@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,但在“较低版本”手机上也失败了。提前致谢。
最佳答案
第一个,您需要检查您的设备是否支持多点触控,这非常重要并且配置了功能。可以红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
}
您可以在不使用上下文的情况下从 Activity(服务)获取 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.
关于java - Android 2.3.x 上的多点触控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20050555/
我是一名优秀的程序员,十分优秀!