Android 开发新手,我的应用是在画布上绘制路径(具有位图),从颜色选择器中选择多种颜色。先前绘制的路径颜色正在使用画布上的新路径颜色进行更新。任何帮助我,这是我的代码......
@Override
public void run() {
// TODO Auto-generated method stub
while (locker) {
canvas = null;
try {
if (!holder.getSurface().isValid()) {
continue;
}
canvas = holder.lockCanvas();
draw(canvas);
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// synchronized (thread.getSurfaceHolder()) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path = new Path();
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
path.lineTo(event.getX(), event.getY());
paths.add(path);
}
return true;
// }
}
/**
* This method deals with paint-works. Also will paint something in
* background
*/
private void draw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bell);
// canvas.drawColor(Color.CYAN);
canvas.drawBitmap(bitmap, 20, 20, mPaint);
// canvas.drawPath(mPath, mPaint);
for (Path path : paths) {
// canvas.drawPoint(graphic.x, graphic.y, mPaint);
canvas.drawPath(path, mPaint);
}
}
}