2017-04-26 73 views
-2

我尝试用其他元素推送一个元素,但没有出现。 此外,我想动画,将彼此相交 当我移动图像中的一个或两个没有任何反应在android中移动一个图像时相交其他视图元素?

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

你有没有登录'boxRect'和'box2Rect'的价值? – azizbekian

+0

现在看起来像 –

+0

@azizbekian是的..我更新我的代码..我不明白 –

回答

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); 
} 
相关问题