我正在嘗試開發一個基本上用於繪製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();
}
}
}
}
}
你解決了你的問題嗎?我也一樣。 – LoveMeow 2013-08-12 16:35:34