1

所以我试图让我的页面的 imageview 组件在滑动时改变。我什至无法拿起滑动手势并将其转发到手势检测器上。

在 OnFling 方法中,我无法访问这些日志语句。有什么我想念的吗?

public class Detail extends Activity implements OnClickListener{

private GestureDetector gestureDetector;
private ImageView wallpaper;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            gestureDetector = new GestureDetector(new MyGestureDetector());
            wallpaper = (ImageView)findViewById(R.id.wallpaper);
    wallpaper.setAdjustViewBounds(true);

            wallpaper.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("getting", "here");
                            //return gestureDetector.onTouchEvent(event);
            return true;
                    }
            });
}

class MyGestureDetector extends SimpleOnGestureListener {
    private static final int SWIPE_MIN_DISTANCE = 50;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            Log.d("MOTION", "STARTED");
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                Log.d("Moving", "Right");
                return true;

            }  
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.d("Moving", "Left");
                return true;

            }
        } catch (Exception e) {

        }
        return false;
    }
}
4

3 回答 3

1

这是一个很好的例子 http://www.androidsnippets.com/gesturedetector-and-gesturedetectorongesturelistener

于 2012-03-28T10:57:09.387 回答
0

试试这个:我有一个名为 GenesMotionDetector.java 的类。这是它的代码:

package gene.com.motioneventssample;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

//This works
public class GenesMotionDetector extends Activity implements GestureDetector.OnGestureListener {
    private GestureDetector gestureScanner;
    LinearLayout mView1;
    TextView mView2;
    ImageView mView3;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nothing);
        gestureScanner= new GestureDetector(getBaseContext(),this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me) {
        System.out.println("Inside onTouchEvent() of GenesMotionDetector.java");
        return gestureScanner.onTouchEvent(me);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        System.out.println("Inside onDown() of GenesMotionDetector.java");
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        System.out.println("Inside onFling() of GenesMotionDetector.java");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        System.out.println("Inside onLongPress() of GenesMotionDetector.java");
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        System.out.println("Inside onScroll() of GenesMotionDetector.java");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        System.out.println("Inside onShowPress() of GenesMotionDetector.java");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        System.out.println("Inside onSingleTapUp() of GenesMotionDetector.java");
        return true;
    }
}

该类对应的 XML 布局文件是 nothing.xml。这是它的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:background="#17528c"
        android:text="testing"
        android:layout_width="100dp"
        android:layout_height="200dp" />

    <ImageView
        android:id="@+id/image"
        android:background="#f8af20"
        android:layout_width="100dp"
        android:layout_height="200dp" />
</LinearLayout>
于 2014-11-20T20:50:28.953 回答
0

你需要设置

mDetector.setIsLongpressEnabled(false);

Longpress 默认启用(true),它的作用是只监听 longpress 事件并丢弃滚动、投掷和其他复杂事件。

设置手势检测器对象后,只需输入该代码:

mDetector = new GestureDetectorCompat(context, this);
mDetector.setOnDoubleTapListener(this);
mDetector.setIsLongpressEnabled(false);

和平

于 2016-03-26T15:23:45.113 回答