Android手勢操作(二)

來源:互聯網
上載者:User

mainActivity如下:

package cn.c;import java.io.File;import java.util.ArrayList;import android.app.Activity;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.os.Bundle;import android.os.Environment;import android.view.KeyEvent;import android.view.View;import android.view.View.OnKeyListener;import android.widget.Button;import android.widget.EditText;/** * 需求描述: * 在例子1的基礎上增加手勢識別. * 所以不用再寫添加手勢的步驟. * 錯誤總結: * 在手勢識別的時候沒有執行mGestureLibrary.load() * 導致mGestureLibrary.recognize()方法失敗. * 該錯誤很類似於剛開始操作 SharedPreferences的時候 * Editor沒有執行commit()方法 * */public class MainActivity extends Activity {    private EditText mEditText;    private GestureOverlayView mGestureOverlayView;    private GestureLibrary mGestureLibrary;    private Gesture mGesture;    private Button mSaveButton;    private Button mResetButton;    private String mGestureLibraryPath;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        init();    }private void init() {mEditText = (EditText) findViewById(R.id.editText);mEditText.setOnKeyListener(new OnKeyListener() {public boolean onKey(View v, int keyCode, KeyEvent event) {if (mGesture!=null&&mEditText.getText().toString().length()>0) {mSaveButton.setEnabled(true);} else {mSaveButton.setEnabled(false);}return false;}});mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView);mSaveButton = (Button) findViewById(R.id.saveButton);mSaveButton.setEnabled(false);mResetButton = (Button) findViewById(R.id.resetButton);//GestureLibrary檔案的路徑mGestureLibraryPath =Environment.getExternalStorageDirectory()+File.separator+"gesturesTest";//為GestureOverlayView添加監聽事件mGestureOverlayView.addOnGesturePerformedListener(new GesturePerformedListenerImpl());}private class GesturePerformedListenerImpl implements OnGesturePerformedListener{public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {// 載入該路徑下的手勢庫.// 若不存在,則會在路徑下建立一個手勢庫mGestureLibraryPath =Environment.getExternalStorageDirectory()+File.separator+"gesturesTest";mGestureLibrary = GestureLibraries.fromFile(mGestureLibraryPath);//載入手勢庫//一定要執行load()方法,否則recognize()沒有用!if (mGestureLibrary.load()) {ArrayList<Prediction> predictionsList=mGestureLibrary.recognize(gesture);if (predictionsList.size()>0) {Prediction bestPrediction=predictionsList.get(0);if (bestPrediction.score>7.0) { System.out.println("xxxx1111 該手勢已經存在");}} else {              System.out.println("xxxx1111 該手勢不存在");}} else {System.out.println("xxxx1111 載入手勢庫失敗");}}}}

main.xml如下:

<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" >    <LinearLayout        android:id="@+id/linearLayout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:orientation="horizontal" >        <TextView            android:id="@+id/textView"            android:layout_width="100dip"            android:layout_height="wrap_content"            android:text="手勢的名稱:" />        <EditText            android:id="@+id/editText"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:hint="在此輸入" />    </LinearLayout> <android.gesture.GestureOverlayView     android:id="@+id/gestureOverlayView"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_below="@id/linearLayout"     android:layout_marginBottom="50dip"     android:gestureStrokeType="multiple"  />  <LinearLayout        android:layout_width="fill_parent"        android:layout_height="50dip"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >    <Button         android:id="@+id/saveButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"         android:text="儲存"        />     <Button         android:id="@+id/resetButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"         android:text="重設"        />   </LinearLayout> </RelativeLayout>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.