我有一個圖像視圖,我在我的遊戲中繪製了我的遊戲板。我需要獲取觸摸相對於圖像視圖的X,Y.例如:如果有人碰到最左上角的像素,它會返回(0,0),或者說遊戲板是一塊100,100塊板子,我在中間碰到了,我會得到(50,50)...會這樣做嗎?在圖像視圖中檢測觸摸
gameView.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
int x = (int) event.getX()/pieceSize;
int y = (int) event.getY()/pieceSize;
Log.d("Touch Event", "Touch event at "+ x + " " +y);
doMove(x, y);
Log.d("Move", "The move was " + doMove(x,y));
}
gameView.setImageBitmap(Draw(pieceSize));
return true;
}
});