Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当一个图像在android中移动时,是否与其他视图元素相交?_Android_Android Animation - Fatal编程技术网

当一个图像在android中移动时,是否与其他视图元素相交?

当一个图像在android中移动时,是否与其他视图元素相交?,android,android-animation,Android,Android Animation,我试着把一个元素和其他元素推到一起,但什么都没有出来。 此外,我想动画元素将相交彼此 当我将图像移动一两次时,什么也没发生 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); box.setOnTouchListener(new View.OnTouchListene

我试着把一个元素和其他元素推到一起,但什么都没有出来。 此外,我想动画元素将相交彼此 当我将图像移动一两次时,什么也没发生

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    box.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {                
        switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    dX = v.getX() - event.getRawX();
                    dY = v.getY() - event.getRawY();

                    return true;
                case MotionEvent.ACTION_MOVE:
                    box.animate()
                            .x(event.getRawX() + dX)
                            .y(event.getRawY() + dY)
                            .setDuration(0)
                            .start();

                    if (viewsOverlap(box, box2)) {
                        Log.d("TAG", "getX = " + box.getX() + "getY = " + box.getY());
                    }
                    velocityTracker.addMovement(event);
                    return true;
            }
            return false;
        }
    });
    box2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        box2.getRight(), box2.getBottom());
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    dX2 = v.getX() - event.getRawX();
                    dY2 = v.getY() - event.getRawY();

                    return true;
                case MotionEvent.ACTION_MOVE:
                    box2.animate()
                            .x(event.getRawX() + dX2)
                            .y(event.getRawY() + dY2)
                            .setDuration(0)
                            .start();

                    if (viewsOverlap(box, box2)) {
                        Log.d("TAG", "getX2 = " + box2.getX() + "getY2 = " + box2.getY());
                    }
                    velocityTracker.addMovement(event);
                    return true;
            }
            return false;
        }
    });
}

private boolean viewsOverlap(View v1, View v2) {


    int[] v1_coords = new int[2];
    v1.getLocationOnScreen(v1_coords);
    int v1_w = v1.getWidth();
    int v1_h = v1.getHeight();
    Rect v1_rect = new Rect(v1_coords[0], v1_coords[1], v1_coords[0] + v1_w, v1_coords[1] + v1_h);
    //textView.setText("v1[0]= " + v1_coords[0] +" v1[1]= " + v1_coords[1] + "v1[0] + w= " +  (v1_coords[0] + v1_w )+ "v1[1]+h= " + (v1_coords[1] + v1_h));

    int[] v2_coords = new int[2];
    v2.getLocationOnScreen(v1_coords);
    int v2_w = v2.getWidth();
    int v2_h = v2.getHeight();
    Rect v2_rect = new Rect(v2_coords[0], v2_coords[1], v2_coords[0] + v2_w, v2_coords[1] + v2_h);
    //textView2.setText("v1[0]= " + v1_coords[0] +" v1[1]= " + v1_coords[1] + "v1[0] + w= " +  (v1_coords[0] + v1_w )+ "v1[1]+h= " + (v1_coords[1] + v1_h));
    return v1_rect.intersect(v2_rect) || v1_rect.contains(v2_rect) || v2_rect.contains(v1_rect);
}
但吐司永远不会出现。
怎么了?

问题出在这条线上

v2.getLocationOnScreen(v2_coords)

所以,如果你使用这种方法,一切都很好

 private boolean viewsOverlap(View v1, View v2) {


    int[] v1_coords = new int[2];
    v1.getLocationOnScreen(v1_coords);
    int v1_w = v1.getWidth();
    int v1_h = v1.getHeight();
    Rect v1_rect = new Rect(v1_coords[0], v1_coords[1], v1_coords[0] + v1_w, v1_coords[1] + v1_h);
    //textView.setText("v1[0]= " + v1_coords[0] +" v1[1]= " + v1_coords[1] + "v1[0] + w= " +  (v1_coords[0] + v1_w )+ "v1[1]+h= " + (v1_coords[1] + v1_h));
    //Log.d("TAG","v1[0]= " + v1_coords[0] +" v1[1]= " + v1_coords[1] + "v1[0] + w= " +  (v1_coords[0] + v1_w )+ "v1[1]+h= " + (v1_coords[1] + v1_h));

    int[] v2_coords = new int[2];
    v2.getLocationOnScreen(v2_coords);
    int v2_w = v2.getWidth();
    int v2_h = v2.getHeight();
    Rect v2_rect = new Rect(v2_coords[0], v2_coords[1], v2_coords[0] + v2_w, v2_coords[1] + v2_h);
    //textView2.setText("v1[0]= " + v1_coords[0] +" v1[1]= " + v1_coords[1] + "v1[0] + w= " +  (v1_coords[0] + v1_w )+ "v1[1]+h= " + (v1_coords[1] + v1_h));
    //Log.d("TAG2","v2[0]= " + v2_coords[0] +" v2[1]= " + v2_coords[1] + "v2[0] + w= " +  (v2_coords[0] + v2_w )+ "v2[1]+h= " + (v2_coords[1] + v2_h));
    return v1_rect.intersect(v2_rect) || v1_rect.contains(v2_rect) || v2_rect.contains(v1_rect);
}

您是否记录了
boxRect
box2Rect
的值?现在是查看like@azizbekian对我更新了我的代码。我不明白。那么,我们应该猜猜那些值是什么,还是你应该发布它们?@azizbekianThanks,我发现了错误