问题
I am reading the gesture in android at android developer, and following the tutorial I tried to run the following codes:
public class MainActivity extends Activity implements OnGestureListener, OnDoubleTapListener {
private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector;
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = new RelativeLayout(this);
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(v);
mDetector = new GestureDetectorCompat(this, this);
mDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent event) {
Log.d(DEBUG_TAG, "onDown: " + event.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString() + event2.toString());
return true;
}
@Override
public void onLongPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d(DEBUG_TAG, "onScroll: " + e1.toString() + e2.toString());
return true;
}
@Override
public void onShowPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
return true;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return true;
}
}
The codes came from the site here.
But when I ran the app, I got no debug information when I click or long press the view.
BTW, I test the app in both emulator and htc e1 device.
What is the problem?
回答1:
You are creating a GestureDetector but you are never "hooking it up" to your View. Try changing your onCreate like this:
super.onCreate(savedInstanceState);
View v = new RelativeLayout(this);
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(v);
mDetector = new GestureDetectorCompat(this, this);
mDetector.setOnDoubleTapListener(this);
v.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent me){
return mDetector.onTouchEvent(me);
}
});
回答2:
Little late, but my solution of this problem is:
@Override
public boolean onTouchEvent(MotionEvent event) {
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
The return of the Detector gone lost. Change to:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.mDetector != null) return this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
But is this a good solution? Time for Android Insider. ;)
Tschau Fred
回答3:
I edited your first line:
public class MainActivity extends Activity implements OnGestureListener, OnDoubleTapListener {
I used your code but supplied the full interface names like so:
public class MainActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
This worked after the edits. Hope this helps.
回答4:
Here is simple Android Code for gesture direction detection:
MainActivity.java
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.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.GestureStroke;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity implements
OnGesturePerformedListener {
GestureOverlayView gesture;
GestureLibrary lib;
ArrayList<Prediction> prediction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lib = GestureLibraries.fromRawResource(MainActivity.this,
R.id.gestureOverlayView1);
gesture = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
gesture.addOnGesturePerformedListener(this);
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// TODO Auto-generated method stub
ArrayList<GestureStroke> strokeList = gesture.getStrokes();
// prediction = lib.recognize(gesture);
float f[] = strokeList.get(0).points;
String str = "";
if (f[0] < f[f.length - 2]) {
str = "Right gesture";
} else if (f[0] > f[f.length - 2]) {
str = "Left gesture";
} else {
str = "no direction";
}
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
}
}
activity_main.xml
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
xmlns:android2="http://schemas.android.com/apk/res/android"
android:id="@+id/gestureOverlayView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android1:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw gesture"
android:textAppearance="?android:attr/textAppearanceMedium" />
</android.gesture.GestureOverlayView>
回答5:
Thank Rahul Raina,
your solution works like a charme for me. However when I tried to generate a signed apk in order to deploy to other devices I got the same error as xpagesbeast: the "GestureDetector" is underlined red, error in Android Studio. Error message "GestureDetector cannot be resolved to a type" –
I just used an explicit cast to (int) in the following line and this made it work and I could build the apk:
lib = GestureLibraries.fromRawResource(DisplayPersonByIDActivity.this,
(int)R.id.gestureOverlayView1);
回答6:
The solution I found was to call setClickable(true)
on the view.
It seems that even though you will get touch events okay, the gesture detectors won't do anything unless the view is marked as clickable.
Calling setOnClickListener()
also works. It seems to also mark the view as clickable.
来源:https://stackoverflow.com/questions/16452627/the-gesturedetector-does-not-work-example-from-android-developer