【发布时间】: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,但在“低版本”手机上也失败了。提前致谢。
【问题讨论】: