我正试图将位图图片从某个点移动到我触摸的位置......但事情进展不顺利。
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 :(得分:0)
我认为您的代码中没有持久性存在您正在尝试显示的图像。代码在一个框架中看起来像它,并在下一个框架中删除它。使用一个数组或列表,其中包含您要绘制的所有图像,并在draw方法中循环显示。