
- 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 - Shared Preferences
- 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 提供了 `GestureDetector` 類來接收運動事件並告訴我們這些事件是否對應於手勢。要使用它,您需要建立一個 `GestureDetector` 物件,然後使用 **`GestureDetector.SimpleOnGestureListener`** 擴充套件另一個類作為偵聽器並覆蓋一些方法。其語法如下:
GestureDetector myG; myG = new GestureDetector(this,new Gesture()); class Gesture extends GestureDetector.SimpleOnGestureListener{ public boolean onSingleTapUp(MotionEvent ev) { } public void onLongPress(MotionEvent ev) { } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { } }
處理捏合手勢
Android 提供了 `ScaleGestureDetector` 類來處理捏合等手勢。為了使用它,您需要例項化此類的一個物件。其語法如下:
ScaleGestureDetector SGD; SGD = new ScaleGestureDetector(this,new ScaleListener());
第一個引數是上下文,第二個引數是事件偵聽器。我們必須定義事件偵聽器並覆蓋 `OnTouchEvent` 函式才能使其工作。其語法如下:
public boolean onTouchEvent(MotionEvent ev) { SGD.onTouchEvent(ev); return true; } private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { float scale = detector.getScaleFactor(); return true; } }
除了捏合手勢外,還有其他可用的方法可以提供更多關於觸控事件的資訊。它們列在下面:
序號 | 方法和描述 |
---|---|
1 |
getEventTime() 此方法獲取正在處理的當前事件的事件時間。 |
2 |
getFocusX() 此方法獲取當前手勢焦點點的 X 座標。 |
3 |
getFocusY() 此方法獲取當前手勢焦點點的 Y 座標。 |
4 |
getTimeDelta() 此方法返回前一個已接受的縮放事件與當前縮放事件之間的時間差(以毫秒為單位)。 |
5 |
isInProgress() 如果縮放手勢正在進行中,此方法返回 true。 |
6 |
onTouchEvent(MotionEvent event) 此方法接受 MotionEvents 並在適當的時候分派事件。 |
示例
這是一個演示 `ScaleGestureDetector` 類用法的示例。它建立了一個基本的應用程式,允許您透過捏合來放大和縮小。
要試驗此示例,您可以在啟用觸控式螢幕的實際裝置或模擬器上執行它。
步驟 | 描述 |
---|---|
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.graphics.Matrix; import android.os.Bundle; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private Matrix matrix = new Matrix(); private float scale = 1f; private ScaleGestureDetector SGD; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv=(ImageView)findViewById(R.id.imageView); SGD = new ScaleGestureDetector(this,new ScaleListener()); } public boolean onTouchEvent(MotionEvent ev) { SGD.onTouchEvent(ev); return true; } private class ScaleListener extends ScaleGestureDetector. SimpleOnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { scale *= detector.getScaleFactor(); scale = Math.max(0.1f, Math.min(scale, 5.0f)); matrix.setScale(scale, scale); iv.setImageMatrix(matrix); return true; } } }
以下是修改後的 xml 檔案 `res/layout/activity_main.xml` 的內容。
這裡 abc 表示 tutorialspoint 的徽標
<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" > <TextView android:text="Gestures 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:scaleType="matrix" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </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="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sairamkrishna.myapplicationMainActivity" 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 螢幕上,然後將它們分開,您就會看到 Android 圖片正在縮放。如下面的圖片所示:

現在再次將兩根手指放在 Android 螢幕上,然後嘗試將它們合攏,您就會看到 Android 圖片現在正在縮小。如下面的圖片所示:
