2011-10-26 57 views
0

我試圖從某些點移動位圖圖片到我點觸的地方......但事情進展不順利。如何使用SurfaceView將位圖移動到觸摸位置?

Render.java(擴展Thread類)

public void run() { 
    Canvas canvas; 
    while (runFlag) { 
     long now = System.currentTimeMillis(); 
     long elapsedTime = now - prevTime; 
     if (elapsedTime > 30){ 
      prevTime = now; 
        if (touched ==true) { 
      matrix.postTranslate(touched_x, touched_y); 
       touched =false; 
       } 
     } 
     canvas = null; 
     try { 
      canvas = surfaceHolder.lockCanvas(null); 
      synchronized (surfaceHolder) { 
        if (touched ==true) { 
        canvas.drawColor(Color.BLACK); 
        canvas.drawBitmap(picture, matrix, null); 
        touched = false; 
        } 
      } 
     } 
     finally { 
      if (canvas != null) { 
       surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
    } 
} 

在這裏,我創建一個標誌感動繪圖時,檢查它...看來,這不是好主意。

View.java(SurfaceView類)

public boolean onTouchEvent(MotionEvent event) { 
      touched_x = event.getX(); 
      touched_y = event.getY(); 

      int action = event.getAction(); 
      switch(action){ 
      case MotionEvent.ACTION_DOWN: 
      touched = true; 
      break; 
      case MotionEvent.ACTION_MOVE: 
      touched = true; 
      break; 
      case MotionEvent.ACTION_UP: 
      touched = false; 
      break; 
      case MotionEvent.ACTION_CANCEL: 
      touched = false; 
      break; 
      case MotionEvent.ACTION_OUTSIDE: 
      touched = false; 
      break; 
      default: 
      } 
     return false; 
    } 

回答

0

我認爲這是在你對你正在嘗試顯示圖像的代碼沒有持久性。該代碼看起來像在一幀中,並在接下來擦除它。使用一個數組或列表來保存所有想要繪製的圖像,並在draw方法中循環。