0
我試圖找到協調是一個用戶只在imageview內觸摸。查找圖像視圖內的座標?
到目前爲止,我已經設法通過下面的代碼來做到這一點:
img = (ImageView) findViewById(R.id.image);
img.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
x = (int) event.getX();
y = (int) event.getY();
int[] viewCoords = new int[2];
img.getLocationOnScreen(viewCoords);
int imageX = (int) (x - viewCoords[0]); // viewCoods[0] is the X coordinate
int imageY = (int) (y - viewCoords[1]); // viewCoods[1] is the y coordinate
text.setText("x:" +x +"y"+y);
return false;
}
});
然而,這是onTouchListener這意味着它只有每個觸摸後發現座標,我想要做的就是創建這樣當用戶在圖像視圖周圍移動他們的手指時,它總是找到座標。我實現了這個針對整個屏幕這段代碼:
@Override
public boolean onTouchEvent(MotionEvent event) {
x = (float)event.getX();
y = (float)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
text.setText("x:"+ x +" y:" +y);
return false;
}
但是我不知道如何使只有ImageView的內此代碼的工作。
非常感謝!這麼簡單,但你爲我節省了很多時間。 – user1472757 2012-08-09 22:52:05