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