我一直在开发应用程序,我需要在另一个线程中执行绘图。现在我的代码是:
public class PainterView extends View implements DrawingListener {
//private GestureDetector detector;
private Context context;
private Painter painter;
private Bitmap background;
private Bitmap bitmap;
private Paint bitmapPaint;
private Path path;
private Paint paint;
private float x;
private float y;
private boolean isErasing=false;
private boolean isTextDrawing=false;
private ExecutorService pool;
public PainterView(Context context, Painter painter) {
super(context);
this.context = context;
this.painter = painter;
pool=Executors.newFixedThreadPool(3);
//detector = new GestureDetector(context, new GestureListener());
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(final Canvas canvas) {
if (bitmap != null) {
pool.submit(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (PainterView.this) {
canvas.drawBitmap(background, 0, 0, bitmapPaint);
canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
canvas.drawPath(path, paint);
}
}
});
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//detector.onTouchEvent(event);
x = event.getX();
y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
painter.touchStart(x, y);
break;
case MotionEvent.ACTION_MOVE:
painter.touchMove(x, y);
break;
case MotionEvent.ACTION_UP:
painter.touchUp();
break;
}
return true;
}
@Override
public void onPictureUpdate(Bitmap background, Bitmap bitmap, Paint bitmapPaint, Path path, Paint paint) {
this.background=background;
this.bitmap = bitmap;
this.bitmapPaint = bitmapPaint;
this.path = path;
this.paint = paint;
invalidate();
}
public void setPainter(Painter painter) {
this.painter = painter;
}
}
我认为如果我使用 ExecutorService 那么应用程序可以在另一个线程中绘制,但它不起作用 - 当我绘制设备的屏幕时会闪烁。那么,请告诉我,如何使用多线程使用 SurfaceHolder 和其他元素进行绘图?我需要对我的代码进行尽可能少的更改。