Android OnGestureListener Fling 未检测到

发布于 2024-10-22 04:09:27 字数 1371 浏览 5 评论 0原文

我想检测屏幕块中的快速移动。我正在使用以下代码。

public class MyinfoActivity extends Activity implements OnGestureListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ..........
        ..........
        gestureScanner = new GestureDetector(this);
        resBlock = (TableRow) findViewById(R.id.ResBlock);
        gestureScanner = new GestureDetector(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me){
        Log.d(null,"Touch");
        if (gestureScanner.onTouchEvent(me))
            return gestureScanner.onTouchEvent(me);
        else
            return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                       float velocityX, float velocityY) {
        Log.d(null,"Fling");
        ............
        ............
    }

    @Override
    public boolean onDown(MotionEvent arg0) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {}

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                        float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {}

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
}

它正在检测 TouchEvent,但没有检测到任何猛击动作。我的代码有什么问题?

I want to detect fling motion in a block of the screen. I am using the following code for that.

public class MyinfoActivity extends Activity implements OnGestureListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ..........
        ..........
        gestureScanner = new GestureDetector(this);
        resBlock = (TableRow) findViewById(R.id.ResBlock);
        gestureScanner = new GestureDetector(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me){
        Log.d(null,"Touch");
        if (gestureScanner.onTouchEvent(me))
            return gestureScanner.onTouchEvent(me);
        else
            return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                       float velocityX, float velocityY) {
        Log.d(null,"Fling");
        ............
        ............
    }

    @Override
    public boolean onDown(MotionEvent arg0) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {}

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                        float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {}

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
}

It is detecting the TouchEvent, but it is not detecting any fling motion. What is the problem in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦言归人 2024-10-29 04:09:27

我使用了以下代码并解决了该问题。

public class MyinfoActivity extends Activity {
    private GestureDetector gestureScanner;

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureScanner.onTouchEvent(event);
    }

    GestureDetector.SimpleOnGestureListener simpleOnGestureListener =
                       new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent event) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2,
                                   float velocityX, float velocityY) {
            Log.d(null,"Fling");
            int dx = (int) (event2.getX() - event1.getX());
            // don't accept the fling if it's too short
            // as it may conflict with a button push
            if (Math.abs(dx) > MAJOR_MOVE 
                       && Math.abs(velocityX) > Math.abs(velocityY)) {
                if (velocityX > 0) {
                    moveGraph("L");
                } else {
                    moveGraph("R");
                }
                return true;
            } else {
                return false;
            }
        }
    };
}

I used the following code and solved the issue.

public class MyinfoActivity extends Activity {
    private GestureDetector gestureScanner;

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureScanner.onTouchEvent(event);
    }

    GestureDetector.SimpleOnGestureListener simpleOnGestureListener =
                       new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent event) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2,
                                   float velocityX, float velocityY) {
            Log.d(null,"Fling");
            int dx = (int) (event2.getX() - event1.getX());
            // don't accept the fling if it's too short
            // as it may conflict with a button push
            if (Math.abs(dx) > MAJOR_MOVE 
                       && Math.abs(velocityX) > Math.abs(velocityY)) {
                if (velocityX > 0) {
                    moveGraph("L");
                } else {
                    moveGraph("R");
                }
                return true;
            } else {
                return false;
            }
        }
    };
}
贪了杯 2024-10-29 04:09:27

首先,您的 onTouchEvent 方法不正确,将导致gestureScanner 对象对 onTouchEvent 进行 2 次调用。
你需要这样改变:

    @Override
    public boolean onTouchEvent(MotionEvent me){
         Log.d(null,"Touch");
         if (gestureScanner.onTouchEvent(me))
             return true;
         else
             return super.onTouchEvent(event); // or false (it's what you whant).
    }

你也有这行两次:

gestureScanner = new GestureDetector(this);

然后尝试改变你的 onFling 方法,如下所示:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
    try{
        if(Math.abs(e1.getY()-e2.getY()) > 250) 
            return false;               
        if(e1.getX() - e2.getX() > 120 && Math.abs(velocityX) > 200){
            Log.d("Fling", "Move Next");
            //do something...
        }
        else if(e2.getX() - e1.getX() > 120 && Math.abs(velocityX) > 200){
            Log.d("Fling", "Move Previous");
            //do something...
        }
        return false;
    }
    catch(Exception e){
        return false;
    }
}

First your onTouchEvent method is incorrect and will cause 2 call of onTouchEvent by the gestureScanner object.
You need to change by this :

    @Override
    public boolean onTouchEvent(MotionEvent me){
         Log.d(null,"Touch");
         if (gestureScanner.onTouchEvent(me))
             return true;
         else
             return super.onTouchEvent(event); // or false (it's what you whant).
    }

You also have this line twice :

gestureScanner = new GestureDetector(this);

Then try to change your onFling method like this :

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
    try{
        if(Math.abs(e1.getY()-e2.getY()) > 250) 
            return false;               
        if(e1.getX() - e2.getX() > 120 && Math.abs(velocityX) > 200){
            Log.d("Fling", "Move Next");
            //do something...
        }
        else if(e2.getX() - e1.getX() > 120 && Math.abs(velocityX) > 200){
            Log.d("Fling", "Move Previous");
            //do something...
        }
        return false;
    }
    catch(Exception e){
        return false;
    }
}
客…行舟 2024-10-29 04:09:27

onDown 方法中返回 true

@Override
public boolean onDown(MotionEvent arg0) {
    return true;
}

return true in your onDown method

@Override
public boolean onDown(MotionEvent arg0) {
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文