- Android 基礎
- Android - 首頁
- Android - 概述
- Android - 環境搭建
- Android - 架構
- Android - 應用元件
- Android - Hello World 例子
- Android - 資源
- Android - 活動 (Activities)
- Android - 服務 (Services)
- Android - 廣播接收器 (Broadcast Receivers)
- Android - 內容提供者 (Content Providers)
- Android - 碎片 (Fragments)
- Android - 意圖/過濾器 (Intents/Filters)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送郵件
- Android - 傳送簡訊
- Android - 打電話
- 釋出 Android 應用
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕捉
- Android - 音訊管理器 (AudioManager)
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 圖片特效
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - LinkedIn 整合
- Android - 載入進度指示器
- Android - 本地化
- Android - 登入介面
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - ProgressBar
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問答
- Android - 實用資源
- Android - 討論
Android - 多點觸控
多點觸控手勢是指多個手指同時觸控式螢幕幕。Android 允許我們檢測這些手勢。
當多個手指同時觸控式螢幕幕時,Android 系統會生成以下觸控事件。
| 序號 | 事件及描述 |
|---|---|
| 1 |
ACTION_DOWN 第一個觸控式螢幕幕的指標。這將啟動手勢。 |
| 2 |
ACTION_POINTER_DOWN 除第一個指標之外,其他進入螢幕的指標。 |
| 3 |
ACTION_MOVE 按下期間發生了更改。 |
| 4 |
ACTION_POINTER_UP 非主要指標抬起時傳送。 |
| 5 |
ACTION_UP 最後一個指標離開螢幕時傳送。 |
因此,為了檢測上述任何事件,您需要重寫onTouchEvent()方法並手動檢查這些事件。其語法如下所示:
public boolean onTouchEvent(MotionEvent ev){
final int actionPeformed = ev.getAction();
switch(actionPeformed){
case MotionEvent.ACTION_DOWN:{
break;
}
case MotionEvent.ACTION_MOVE:{
break;
}
return true;
}
}
在這些情況下,您可以執行任何您喜歡的計算。例如縮放、縮小等。為了獲取 X 和 Y 軸的座標,您可以呼叫getX()和getY()方法。其語法如下所示:
final float x = ev.getX(); final float y = ev.getY();
除了這些方法之外,此 MotionEvent 類還提供其他方法來更好地處理多點觸控。這些方法列在下面:
| 序號 | 方法及描述 |
|---|---|
| 1 |
getAction() 此方法返回正在執行的操作型別。 |
| 2 |
getPressure() 此方法返回此事件對於第一個索引的當前壓力。 |
| 3 |
getRawX() 此方法返回此事件的原始原始 X 座標。 |
| 4 |
getRawY() 此方法返回此事件的原始原始 Y 座標。 |
| 5 |
getSize() 此方法返回第一個指標索引的大小。 |
| 6 |
getSource() 此方法獲取事件的來源。 |
| 7 |
getXPrecision() 此方法返回報告的 X 座標的精度。 |
| 8 |
getYPrecision() 此方法返回報告的 Y 座標的精度。 |
示例
這是一個演示多點觸控用法的示例。它建立了一個基本的多點觸控手勢應用程式,允許您在執行多點觸控時檢視座標。
要試驗此示例,您需要在實際裝置上執行它。
| 步驟 | 描述 |
|---|---|
| 1 | 您將使用 Android Studio 在 com.example.sairamkrishna.myapplication 包下建立一個 Android 應用程式。 |
| 2 | 修改 src/MainActivity.java 檔案以新增多點觸控程式碼。 |
| 3 | 修改 res/layout/activity_main 以新增相應的 XML 元件。 |
| 4 | 執行應用程式,選擇正在執行的 Android 裝置,將應用程式安裝在其上並驗證結果。 |
以下是修改後的主活動檔案src/MainActivity.java的內容。
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
float xAxis = 0f;
float yAxis = 0f;
float lastXAxis = 0f;
float lastYAxis = 0f;
EditText ed1, ed2, ed3, ed4;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = (EditText) findViewById(R.id.editText);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
ed4 = (EditText) findViewById(R.id.editText4);
tv1=(TextView)findViewById(R.id.textView2);
tv1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int actionPeformed = event.getAction();
switch(actionPeformed){
case MotionEvent.ACTION_DOWN:{
final float x = event.getX();
final float y = event.getY();
lastXAxis = x;
lastYAxis = y;
ed1.setText(Float.toString(lastXAxis));
ed2.setText(Float.toString(lastYAxis));
break;
}
case MotionEvent.ACTION_MOVE:{
final float x = event.getX();
final float y = event.getY();
final float dx = x - lastXAxis;
final float dy = y - lastYAxis;
xAxis += dx;
yAxis += dy;
ed3.setText(Float.toString(xAxis));
ed4.setText(Float.toString(yAxis));
break;
}
}
return true;
}
});
}
}
以下是修改後的 xml 檔案res/layout/activity_main.xml的內容。
在下面的程式碼中,abc表示 tutorialspoint.com 的徽標。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:transitionGroup="true">
<TextView android:text="Multitouch example" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:theme="@style/Base.TextAppearance.AppCompat" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/imageView"
android:layout_alignRight="@+id/textview"
android:layout_alignEnd="@+id/textview"
android:hint="X-Axis"
android:layout_alignLeft="@+id/textview"
android:layout_alignStart="@+id/textview"
android:textColorHint="#ff69ff0e" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:textColorHint="#ff21ff11"
android:hint="Y-Axis"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:hint="Move X"
android:textColorHint="#ff33ff20"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText4"
android:layout_below="@+id/editText3"
android:layout_alignLeft="@+id/editText3"
android:layout_alignStart="@+id/editText3"
android:textColorHint="#ff31ff07"
android:hint="Move Y"
android:layout_alignRight="@+id/editText3"
android:layout_alignEnd="@+id/editText3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Touch here"
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:focusable="true"
android:typeface="sans"
android:clickable="true"
android:textColor="#ff5480ff"
android:textSize="35dp" />
</RelativeLayout>
以下是res/values/string.xml的內容。
<resources> <string name="app_name">My Application</string> </resources>
以下是AndroidManifest.xml檔案的內容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 Android Studio 執行應用程式,請開啟專案中的一個活動檔案,然後單擊工具欄中的執行
圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗,供您選擇要在其中執行 Android 應用程式的選項。
選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕:
預設情況下,您不會在任何欄位中看到任何內容。現在只需點選“在此處觸控”區域,然後在欄位中檢視一些資料。如下所示:
您會看到“移動”欄位中的資料為 0,因為只執行了單點觸控手勢。現在點選螢幕並開始拖動手指。您將看到“移動”欄位資料發生變化。如下所示:
