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

Android實現(xiàn)自定義手勢和識別手勢的功能

 更新時間:2019年10月11日 15:00:16   作者:季風__  
這篇文章主要介紹了Android實現(xiàn)自定義手勢和識別手勢的功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

1. 先完成自定義手勢的Activity

1.1 因為需要存儲手勢文件所以需要聲明權限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> //讀取SD卡權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //寫入SD卡權限

1.2 簡單寫一個布局文件,其中用到了GestureOverlayView,相當于一個繪制組件。其中有一個重要屬性gestureStrokeType,值為single時表示只繪制一筆,若要多筆繪制值應該設為multiple:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".addgesture.Main3Activity">
 <Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:onClick="recognition"
  android:text="識別手勢" />
 <TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="請繪制手勢" />
 <android.gesture.GestureOverlayView
  android:id="@+id/activity_main3_gov"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gestureStrokeType="multiple" //多筆繪制
  ></android.gesture.GestureOverlayView>
</LinearLayout>

1.3 這里自定義了AlertDialog的樣式;

<?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">
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TextView
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:gravity="center"
   android:text="請輸入手勢名稱" />
  <EditText  //輸入手勢的名稱
   android:id="@+id/save_dialog_et"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
 </LinearLayout>
 <ImageView  //展示繪制的手勢
  android:id="@+id/save_dialog_iv"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
</LinearLayout>

1.4 代碼部分:

package com.example.mygesture.addgesture;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.mygesture.R;
import com.example.mygesture.recognitiongesture.Main4Activity;
public class Main3Activity extends AppCompatActivity {
 GestureOverlayView gov;   //定義繪制組件
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main3);
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
   ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);  
  }    //高版本需要動態(tài)申請權限
  init();
 }
 private void init() {
  gov = findViewById(R.id.activity_main3_gov);
//  gov.setGestureColor(Color.RED);  //設置繪制的顏色
  gov.setGestureStrokeWidth(4);  //設置畫筆的寬度
  gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { //設置繪制完成監(jiān)聽
   @Override
   public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
    View saveDialog = getLayoutInflater().inflate(R.layout.save_dialog, null); //獲取AlertDialog的布局樣式
    final EditText editText = saveDialog.findViewById(R.id.save_dialog_et);
    ImageView imageView = saveDialog.findViewById(R.id.save_dialog_iv);
    Bitmap bitmap = gesture.toBitmap(128, 128, 10, 0xFFFF0000);  //將手勢轉換為位圖
    imageView.setImageBitmap(bitmap);   //用ImageView加載手勢圖片
    new AlertDialog.Builder(Main3Activity.this).setView(saveDialog).setPositiveButton("確定", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      GestureLibrary gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture");//利用手勢庫獲取存放手勢文件的地址
      gestureLibrary.addGesture(editText.getText().toString(), gesture);  //向手勢庫中添加手勢名稱和手勢
      gestureLibrary.save();    //保存手勢庫
      Toast.makeText(Main3Activity.this, "保存成功", Toast.LENGTH_SHORT).show();
     }
    }).setNegativeButton("取消", null)
      .show();
   }
  });
 }
 public void recognition(View view) {
  Intent intent = new Intent(this, Main4Activity.class);
  startActivity(intent);
 }
}

2. 接下來完成識別手勢的Activity:

2.1 一樣的先寫布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".recognitiongesture.Main4Activity">

 <TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="請繪制需要識別的手勢" />

 <android.gesture.GestureOverlayView
  android:id="@+id/activity_main4_gov"
  android:layout_width="match_parent"
  android:layout_height="match_parent"></android.gesture.GestureOverlayView>
</LinearLayout>

2.2 代碼的編寫

