2011-12-15 120 views
2

我正在嘗試開發一個基本上用於繪製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

回答

0

完整的代碼它看起來像你的代碼將產生不斷增長的的LinearLayout內觀看次數。這是你的意圖嗎?如果沒有,您可以嘗試從佈局中刪除過時的視圖,然後再添加新的視圖。問題可能是在添加第一個視圖後,在佈局中沒有空間。

此外,動態添加這樣的視圖看起來像是一種奇怪的繪製圖形的方式。爲什麼不定義一個自定義視圖來覆蓋所有類型的繪圖並將其靜態包含在XML中?然後,您的onGesturePerformed()方法將調用您的視圖的某個方法來指定要執行的繪圖類型,然後調用invalidate()來觸發重繪。然後,視圖的onDraw()方法將繪製剛剛指定的圖形。