2011-09-01 6 views
9

で検索すると、特定の絶対x/yピクセル座標で表示されるビューを見つけることは可能ですか?指定されたx/y座標を含むビューをandroid

編集:私は素晴らしい作品の適切な解決策を見つけた:

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

パフォーマンスを向上させるために、私はTextViewの配列を使用し、すべてのループでfindViewById()を呼び出すことを検討します。

答えて

4

「解決策」は、親ビューの子をループして、getLeft()getTop()の座標を、選択したX座標とY座標と照合することです。一致するものがあれば、あなたの意見があります。

私は他の選択肢を聞いてみたいと思います。

編集:座標の範囲内にあるかどうかを確認するために、与えられた左上および上の座標に対してビューの高さ/幅も調整する必要があります。

関連する問題