0

このメソッドを実装したい:

//#OnTouchEvent()
public boolean onTouchEvent(MotionEvent event) {
//Coordinates of the touch
int x = (int) event.getX();
int y = (int) event.getY();

switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    // Check if the touch is within the coordinates of an image
            if x isBetweenCoordinatesXofTheImage
                 if y isBetweenCoordinatesYofTheImage
                     //DoSomething
            else
                 //DoNothing
    break;
    case MotionEvent.ACTION_MOVE:
            //nothing
    break;
    case MotionEvent.ACTION_UP:
    //check if the touch is within the coordinates of an image;
            if x is betweenCoordinatesX of the image
                 y is betweenCoordinatesYofTheImage
                     //DoSomething
            else
                 //DoNothing
    break;
    }
    return true;
 }

ImageViewには4つの座標があり、前にチェックを行うには4つの座標(x、y)を知っている必要があります。左上隅は左下と同じX座標を持ち、右上は右下と同じX座標を持ちます。Y座標の場合も同様です。私の問題は、ImageViewの4つの座標を取得するにはどうすればよいですか?

全てに感謝!!!

4

2 に答える 2

2

ちょっとした質問: ImageView 自体をタッチ リスナーに設定しないのはなぜですか? それがオプションでない場合は、次を試してください。

int pos[] = new int[2];
yourImageView.getLocationOnScreen(pos);

このメソッドを呼び出すと、配列にはビューの左上の座標が含まれます。他の座標を取得するには、ビューの高さと幅を使用するだけです:

yourImageView.getWidth();
yourImageView.getHeight();

これらの 3 つのメソッドは、ビューが画面に描画された後にのみ呼び出されるようにしてください (onCreate で呼び出すと、すべての値に 0 が返されます)。

于 2012-09-25T15:54:43.543 に答える
0

試す

getLocationOnScreen() 

および/または

getLocationInWindow()

あなたのimageViewで。

于 2012-09-25T15:39:07.350 に答える