android gesture,Android Gesture Detection

I'm trying to develop an android application that is basically for drawing shapes.I want the user to make gesture on the screen and the shape that is more closely matching to gesture should be drawn on the screen.

In my application,I can detect the gesture that is being performed on the screen like circle,line,rectangle etc. but there is some problem with my code.It actually detects the gesture and draws the respective shape but it happens only once.

For example. If I draw Line on the screen then line is drawn on my view but after that If I draw circle or rectangle etc then gesture is recognized but the shape is not drawn there.

Here is the full code of that

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 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值