OnFling和ImageView无法正常工作

时间:2012-03-27 15:41:29

标签: android imageview gesture gesturedetector onfling

所以我试图让我的页面的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;
    }
}

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分: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>

答案 2 :(得分:0)

你需要设置

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

默认情况下启用Longpress(true),它的作用是仅侦听longpress事件并丢弃滚动,投掷和其他复杂事件。

在设置手势检测器对象后,只需将代码放入:

DefaultListModel

和平