1

我的 OnTouchListener 遇到了一个非常奇怪的问题。我昨晚将我的 Galaxy Nexus 升级到了 4.1.1,以下代码在所有以前的 android OS 版本上仍然有效,根本无法工作。我想这个监听器的调用有一些问题。以下是代码,如果您发现它很复杂,请忽略逻辑,只需查看日志的位置和输出:-

private OnTouchListener drag = new OnTouchListener() {
    private float error_x = 0;
    private float error_y = 0;
    private int c = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i("branch", "onTouch called " + ++c);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Log.i("branch", "1");
            error_x = event.getX();
            error_y = event.getY();
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            Log.i("branch", "2");
            refreshTriangleColor(v);
            root_layout.removeView(v);
            root_layout.addView(v);
            Point position = getViewPosition(v);
            if (position != null) {
                Log.i("branch", "3");
                if (triangle_red
                        .pointLiesInTriangle(position.x, position.y)) {
                    Log.i("branch", "4");
                    int x = Math.round(event.getRawX());
                    int y = Math.round(event.getRawY());
                    if (triangle_red.pointLiesInTriangle(x, y)) {
                        Log.i("branch", "5");
                        setViewPosition(v,
                                Math.round(event.getRawX() - error_x),
                                Math.round(event.getRawY() - error_y));
                    } else {
                        Log.i("branch", "6");
                        long d = 0;
                        d = Math.round(android.util.FloatMath.sqrt((((position.x - event
                                .getRawX()) * (position.x - event.getRawX())) - ((position.y - event
                                .getRawY()) * (position.y - event.getRawY())))));
                        if (d >= Util.convertDip2Pixels(MAX_RESISTANCE,
                                MelodyTriangle_AppActivity.this)) {
                            Log.i("branch", "7");
                            setViewPosition(v,
                                    Math.round(event.getRawX() - error_x),
                                    Math.round(event.getRawY() - error_y));
                            vibrate();
                        } else {
                            Log.i("branch", "8");
                            Point nearest_point = triangle_red
                                    .getNearestPointOnLine(new Point(x, y));
                            if (nearest_point != null) {
                                Log.i("branch", "9");
                                setViewPosition(
                                        v,
                                        Math.round(nearest_point.x
                                                - error_x),
                                        Math.round(nearest_point.y
                                                - error_y));
                            }
                        }
                    }
                } else {
                    Log.i("branch", "10");
                    setViewPosition(v,
                            Math.round(event.getRawX() - error_x),
                            Math.round(event.getRawY() - error_y));
                }
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            Log.i("branch", "11");
            Point position = getViewPosition(v);
            if (position != null) {
                Log.i("branch", "12");
                if (!triangle_red.pointLiesInTriangle(position.x,
                        position.y)) {
                    Log.i("branch", "13");
                    if (api.voices[0].isPlaying()) {
                        Log.i("branch", "14");
                        api.voices[0].stopPlaying();
                    }
                    animateToLayout(v, red_layout);
                } else {
                    Log.i("branch", "15");
                    playVoice(v, position.x, position.y);
                }
            }
        }
        return true;
    }
};

当我尝试拖动时,上面的代码给出了以下输出: -

07-18 16:28:53.897: I/branch(31176): onTouch called 1
07-18 16:28:53.897: I/branch(31176): 1
07-18 16:28:54.007: I/branch(31176): onTouch called 2
07-18 16:28:54.007: I/branch(31176): 2
07-18 16:28:54.007: I/branch(31176): onTouch called 3
07-18 16:28:54.007: I/branch(31176): 3
07-18 16:28:54.007: I/branch(31176): 10
07-18 16:28:56.077: I/branch(31176): onTouch called 4
07-18 16:28:56.077: I/branch(31176): 1
07-18 16:28:56.124: I/branch(31176): onTouch called 5
07-18 16:28:56.124: I/branch(31176): 2
07-18 16:28:56.124: I/branch(31176): onTouch called 6
07-18 16:28:56.124: I/branch(31176): 3
07-18 16:28:56.124: I/branch(31176): 10
07-18 16:29:00.764: I/branch(31176): onTouch called 7
07-18 16:29:00.764: I/branch(31176): 1
07-18 16:29:00.803: I/branch(31176): onTouch called 8
07-18 16:29:00.803: I/branch(31176): 2
07-18 16:29:00.803: I/branch(31176): onTouch called 9
07-18 16:29:00.803: I/branch(31176): 3
07-18 16:29:00.803: I/branch(31176): 10

注意到的问题是:-

  1. 由于拖动不顺畅,因此未定期调用 touch。您必须涂抹视图以将其从其位置略微移动,因此不会随着触摸而移动。然而,它在以前的版本中是常规的。

  2. event.getAction() == MotionEvent.ACTION_UP 仅在您触摸并释放视图时为真。而在以前的版本中,几乎每次都会调用它,因为视图会沿着触摸移动,并且无论何时释放都是如此。

知道出了什么问题吗?

谢谢

4

0 回答 0