Android成長之路-手勢識別的實現

來源:互聯網
上載者:User
文章目錄
  • 手勢識別系統:
  • 先把手勢庫放到項目中:(建立手勢庫見下一篇部落格)
  • 在res檔案夾下建立一個名為raw的檔案夾,然後把手勢庫放進去
  •  
  • 然後開始項目的建立:
  •  
  • strings.xml:
手勢識別系統:先把手勢庫放到項目中:(建立手勢庫見下一篇部落格)在res檔案夾下建立一個名為raw的檔案夾,然後把手勢庫放進去 然後開始項目的建立: strings.xml:
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">GestureTest</string>    <string name="notrecognize">沒有手勢</string>    <string name="noprediction">手勢識別率太低,請重新輸入</string>    <string name="noloading">手勢庫沒有載入成功</string></resources>

 

 

main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >      <android.gesture.GestureOverlayView         android:id="@+id/myGesture"        android:layout_width="fill_parent"        android:layout_height="0dip"        android:layout_weight="1.0"        /></LinearLayout>

 

GestureTestActivity.java:

 

package cn.csdn.gesture;import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.gesture.Gesture;import android.gesture.GestureLibraries;import android.gesture.GestureLibrary;import android.gesture.GestureOverlayView;import android.gesture.GestureOverlayView.OnGesturePerformedListener;import android.gesture.Prediction;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.widget.Toast;public class GestureTestActivity extends Activity {GestureOverlayView gestureView;GestureLibrary gLibrary;boolean loadState;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}  private void init() {gestureView = (GestureOverlayView) this.findViewById(R.id.myGesture);gestureView.addOnGesturePerformedListener(new MyOnGesturePerformedListener());// 建立首飾庫對象GestureLibrarygLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);// 載入手勢庫資源loadState = gLibrary.load();}private final class MyOnGesturePerformedListener implementsOnGesturePerformedListener {public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {if (loadState) {//載入手勢資源成功// 擷取畫的圖形進行匹配,匹配程度就是Prediction中的scoreArrayList<Prediction> predictions = gLibrary.recognize(gesture);if (!predictions.isEmpty()) {// 如果使用者畫了圖形,就會匹配Prediction prediction = predictions.get(0);Log.i("TAG", String.valueOf(prediction.score));if (prediction.score > 5) {// 判斷相似性大於1,與裡面的兩者進行匹配if ("close".equals(prediction.name)) {//關閉finish();} else if ("dialto".equals(prediction.name)) {//打電話Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:11111111111"));startActivity(intent);}} else {// 相似性小於1,不識別showToast(R.string.noprediction);}} else {//沒有畫圖形showToast(R.string.notrecognize);}} else {showToast(R.string.noloading);}}}private void showToast(int tesId) {Toast.makeText(this, tesId, Toast.LENGTH_LONG).show();}}

 

 

 

:(必須畫的比較精確)

 

 

如果畫c形狀的話,會退出這個程式

 

 

如果畫一個對鉤的話,會去進行撥號的操作

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.