12

我正在 android developer 阅读 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;
    }
}

代码来自这里的站点。

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

顺便说一句,我在模拟器和 htc e1 设备中测试了该应用程序。

问题是什么?

4

7 回答 7

7

您正在创建一个 GestureDetector,但您永远不会将其“连接”到您的视图。尝试像这样更改您的 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);
    }
});
于 2013-05-09T02:29:28.490 回答
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 的时间。;)

曹弗雷德

于 2013-12-19T13:52:47.033 回答
0

我编辑了你的第一行:

public class MainActivity extends Activity implements OnGestureListener, OnDoubleTapListener {

我使用了您的代码,但提供了完整的接口名称,如下所示:

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

这在编辑后起作用。希望这可以帮助。

于 2013-06-06T04:45:18.680 回答
0

感谢拉胡尔雷纳,

你的解决方案对我来说就像一个魅力。但是,当我尝试生成签名的 apk 以部署到其他设备时,我遇到了与 xpagesbeast 相同的错误:“GestureDetector”带有红色下划线,Android Studio 中的错误。错误消息“GestureDetector 无法解析为类型” –

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

lib = GestureLibraries.fromRawResource(DisplayPersonByIDActivity.this,
            (int)R.id.gestureOverlayView1);
于 2018-04-26T08:31:19.740 回答
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>
于 2016-06-06T06:42:28.933 回答
0

我找到的解决方案是调用setClickable(true)视图。

似乎即使您可以正常接收触摸事件,手势检测器也不会做任何事情,除非视图被标记为可点击。

调用setOnClickListener()也有效。它似乎也将视图标记为可点击。

于 2019-06-16T22:27:32.407 回答
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)
}
于 2020-10-05T22:53:09.817 回答