Android手势库

<?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:layout_weight="1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <ImageView
            android:id="@+id/my_gesture"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <ImageView
            android:id="@+id/old_gesture"<--!找到手势库中的对应手势显示在这个ImageView上-->
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

    </LinearLayout>

    <android.gesture.GestureOverlayView
        android:background="#191919"
        android:id="@+id/draw_gestrue"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gestureStrokeType="multiple"
        android:gestureStrokeWidth="5"
        android:gestureColor="#ffb200"
        android:fadeOffset="3000"
        />
</LinearLayout>

下面是一个可以书写的文本区域,调用的是Android中的gestureOverlayView



/**
 * 手势库
 */
public class MainActivity extends AppCompatActivity {

    private ImageView mShowImage,mOldImage;
    private GestureOverlayView mGestureDraw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
//        GestureLibraries.fromFile();
        mShowImage = (ImageView) findViewById(R.id.my_gesture);
        mOldImage = (ImageView) findViewById(R.id.old_gesture);
        mGestureDraw = (GestureOverlayView) findViewById(R.id.draw_gestrue);
        mGestureDraw.addOnGesturePerformedListener(
        new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                GestureLibrary library = GestureLibraries.fromFile(Environment.
                getExternalStorageDirectory().getAbsolutePath() + "/gesture");

                if(library.load()){
                    Log.i("gesture","加载");
                }else{
                    Log.i("gesture","未加载");
                }
                Bitmap bitmap = gesture.toBitmap(200, 200, 10, Color.RED);
                mShowImage.setImageBitmap(bitmap);
                //将我们绘制的手势 与 手势库中 存储的手势进行对比 查询出 最相近的手势
                ArrayList<Prediction> recognize = library.recognize(gesture);
                if(recognize.size()>0){
                    Toast.makeText(MainActivity3.this,
                    "找到手势"+recognize.size(),Toast.LENGTH_SHORT).show();
                    for (Prediction prediction:recognize ) {
                        if( prediction.score>5.0f){//相识度的判断
//                            ArrayList<Gesture> gestures = library.getGestures(prediction.name);
//                            Bitmap bitmap1 = gestures.get(0).toBitmap(200, 200, 10, Color.RED);
//                            mOldImage.setImageBitmap(bitmap1);
                            Toast.makeText(MainActivity.this,
                            "手势相似度"+ prediction.score,Toast.LENGTH_SHORT).show();
                        }
                    }
                }else{
                    Toast.makeText(MainActivity.this,
                    "没找手势"+recognize.size(),Toast.LENGTH_SHORT).show();
                }
                //手势添加到手势类库
                library.addGesture("gesture", gesture);
                //保存手势
                library.save();
            }
        });
    }
}



/**
 * 演示了手势的方法 手势方法的调用时机
 */
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener{

    private GestureDetector mGesture;

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

        /**
         * 1、创建一个手势
         */
        mGesture = new GestureDetector(this,this);
    }

    /**
     * 2、将我们的Touch事件 传递给我们的手势
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGesture.onTouchEvent(event);
    }

    /**
     * 摁下
     * @param e
     * @return
     */
    @Override
    public boolean onDown(MotionEvent e) {
        Log.i("gesture","onDown");
        return false;
    }

    /**
     * 按下未移动
     * @param e
     */
    @Override
    public void onShowPress(MotionEvent e) {
        Log.i("gesture","onShowPress");
    }

    /**
     * 轻轻点
     * @param e
     * @return
     */
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.i("gesture","onSingleTapUp");
        return false;
    }

    /**
     * 滑动
     * @param e1        起点 并且这个起点 不移动
     * @param e2        结束点 这点是变化的
     * @param distanceX     e2 到 上一个e2的X距离
     * @param distanceY     e2 到 上一个e2的Y距离
     * @return
     */
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.i("gesture","onScroll e1:X "+e1.getX()+
        " :Y "+e1.getY()+"   e2:X "+e2.getX()+
        " :Y "+e2.getY()+" distanceX:"+distanceX +
         " distanceY:"+distanceY);
        return false;
    }

    /**
     * 长摁
     * @param e
     */
    @Override
    public void onLongPress(MotionEvent e) {
        Log.i("gesture","onLongPress");
    }

    /**
     * 快速滑动
     * @param e1    摁下位置
     * @param e2    结束位置
     * @param velocityX     e1 到 e2 的X速率
     * @param velocityY     e1 到 e2 的Y速率
     * @return
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.i("gesture","onFling e1:X "+e1.getX()+
        " :Y "+e1.getY()+"   e2:X "+e2.getX()+
        " :Y "+e2.getY()+" velocityX:"+velocityX + 
        " velocityY:"+velocityY);
        return false;
    }
}


你可能感兴趣的:(手势识别,Touch事件,手势库)