Voici un exemple qui fonctionne très bien avec le 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>
Comment appliquer un nouveau geste ures?
- gestes de piste avec l'application Android Gesture Builder
- Créer un dossier de ressources
raw
dans votre projet: <Project>/app/src/main/res/raw/
- Copiez le fichier généré
gesture.txt
à partir de votre appareil: /Android/data/pack.GestureApp/files/gesture.txt
- Et enfin coller à le dossier créé
raw
: <Project>/app/src/main/res/raw/gesture.txt
Enfin j'ai poussé le code source GitHub: https://github.com/nok/android-gesture-libraries
2ème édition:
Vous avez raison. Donc j'utilise un simple ImageView
pour dessiner les gestes effectués. En outre, vous pouvez changer le style en mPaint
de type Paint. Vous pouvez trouver tous les changements dans le commit poussé 82eabfb et 5ce427d sur GitHub.
Cela fonctionne exactement comme mon code - aucune valeur ajoutée. Le geste disparaît de l'écran mais je souhaite qu'il reste à l'écran – scrayne
J'espère qu'un autre événement se produira lorsque le geste sera effectué mais avant qu'il ne disparaisse. – scrayne
D'accord, j'ai édité ma réponse, peut-être que les changements peuvent répondre à votre question. –