我正在开发游戏,多人游戏的基本要求是多点触控(同时按下两个按钮)。没有它,游戏毫无意义。所以我试图让它正常工作几天,我得出了一些结论。
我将展示我的测试课程,如果我在那里工作,那么在我的游戏中实现它是件小事。用户@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。) (当我按下两个按钮时 - &gt;两个按下) 它不适用于: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,在“低版本”手机上也失败了。提前谢谢。
答案 0 :(得分:0)
首先,您需要检查您的设备是否支持多点触控,这非常重要并且配置了功能。 您可以红色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.