欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實(shí)現(xiàn)自定義手勢(shì)的方法詳解

 更新時(shí)間:2016年10月24日 15:05:57   作者:pku_android  
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義手勢(shì)的方法,結(jié)合實(shí)例形式分析了Android自定義手勢(shì)的功能、相關(guān)函數(shù)與具體實(shí)現(xiàn)步驟,需要的朋友可以參考下

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

之前介紹過如何在Android程序中使用手勢(shì),主要是系統(tǒng)默認(rèn)提供的幾個(gè)手勢(shì),這次介紹一下如何自定義手勢(shì),以及如何對(duì)其進(jìn)行管理。

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

一、加載手勢(shì)庫文件:

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

二、管理手勢(shì)庫:

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

接下來介紹一下如何自定義手勢(shì),Android提供了一個(gè)名為GestureOverlayView的組件專門用于繪制自定義的手勢(shì),并提供OngestureListener、OnGesturePerformedListener、OnGesturingListener三個(gè)監(jiān)聽接口,分別用于響應(yīng)手勢(shì)事件開始、結(jié)束、完成、取消等事件,一般來說,OnGesturePerformedListener是最常用的,他可用于在手勢(shì)事件完成時(shí)提供響應(yīng)。下面通過一個(gè)程序?qū)嵗f明如何自定義手勢(shì)。

用到的布局文件如下:

一、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="請(qǐng)?jiān)谙旅嫫聊簧侠L制手勢(shì)"
/>
<android.gesture.GestureOverlayView
    android:id="@+id/gesture"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    <!--該參數(shù)指定是否一筆完成,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"
          />
<!-- 定義一個(gè)文本框來讓用戶輸入手勢(shì)名 -->
<EditText
           android:id="@+id/gesture_name"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- 定義一個(gè)圖片框來顯示手勢(shì) -->
<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);
    // 設(shè)置手勢(shì)的繪制顏色
    gestureView.setGestureColor(Color.RED);
    // 設(shè)置手勢(shì)的繪制寬度
    gestureView.setGestureStrokeWidth(4);
    // 為gesture的手勢(shì)完成事件綁定事件監(jiān)聽器
    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);
          // 根據(jù)Gesture包含的手勢(shì)創(chuàng)建一個(gè)位圖
          Bitmap bitmap = gesture.toBitmap(128, 128, 10, 0xFFFF0000);
          imageView.setImageBitmap(bitmap);
          //使用對(duì)話框顯示saveDialog組件
          new AlertDialog.Builder(AddGesture.this)
            .setView(saveDialog)
            .setPositiveButton("保存", new OnClickListener()
            {
              @Override
              public void onClick(DialogInterface dialog,
                int which)
              {
                // 獲取指定文件對(duì)應(yīng)的手勢(shì)庫
                GestureLibrary gestureLib = GestureLibraries
                  .fromFile("/mnt/sdcard/mygestures");
                // 添加手勢(shì)
                gestureLib.addGesture(
gestureName.getText().toString()
                  ,gesture);
                // 保存手勢(shì)庫
                gestureLib.save();
              }
            })
            .setNegativeButton("取消", null)
            .show();
          }
      });
  }
}

運(yùn)行以上程序即可完成自定義手勢(shì)的添加,運(yùn)行結(jié)果如圖所示:

需要使用該手勢(shì)時(shí),只需要加載相應(yīng)的手勢(shì)庫,并調(diào)用前面給出的識(shí)別函數(shù)接口進(jìn)行識(shí)別即可,這里就不再詳述了,以上內(nèi)容學(xué)習(xí)自瘋狂Android一書

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

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論