Android 4.1.1 JellyBean上的OnTouchListener不规则

时间:2012-07-18 15:53:22

标签: android ontouchlistener android-4.2-jelly-bean

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

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. 由于拖动不平滑而没有定期调用触摸。您必须弄脏视图,使其从位置稍微移动,因此不会随着触摸而移动。然而,它在以前的版本中是常规的。

  2. event.getAction()== MotionEvent.ACTION_UP仅在您触摸并释放视图时才为true。几乎每次都在以前的版本上调用它,因为视图沿着触摸移动,每当你发布时都是如此。

  3. 有什么想法会出错?

    由于

0 个答案:

没有答案