アンドロイドジェスチャー認識開発の概要

3997 ワード

ジェスチャー認識はアンドロイド開発で使われているシーンは多くありませんが、時にはあなたの製品をもっと便利にして、もっと面白くすることができます.例えば、ログイン時にパスワードを繰り返し入力する必要はなく、アリペイクライアントのように九宮格ジェスチャーでロックを解除するだけです.簡単なジェスチャー認識の開発を完了します.
まずジェスチャーの動作を説明するファイルが必要です.シミュレータにGesturesBuilderというプログラムをプリインストールします.このプログラムはあなたに自分のジェスチャーを作成させるものです(GesturesBuilderのソースコードはsdkでsamplesに聞いて、興味があれば見てください).作成したジェスチャーは/sdcard/gesturesに保存されます.
自分のジェスチャーライブラリを作成すると、ジェスチャー認識を実現することができます.新しいプロジェクトを作成し、上のジェスチャーライブラリファイルをあなたのプロジェクト/res/rawにコピーすると、あなたのプロジェクトでこれらのジェスチャーを使用することができます./res/rawにコピーしたジェスチャーは読み取り専用です.つまり、ジェスチャーを修正したり追加したりすることはできません.
レイアウトxmlファイルは次のようになります.
<android.gesture.GestureOverlayView
	    android:id="@+id/gestures"
	    android:layout_width="fill_parent"
	    android:layout_height="0dp"
	    android:layout_weight="1"
	    android:gestureStrokeType="multiple"
	    />
	<Button
	android:layout_weight="0"
	 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/recognize"
    android:onClick="find"
    />

MainActivityの主なコード:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        library = GestureLibraries.fromRawResource(this, R.raw.gestures);
        library.load();
        
        overlayView = (GestureOverlayView) this.findViewById(R.id.gestures);
        //       :overlayView.addOnGesturePerformedListener(new GesturePerformedListener());
        overlayView.addOnGestureListener(new GestureListener());
    }
    
    public void find(View v){
    	recognize(mgesture);
    	overlayView.clear(true);
    }
    
    private final class GestureListener implements OnGestureListener{
		public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
			
		}
		public void onGesture(GestureOverlayView overlay, MotionEvent event) {
			
		}
		public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
			mgesture = overlay.getGesture();
		}
		public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
		}
    }
    
    private final class GesturePerformedListener implements OnGesturePerformedListener{
		public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
			recognize(gesture);
		}		
    }
    
    private void recognize(Gesture gesture) {
		ArrayList<Prediction> predictions = library.recognize(gesture);
		if(!predictions.isEmpty()){
			Prediction prediction = predictions.get(0);
                        //6  60%  
			if(prediction.score >= 6){
                          //call close                 
				if("call".equals(prediction.name)){
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:1350505050"));
					startActivity(intent);
				}else if("close".equals(prediction.name)){
					finish();//  Activity
				}
			}else{
				Toast.makeText(getApplicationContext(), R.string.low, 1).show();
			}
		}else{
			Toast.makeText(getApplicationContext(), R.string.notfind, 1).show();
		}
	}
    
	@Override
	protected void onDestroy() {
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());//    
	}

ここでは2つのジェスチャー動作を定義しており、ジェスチャーがジェスチャーライブラリのcallのようになると電話をかけ、ジェスチャーライブラリのcloseのようになるとプログラムを終了します.OK、ジェスチャー認識の開発はこのように簡単で、ジェスチャーのロック解除を実現するなど、この基礎の上で複雑なことをすることができます.考え方は簡単で、GesturesBuilderのソースコードを修正して自分のプロジェクトに入れることができ、ユーザーは自分でジェスチャー動作を定義してジェスチャーライブラリに保存することができ、ユーザーはジェスチャーで検証することができます.