package com.example.mygesture.recognitiongesture;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.example.mygesture.R;
import java.util.ArrayList;
import java.util.logging.Level;
public class Main4Activity extends AppCompatActivity {
 GestureOverlayView gov;
 GestureLibrary gestureLibrary;  //定義手勢庫
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main4);
  init();
 }
 private void init() {
  gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/mygesture"); //獲取手勢文件
  if (gestureLibrary.load()) {   //判斷手勢文件是否存在以及加載
   Toast.makeText(this, "手勢文件加載成功", Toast.LENGTH_SHORT).show();
  } else {
   Toast.makeText(this, "手勢文件加載失敗", Toast.LENGTH_SHORT).show();
  }
  gov = findViewById(R.id.activity_main4_gov);
  gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
   @Override
   public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture); //匹配手勢庫中的所有手勢
    ArrayList<String> result = new ArrayList<>();  //匹配結果數(shù)組
    for (Prediction pred : predictions) {
     if (pred.score > 2) {    //匹配手勢庫中的所有手勢,并將相似度>2存入匹配結果數(shù)組
      result.add("相似度:" + pred.score);
     }
    }
    if (result.size() > 0) {  //這里用了適配器來作為AlertDialog的布局樣式,用于顯示所有手勢的相似度
     ArrayAdapter<Object> arrayAdapter = new ArrayAdapter<Object>(Main4Activity.this, android.R.layout.simple_dropdown_item_1line, result.toArray());
     new AlertDialog.Builder(Main4Activity.this).setAdapter(arrayAdapter, null).setPositiveButton("確定", null).show();
    } else {
     Toast.makeText(Main4Activity.this, "未找到與之匹配的手勢", Toast.LENGTH_SHORT).show();
    }
   }
  });
 }
}

總結

以上所述是小編給大家介紹的Android實現(xiàn)自定義手勢和識別手勢的功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

相關文章

  • Android開發(fā)新手必須知道的10大嚴重錯誤

    Android開發(fā)新手必須知道的10大嚴重錯誤

    這篇文章主要介紹了Android開發(fā)新手必須知道的10大嚴重錯誤,總結分析了Android開發(fā)中幫助文件、開發(fā)工具、社區(qū)等的重要性以及重要的開發(fā)原則,需要的朋友可以參考下
    2016-01-01
  • Android使用ListView批量刪除item的方法

    Android使用ListView批量刪除item的方法

    這篇文章主要介紹了Android使用ListView批量刪除item的方法,實例分析了Android中ListView控件的相關操作技巧,需要的朋友可以參考下
    2016-07-07
  • Android實現(xiàn)側滑只需一步

    Android實現(xiàn)側滑只需一步

    這篇文章主要介紹了Android實現(xiàn)側滑只需一步,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • Android EditText限制輸入字符類型的方法總結

    Android EditText限制輸入字符類型的方法總結

    這篇文章主要介紹了Android EditText限制輸入字符類型的方法總結的相關資料,需要的朋友可以參考下
    2017-03-03
  • Android開發(fā)中TextView各種常見使用方法小結

    Android開發(fā)中TextView各種常見使用方法小結

    這篇文章主要介紹了Android開發(fā)中TextView各種常見使用方法,結合實例形式總結分析了Android開發(fā)中TextView各種常見布局與功能實現(xiàn)技巧,需要的朋友可以參考下
    2019-04-04
  • Android自定義控件(實現(xiàn)狀態(tài)提示圖表)

    Android自定義控件(實現(xiàn)狀態(tài)提示圖表)

    本篇文章主要介紹了android實現(xiàn)狀態(tài)提示圖表的功能,實現(xiàn)了動態(tài)圖表的顯示,有需要的朋友可以了解一下。
    2016-11-11
  • Android開發(fā)實現(xiàn)在TextView前面加標簽示例

    Android開發(fā)實現(xiàn)在TextView前面加標簽示例

    這篇文章主要為大家介紹了Android開發(fā)實現(xiàn)TextView前面加標簽示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • Android XML數(shù)據(jù)的三種解析方式

    Android XML數(shù)據(jù)的三種解析方式

    這篇文章主要為大家詳細介紹了Android XML數(shù)據(jù)的三種解析方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Kotlin類對象class初始化與使用

    Kotlin類對象class初始化與使用

    Kotlin 是一種追求簡潔的語言,在類上也下了不少功夫,放棄了很多c++ 中類非常復雜的概念,其實對于類可以這樣來理解,為了復用的方便性和完整性,我們把變量和函數(shù)組合在一起,形成了類的概念
    2022-12-12
  • Android編程之文件讀寫操作與技巧總結【經(jīng)典收藏】

    Android編程之文件讀寫操作與技巧總結【經(jīng)典收藏】

    這篇文章主要介紹了Android編程之文件讀寫操作與技巧,結合實例形式總結分析了Android常見的文件與目錄的讀寫操作,及相關函數(shù)的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06

最新評論