Android手势检测

问题描述:

我正在尝试开发一个基本上用于绘制shape的android应用程序。我希望用户在屏幕上制作手势,并且应该在屏幕上绘制与手势更加匹配的形状。 在我的应用程序中,我可以检测屏幕上正在执行的手势,如圆形,直线,矩形等,但是我的代码存在一些问题。它实际上检测到手势并绘制相应的形状,但它只发生一次。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(); 
       } 
      } 




      } 

    } 
} 
+0

你解决了你的问题吗?我也一样。 – LoveMeow 2013-08-12 16:35:34

完整的代码它看起来像你的代码将产生不断增长的的LinearLayout内观看次数。这是你的意图吗?如果没有,您可以尝试从布局中删除过时的视图,然后再添加新的视图。问题可能是在添加第一个视图后,在布局中没有空间。

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