[英]Android Multitouch on 2.3.x
我正在開發游戲,並且多人游戲的基本要求是多點觸控(同時按下兩個按鈕)。 沒有它,游戲就毫無意義。 因此,我試圖使其正常工作幾天,然后得出了一些結論。
我將展示我的測試課程,如果我在那里進行測試的話,在我的游戲中實現這一點是小菜一碟。 用戶@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),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中添加的。
我有24小時來解決這個問題。 (可選)我可以使我的應用僅與4.0+兼容,但是我真的很想使其能夠在大多數Android手機上運行。 我的問題是:我如何使此代碼在2.3-4.1上正常工作,因為我確定它可以正常工作(其他應用在這些手機上具有多點觸控功能)。 也可以完全重寫我的代碼。 我嘗試使用ACTION_POINTER_DOWN,但在“較低版本”的電話上也失敗了。 提前致謝。
首先,您需要檢查您的設備是否支持多點觸控,這非常重要,並且功能已配置。 您可以將本文改成紅色。
您可以在代碼中檢查多點觸控的可用性:
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.