Android手势检测

时间:2011-12-15 22:20:41

标签: android gesture

我正在尝试开发一个基本上用于绘制shape的Android应用程序。我希望用户在屏幕上做出手势,并且应该在屏幕上绘制与手势更紧密匹配的形状。 在我的应用程序中,我可以检测屏幕上正在执行的手势,如圆形,线形,矩形等。但是我的代码存在一些问题。它实际上检测到手势并绘制相应的形状,但它只发生一次。

例如。如果我在屏幕上绘制线条,那么在我的视图上绘制线条,但之后如果我绘制圆形或矩形等,则会识别手势,但不会在那里绘制形状。

以下是该

的完整代码
package com.pck.ShapeMaker;

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.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;

public class GestureDetection extends Activity {
    private GestureLibrary gLib;    
    private LinearLayout l1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gesture);
        l1 = (LinearLayout) findViewById(R.id.playArea);

        gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (!gLib.load()) 
            finish();   
        GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures);
        myGestureHandler handler = new myGestureHandler();      
        gesturesView.addOnGesturePerformedListener(handler);


    }

    class myGestureHandler implements OnGesturePerformedListener{

        public void onGesturePerformed(GestureOverlayView gestureView, Gesture gesture){
            ArrayList<Prediction> predictions = gLib.recognize(gesture);        


            if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
                String action = predictions.get(0).name;
                if ("l".equals(action)) {
                    Line line = new Line(getApplicationContext(),20,230,200,230);
                    l1.addView(line);
                } else if ("r".equals(action)) {
                    Rectangle rect = new Rectangle(getApplicationContext());
                    l1.addView(rect);
                    Toast.makeText(getApplicationContext(), "rect", Toast.LENGTH_SHORT).show();
                } else if ("t".equals(action)) {
                    Triangle tri = new Triangle(getApplicationContext(), 300,300,250, 350, 350, 350);
                    l1.addView(tri);
                    Toast.makeText(getApplicationContext(), "trianlge", Toast.LENGTH_SHORT).show();
                }else if ("c".equals(action)) {
                    Circle c1 = new Circle(getApplicationContext(),50,50,30);
                    l1.addView(c1);
                    Toast.makeText(getApplicationContext(), "circle", Toast.LENGTH_SHORT).show();
                }else if ("d".equals(action)) {
                     Toast.makeText(getApplicationContext(), "diamond", Toast.LENGTH_SHORT).show();
                }
            }




            }

    }
}

1 个答案:

答案 0 :(得分:0)

看起来你的代码会在LinearLayout中产生越来越多的视图。这是你的意图吗?如果没有,您可以尝试在添加新视图之前从布局中删除过时的视图。问题可能是在添加第一个视图后,后续视图的布局中没有空间。

此外,动态添加这样的视图,似乎是绘制图形的奇怪方法。为什么不定义一个自定义视图来覆盖所有类型的绘图并将其静态包含在XML中?然后,你的onGesturePerformed()方法将调用视图的某个方法来指定要执行的绘图类型,然后调用invalidate()来触发重绘。然后,视图的onDraw()方法将绘制刚刚指定的图形。