liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのIO - GestureLibrary

GestureLibraryを使い新しいジェスチャーを保存し、次回検知することができる。

GestureLibraryのロードメソッド

  • static GestureLibrary fromFile(String path)
  • static GestureLibrary fromFile(File path)
  • static GestureLibrary fromPrivateFile(Context context, String name)
  • static GestureLibrary fromRawResource(Context context, int resourceId)

GestureLibraryのメソッド

  • void addGesture(String entryName, Gesture gesture)
  • void removeEntry(String entryName)
  • void removeGesture(String entryName, Gesture gesture)

  • boolean save()

  • Set getGestureEntries()

  • ArrayList getGestures(String entryname)
  • ArrayList recognize(Gesture gesture)

GestureOverlayViewはジェスチャー編集コンポーネントである。

GestureOverlayViewのリスナー

  • OnGestureListner
  • OnGesturePerformedListener
  • OnGesturingListener

サンプルコード
①MainActivity.xml

<?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:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView android:text="ジェスチャー登録" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.gesture.GestureOverlayView
        android:id="@+id/addGesture"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:gestureStrokeType="single"
        android:background="#c8faf8"
        />
    <TextView android:text="ジェスチャー読取" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <android.gesture.GestureOverlayView
        android:id="@+id/readGesture"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:gestureStrokeType="single"
        android:background="#d8faa7"
        />
</LinearLayout>

②save.xml(ジェスチャー保存確認用)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dip"
    android:text="ジェスチャー名" />
<!-- ジェスチャー名 -->
<EditText
    android:id="@+id/gesture_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>    
</LinearLayout>
<!-- ジェスチャーイメージ -->
<ImageView
    android:id="@+id/show"
    android:layout_width="128dp"
    android:layout_height="128dp"
    android:layout_marginTop="10dp" />
</LinearLayout>

③ MainActivity.java

package com.example.liguofeng.gesturelibrarysample;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.RemoteControlClient;
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 java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    GestureOverlayView addGestureOverlayView;
    GestureOverlayView readGestureOverlayView;
    GestureLibrary gestureLibrary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // GestureLibrary取得
        gestureLibrary = getGestureLibrary();
        // ジェスチャー登録設定
        setAddGesture();
        // ジェスチャー読取設定
        setReadGesture();
    }

    // GestureLibrary取得
    private GestureLibrary getGestureLibrary() {
        return GestureLibraries.fromPrivateFile(MainActivity.this, "xxxx.gusture");
    }
    // ジェスチャー登録設定
    private void setAddGesture() {
        // ジェスチャー登録
        addGestureOverlayView = (GestureOverlayView) findViewById(R.id.addGesture);
        // ジェスチャー色
        addGestureOverlayView.setGestureColor(Color.RED);
        // ジェスチャー幅
        addGestureOverlayView.setGestureStrokeWidth(5);
        // ジェスチャーリスナー
        addGestureOverlayView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
                // View取得
                View saveDialog = getLayoutInflater().inflate(R.layout.save, null);
                // ImageView取得
                ImageView imageView = (ImageView) saveDialog.findViewById(R.id.show);
                // EditText取得
                final EditText gestureName = (EditText) saveDialog.findViewById(R.id.gesture_name);
                // ジェスチャーイメージ
                Bitmap bitmap = gesture.toBitmap(128, 128, 10, 0xffff0000);
                imageView.setImageBitmap(bitmap);

                // ダイアログ表示
                new AlertDialog.Builder(MainActivity.this)
                        .setView(saveDialog)
                        .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // GestureLibrary取得
                                // GestureLibrary gestureLibrary = getGestureLibrary();
                                // ジェスチャー保存
                                gestureLibrary.addGesture(gestureName.getText().toString(), gesture);
                                // 保存
                                gestureLibrary.save();
                                Toast.makeText(MainActivity.this, "登録完了", Toast.LENGTH_SHORT).show();
                            }
                        }).setNegativeButton("取消", null)
                        .show();
            }
        });
    }

    // ジェスチャー読取
    private void setReadGesture() {
        // ジェスチャー登録
        readGestureOverlayView = (GestureOverlayView) findViewById(R.id.readGesture);
        // ジェスチャー色
        readGestureOverlayView.setGestureColor(Color.BLUE);
        // ジェスチャー幅
        readGestureOverlayView.setGestureStrokeWidth(5);
        // ジェスチャーリスナー
        readGestureOverlayView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
                List<Prediction> predictions = gestureLibrary.recognize(gesture);
                int cnt = 0;
                for (Prediction p : predictions) {
                    if (p.score > 2.0) {
                        Toast.makeText(MainActivity.this,
                                "ジェスチャー名 :" + p.name +
                                        "\nジェスチャー類似度 :" + p.score,
                                Toast.LENGTH_SHORT).show();
                        cnt++;
                    }
                }
                if (cnt == 0) {
                    Toast.makeText(MainActivity.this,
                            "検出できませんでした",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

f:id:liguofeng29:20151231123207g:plain