Android 如何使用SurfaceView将位图移动到触摸位置?

Android 如何使用SurfaceView将位图移动到触摸位置?,android,matrix,bitmap,surfaceview,Android,Matrix,Bitmap,Surfaceview,我正在尝试将位图图片从我触摸的某个点移动到另一个点。。。但事情进展不顺利 java扩展了线程类 public void run() { Canvas canvas; while (runFlag) { long now = System.currentTimeMillis(); long elapsedTime = now - prevTime; if (elapsedTime > 30){ prevT

我正在尝试将位图图片从我触摸的某个点移动到另一个点。。。但事情进展不顺利

java扩展了线程类

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;
    }

我认为对于您试图显示的图像,您的代码中没有持久性。代码在一个帧中看起来像它,并在下一帧中删除它。使用包含要绘制的所有图像的数组或列表,并在draw方法中循环绘制这些图像