I'm using the Gesture API with a Gesture Library that I created, and it is working really well. The problem is that I would like the gesture to be visible on the screen after OnGesturePerformedListener exits, but instead the gesture is erased. I was thinking that perhaps there is an event after OnGesturePerformedListener -- I could save the gesture in OnGesturePerformedListener and then display it again in this later event. Anyone know if there is such an event? Here is the code:
private OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView gestureView,
Gesture gesture) {
if (gesture.getStrokesCount() != 2){
setWonderEmoticon();
return;
}
ArrayList<Prediction> predictions = gLib.recognize(gesture);
// one prediction needed
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);
// checking prediction
if (prediction.score > 20.0) {
setHappyEmoticon();
}
else {
setWonderEmoticon();
}
}
}
};
By the way, the same thing happens when setWonderEmoticon() and setHappyEmoticon() are removed from the code.
Detect gestures. Android provides the GestureDetector class for detecting common gestures. Some of the gestures it supports include onDown() , onLongPress() , onFling() , and so on. You can use GestureDetector in conjunction with the onTouchEvent() method described above.
Different sequences of motion add up to different gestures. For example, a fling gesture involves the user pressing a finger down on the screen, swiping across the screen, and lifting the finger up off the screen while the swipe is still in motion (that is, without slowing down to stop before lifting the finger).
Android provides special types of touch screen events such as pinch , double tap, scrolls , long presses and flinch. These are all known as gestures. Android provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not.
Here is an example that works fine with the OnGesturePerformedListener
:
MainActivity.java:
// ...
public class MainActivity extends AppCompatActivity implements GestureOverlayView.OnGesturePerformedListener, GestureOverlayView.OnGestureListener {
GestureOverlayView mGestureView;
ImageView mImageView;
TextView mText;
Paint mPaint;
Bitmap mBitmap;
Canvas mCanvas;
GestureLibrary mGestureLib;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image_view);
mGestureView = (GestureOverlayView) findViewById(R.id.gesture_overlay);
mGestureView.addOnGesturePerformedListener(this);
mGestureView.addOnGestureListener(this);
mGestureView.setGestureColor(Color.BLACK);
mGestureView.setUncertainGestureColor(Color.BLACK);
mGestureView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
mGestureView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mBitmap = Bitmap.createBitmap(mGestureView.getWidth(), mGestureView.getHeight(), Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
});
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(12.0f);
mPaint.setColor(Color.GRAY);
mText = (TextView) findViewById(R.id.text);
mGestureLib = GestureLibraries.fromRawResource(getApplicationContext(), R.raw.gesture);
mGestureLib.load();
}
@Override
public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {
if (gesture.getStrokesCount() > 0) {
ArrayList<Prediction> predictions = mGestureLib.recognize(gesture);
for (GestureStroke stroke : gesture.getStrokes()) {
Path path = stroke.getPath();
mCanvas.drawPath(path, mPaint);
}
mImageView.setImageBitmap(mBitmap);
if (!predictions.isEmpty()) {
Prediction best = predictions.get(0);
mText.setText(String.format("Gesture: %s", best.name));
}
}
}
// ...
}
activity_main.xml (snapshot):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="onl.nok.gestures.MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting ..."
android:textSize="25sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image_view" />
<android.gesture.GestureOverlayView
android:id="@+id/gesture_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeType="single"
android:fadeEnabled="false" />
</RelativeLayout>
How to apply new gestures?
raw
in your project:
<Project>/app/src/main/res/raw/
gesture.txt
from your device:
/Android/data/pack.GestureApp/files/gesture.txt
raw
:
<Project>/app/src/main/res/raw/gesture.txt
Finally I pushed the source code to GitHub: https://github.com/nok/android-gesture-libraries
2nd edit:
You're right. So I use a simple ImageView
to draw the performed gestures. Furthermore you can change the style in mPaint
of type Paint. You can find all changes in the pushed commit 82eabfb and 5ce427d on GitHub.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With