간단한 작업을하려고합니다. 화면의 왼쪽 상단에서 시작하는 서클이 있습니다. 화면의 아무 곳이나 터치하면 서클이 사용자가 터치 한 지점을 향해 천천히 움직이고 멈추도록하고 싶습니다. 나는 성공적으로 이것을 구현했지만, 원은 너무 빨리 움직인다. 나는 run() 스레드를 몇 밀리 초 동안 잠자기 상태로 놓으려고 노력해야한다고 생각하지만, 성공적으로 수행 할 수는 없다. 이것에 대한 해결책은 무엇입니까? 코드는 간단하지만 아래에 게시 한 경우 사전에 도움을 주셔서 감사합니다!터치로 이미지가 너무 빨리 이동합니다.
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public GameView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
public void run()
{
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
int screenArrayWidth = screenWidth/50;
int screenArrayHeight = screenHeight/30;
int[][] mapArray = new int[50][30];
while (isItOK)
{
if (!holder.getSurface().isValid())
{
continue;
}
c = holder.lockCanvas();
c.drawARGB(255, 255, 0, 0);
c.drawBitmap(ball, destinationX - (ball.getWidth()/2),
destinationY - (ball.getHeight()/2), null);
}
}
public boolean onTouch(View v, MotionEvent me)
{
switch (me.getAction())
{
case MotionEvent.ACTION_DOWN:
{
while (((int)(me.getX()) != (int)(destinationX)) && ((int)(me.getY()) != (int)(destinationY)))
{
if (me.getX() > destinationX)
{
destinationX++;
}
if (me.getX() < destinationX)
{
destinationX--;
}
if (me.getY() > destinationY)
{
destinationY++;
}
if (me.getY() < destinationY)
{
destinationY--;
}
}
}
실행 루프에 Thread.sleep()을 추가해도 도움이되지 않습니까? 그리고 당신은 unlockAndPost를 각 비트 맵이 코드 조각에서 그냥 벗어난 후에 그립니다. –
예, 몇 시간 간격으로 Thread.sleep()을 시도했지만 효과가없는 것 같습니다. 그리고 예, unlockAndPost() 메서드가 있습니다. 그러나 실수로이 코드를 생략했습니다. – Derek