国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現自定義手勢的方法詳解

2019-12-12 04:58:08
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現自定義手勢的方法。分享給大家供大家參考,具體如下:

之前介紹過如何在Android程序中使用手勢,主要是系統默認提供的幾個手勢,這次介紹一下如何自定義手勢,以及如何對其進行管理。

先介紹一下Android系統對手勢的管理,Android系統允許應用程序把用戶的手勢以文件的形式保存以前,以后要使用這些手勢只需要加載這個手勢庫文件即可,同時Android系統還提供了諸如手勢識別、查找及刪除等的函數接口,具體如下:

一、加載手勢庫文件:

staticGestureLibrary fromFile(String path);
staticGestureLibrary fromFile(File path);
staticGestureLibrary fromPrivateFile(Context context, String name);//從指定應用程序的數據文件夾中name文件加載手勢庫
staticGestureLibrary fromRawResource(Context context, int resourceId);

二、管理手勢庫:

voidaddGesture(String entryName, Gesture gesture);//添加一個名為entryName的手勢Set<String>getGestureEntries();//獲取該手勢庫中的所有手勢的名稱ArrayList<Gesture>getGestures(String entryName);//獲取entryName名稱對應的全部手勢ArrayList<Prediction>recognize(Gesture gesture);//從當前手勢庫中識別與gesture匹配的全部手勢voidremoveEntry(String entryName);//刪除手勢庫中entryName對應的手勢voidremoveGesture(String entryName, Gesture gesture);//刪除手勢庫中entryName、gesture對應的手勢booleansave();//手勢庫文件有改動后調用該方法保存手勢庫

接下來介紹一下如何自定義手勢,Android提供了一個名為GestureOverlayView的組件專門用于繪制自定義的手勢,并提供OngestureListener、OnGesturePerformedListener、OnGesturingListener三個監聽接口,分別用于響應手勢事件開始、結束、完成、取消等事件,一般來說,OnGesturePerformedListener是最常用的,他可用于在手勢事件完成時提供響應。下面通過一個程序實例說明如何自定義手勢。

用到的布局文件如下:

一、main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:gravity="center_horizontal"  ><TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"  android:text="請在下面屏幕上繪制手勢"/><android.gesture.GestureOverlayView    android:id="@+id/gesture"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    <!--該參數指定是否一筆完成,single為一筆完成-->android:gestureStrokeType="multiple"/></LinearLayout>

二、save.xml:

<?xmlversionxmlversion="1.0" encoding="utf-8"?><LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"><LinearLayout       android:orientation="horizontal"       android:layout_width="fill_parent"       android:layout_height="wrap_content"><TextView           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_marginRight="8dip"           android:text="@string/gesture_name"          /><!-- 定義一個文本框來讓用戶輸入手勢名 --><EditText           android:id="@+id/gesture_name"           android:layout_width="fill_parent"           android:layout_height="wrap_content"/></LinearLayout><!-- 定義一個圖片框來顯示手勢 --><ImageView       android:id="@+id/show"       android:layout_width="128dp"       android:layout_height="128dp"       android:layout_marginTop="10dp"/></LinearLayout>

Java代碼如下:

public class AddGesture extends Activity{  EditText editText;  GestureOverlayView gestureView;  @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    editText = (EditText) findViewById(R.id.gesture_name);    gestureView = (GestureOverlayView) findViewById(R.id.gesture);    // 設置手勢的繪制顏色    gestureView.setGestureColor(Color.RED);    // 設置手勢的繪制寬度    gestureView.setGestureStrokeWidth(4);    // 為gesture的手勢完成事件綁定事件監聽器    gestureView.addOnGesturePerformedListener(      new OnGesturePerformedListener()      {        @Override        public void onGesturePerformed(GestureOverlayView overlay          ,final Gesture gesture)        {          //加載save.xml界面布局代表的視圖          View saveDialog = getLayoutInflater().inflate(            R.layout.save, null);          // 獲取saveDialog里的show組件          ImageView imageView = (ImageView) saveDialog            .findViewById(R.id.show);          // 獲取saveDialog里的gesture_name組件          final EditText gestureName = (EditText) saveDialog            .findViewById(R.id.gesture_name);          // 根據Gesture包含的手勢創建一個位圖          Bitmap bitmap = gesture.toBitmap(128, 128, 10, 0xFFFF0000);          imageView.setImageBitmap(bitmap);          //使用對話框顯示saveDialog組件          new AlertDialog.Builder(AddGesture.this)            .setView(saveDialog)            .setPositiveButton("保存", new OnClickListener()            {              @Override              public void onClick(DialogInterface dialog,                int which)              {                // 獲取指定文件對應的手勢庫                GestureLibrary gestureLib = GestureLibraries                  .fromFile("/mnt/sdcard/mygestures");                // 添加手勢                gestureLib.addGesture(gestureName.getText().toString()                  ,gesture);                // 保存手勢庫                gestureLib.save();              }            })            .setNegativeButton("取消", null)            .show();          }      });  }}

運行以上程序即可完成自定義手勢的添加,運行結果如圖所示:

需要使用該手勢時,只需要加載相應的手勢庫,并調用前面給出的識別函數接口進行識別即可,這里就不再詳述了,以上內容學習自瘋狂Android一書

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android滾動條與滾動操作技巧總結》、《Android圖形與圖像處理技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄄城县| 嘉荫县| 阜新| 大同县| 泾源县| 巴楚县| 永嘉县| 巴楚县| 茌平县| 如东县| 湟源县| 涟水县| 乃东县| 铜鼓县| 仁布县| 绥化市| 射洪县| 宜兰市| 高雄市| 肇源县| 黎川县| 青冈县| 海原县| 重庆市| 陇川县| 安顺市| 绥宁县| 永兴县| 河西区| 红河县| 广水市| 饶河县| 息烽县| 潍坊市| 乌海市| 宜兰市| 灌南县| 通渭县| 河北省| 新乐市| 潼南县|