2012-03-16 3 views
0

간단한 작업을하려고합니다. 화면의 왼쪽 상단에서 시작하는 서클이 있습니다. 화면의 아무 곳이나 터치하면 서클이 사용자가 터치 한 지점을 향해 천천히 움직이고 멈추도록하고 싶습니다. 나는 성공적으로 이것을 구현했지만, 원은 너무 빨리 움직인다. 나는 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--; 

      } 

     } 
    } 
+0

실행 루프에 Thread.sleep()을 추가해도 도움이되지 않습니까? 그리고 당신은 unlockAndPost를 각 비트 맵이 코드 조각에서 그냥 벗어난 후에 그립니다. –

+0

예, 몇 시간 간격으로 Thread.sleep()을 시도했지만 효과가없는 것 같습니다. 그리고 예, unlockAndPost() 메서드가 있습니다. 그러나 실수로이 코드를 생략했습니다. – Derek

답변

0

좋아, 당신이로 실행중인 문제는 당신이 계정에 경과 시간을 복용하지 않는 것입니다. 이것은 또한 애니메이션이 다른 장치에서 다른 속도로 움직이고 다른 것들이 백그라운드에서 움직이는 것을 의미합니다. 당신이해야 할 일은 원을 움직이기 원하는 밀리 초 당 픽셀 수를 결정한 다음 마지막 프레임 이후 경과 된 시간만큼 이동 거리를 수정하는 것입니다.

+0

선형 보간이 도움이 될 수도 있습니다. – triggs

관련 문제