我有一個使用OnTouch的畫布繪圖,但由於某種原因,我無法使其正常工作。它第一次運作(第一次觸摸運動)。但是,當用戶移開他的手指時,OnTouch再也不會再次運行,停止用戶移動角色項目。Android - onTouchEvent只能第一次工作
經過大量的研究和各種不同的選擇,我簡直無法得到這個工作,並在這樣做注意到ACTION_DOWN首次調用,然後在ACTION_UP被調用。
下面你可以找到我的2個代碼塊,第一個是實際的onTouchEvent。第二大塊代碼用於處理我繪製的畫布(基於迷宮(5 x 5))內的用戶權限。
另一個諾雷要考慮的是在畫布重繪(無效),每次用戶位置移動(這樣做是由正方形正方形)
@Override
public boolean onTouchEvent(MotionEvent event)
{
float touchX = event.getX();
float touchY = event.getY();
int currentX = maze.getCurrentX();
int currentY = maze.getCurrentY();
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
if(Math.floor(touchX/totalCellWidth) == currentX && Math.floor(touchY/totalCellHeight) == currentY)
{
dragging = true;
return true;
}
break;
case MotionEvent.ACTION_UP:
dragging = false;
break;
case MotionEvent.ACTION_MOVE:
if(dragging)
{
int cellX = (int)Math.floor(touchX/totalCellWidth);
int cellY = (int)Math.floor(touchY/totalCellHeight);
if((cellX != currentX && cellY == currentY) || (cellY != currentY && cellX == currentX))
{
boolean moved = false;
switch(cellX-currentX)
{
case 1:
moved = maze.move(Maze.RIGHT);
break;
case -1:
moved = maze.move(Maze.LEFT);
}
switch(cellY-currentY)
{
case 1:
moved = maze.move(Maze.DOWN);
break;
case -1:
moved = maze.move(Maze.UP);
}
if(moved)
{
invalidate();
if(maze.isGameComplete())
{
showFinishDialog();
}
}
}
break;
}
}
return true;
}
迷宮位置處理程序代碼:
public boolean move(int direction)
{
boolean moved = false;
if(direction == UP)
{
if(currentY != 0 && !horizontalLines[currentY-1][currentX])
{
currentY--;
moved = true;
}
}
if(direction == DOWN)
{
if(currentY != verticalLines[0].length-1 && !horizontalLines[currentY][currentX])
{
currentY++;
moved = true;
}
}
if(direction == RIGHT)
{
if(currentX != horizontalLines[0].length-1 && !verticalLines[currentY][currentX])
{
currentX++;
moved = true;
}
}
if(direction == LEFT)
{
if(currentX != 0 && !verticalLines[currentY][currentX-1])
{
currentX--;
moved = true;
}
}
if(moved)
{
if(currentX == finalX && currentY == finalY)
{
gameComplete = true;
}
}
return moved;
}
從開關案例中刪除MotionEvent.ACTION_MASK – AkashG 2012-07-25 09:32:09
@AkashG,試過,但沒有工作還有其他想法嗎? – 2012-07-25 09:38:20
你的意思是說,當用戶第一次做ACTION_DOWN + MOVE + UP它確定,但下次不起作用?第二次輸入MOVE並拖動是否等於true? – Dayerman 2012-07-25 09:59:04