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

时间:2017-04-26 15:08:59

标签: android android-animation

我尝试用其他元素推送一个元素,但没有任何结果。 此外,我想动画相互交叉的元素 当我移动图像时,一两个都没有发生

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);
}

但吐司从未表现出来。 怎么了?

1 个答案:

答案 0 :(得分:0)

问题在于这一行

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);
}