GestureDetector不起作用(来自android开发人员的示例)

时间:2013-05-09 00:54:04

标签: android gesture

我正在android开发人员的android中阅读这个手势,按照教程我尝试运行以下代码:

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;
    }
}

代码来自网站here

但是当我运行应用程序时,单击或长按视图时没有调试信息。

顺便说一下,我在模拟器和htc e1设备上测试应用程序。

有什么问题?

7 个答案:

答案 0 :(得分:7)

您正在创建一个GestureDetector,但您永远不会将其“挂钩”到您的View。 尝试更改你的onCreate,如下所示:

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);
    }
});

答案 1 :(得分:4)

迟到了,但我解决这个问题的方法是:

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.mDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

探测器的返回丢失了。 改为:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.mDetector != null) return this.mDetector.onTouchEvent(event);
   return super.onTouchEvent(event);
}

但这是一个很好的解决方案吗? Android Insider的时间。 ;)

Tschau Fred

答案 2 :(得分:0)

我编辑了你的第一行:

public class MainActivity extends Activity implements OnGestureListener, OnDoubleTapListener {

我使用了你的代码,但提供了如下的完整界面名称:

public class MainActivity extends Activity implements GestureDetector.OnGestureListener,     GestureDetector.OnDoubleTapListener {

这在编辑后起作用。希望这会有所帮助。

答案 3 :(得分:0)

以下是用于手势方向检测的简单Android代码

  

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>

答案 4 :(得分:0)

感谢Rahul Raina,

你的解决方案对我来说很有魅力。然而,当我尝试生成一个已签名的apk以便部署到其他设备时,我得到了与xpagesbeast相同的错误:&#34; GestureDetector&#34;带有下划线红色,Android Studio中出现错误。错误消息&#34; GestureDetector无法解析为类型&#34; -

我只是在下一行中使用了一个显式强制转换为(int),这使它工作,我可以构建apk:

lib = GestureLibraries.fromRawResource(DisplayPersonByIDActivity.this,
            (int)R.id.gestureOverlayView1);

答案 5 :(得分:0)

我找到的解决方案是在视图上调用setOnClickListener()

看来,即使您可以顺利进行触摸事件,但除非将视图标记为可点击,否则手势检测器不会执行任何操作。

也可以拨打mat2str。它似乎也将视图标记为可点击。

答案 6 :(得分:0)

如果您关注Android开发人员文档,我会在自己的答案中找到@FoamyGuy评论,以使其完美工作:

更改

    override fun onTouchEvent(event: MotionEvent): Boolean {
    mDetector.onTouchEvent(event)
    return super.onTouchEvent(event)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
    mDetector.onTouchEvent(ev)
    return super.dispatchTouchEvent(ev)
}