2017-04-26 1 views
-2

다른 요소로 하나의 요소를 밀어 넣으려고하지만 아무 것도 나오지 않습니다. 또한 나는 이미지를 이동하거나이 아무것도안드로이드에서 한 이미지가 움직이는 동안 다른 뷰 요소를 교차 시키시겠습니까?

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); 
} 
관련 문제