2011-09-01 7 views
9

주어진 절대 x/y 픽셀 좌표에 표시되는보기를 찾을 수 있습니까?주어진 x/y 좌표로 안드로이드에서보기 찾기

편집 : 나는 좋은 작품에 적합한 솔루션을 발견 :

int[] cardReference = new int[]{R.id.card1_1, R.id.card1_2, R.id.card1_3, R.id.card1_4, 
           R.id.card2_1, R.id.card2_2, R.id.card2_3, R.id.card2_4, 
           R.id.card3_1, R.id.card3_2, R.id.card3_3, R.id.card3_4, 
           R.id.card4_1, R.id.card4_2, R.id.card4_3, R.id.card4_4, 
           R.id.card5_1, R.id.card5_2, R.id.card5_3, R.id.card5_4}; 
: cardReference은 (20 TextViews은 4 × 5 매트릭스 배열 내 경우) 자원 정수의 배열 인

private View findViewByCoord(float x, float y){ 
    TextView textView = null; 
    int[] location = new int[2]; 
    int width = 0; 
    int height = 0; 
    for(int reference : cardReference){ 
     textView = (TextView) findViewById(reference); 
     textView.getLocationOnScreen(location); 
     width = textView.getWidth(); 
     height = textView.getHeight(); 
     if(location[0] <= x && x <= (location[0] + width) && location[1] <= y && y <= (location[1] + height)){ 
      Log.i("Test", "Card " + textView.getText() + " is pointed"); 
      return textView; 
     } 
    } 
    return null; 
} 

성능 향상을 위해 모든 루프에서 findViewById()를 호출 한 다음 TextViews 배열을 사용하는 것이 좋습니다.

답변

4

하나의 '해결책'은 부모보기의 하위를 반복하고 getLeft()getTop()의 좌표를 선택한 X 및 Y 좌표와 비교하는 것입니다. 일치하는 항목이 있으면보기가 가능합니다.

다른 대안을 듣고 싶습니다.

편집 : 또한 왼쪽 위 및 위 좌표와 관련하여보기의 높이/너비를 조정해야 좌표가 해당 범위 내에 있는지 확인할 수 있습니다.

관련 문제