如何画在画布上的另一个线程? [英] How to draw on Canvas in another thread?

查看:118
本文介绍了如何画在画布上的另一个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发应用程序,我需要drawingg在另一个线程中执行。现在我的code是:

I have been developing the application, and I need that drawingg is executed in another thread. Now my code is:

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那么应用程序可以在另一个线程画,但它不工作 - 当我画设备blinkes的屏幕。因此,请告诉我,我怎么能使用多线程使用SurfaceHolder等元素在画什么?我需要为我的code可能变化少。

I thought that if I uses ExecutorService then the application can draw in another thread, but it doesn't work - when I draw the screen of device blinkes. So, please, tell me, how can I use multithreading for drawing using SurfaceHolder and other elements? I need to make as few as possible changes in my code.

推荐答案

您只能在主UI线程绘制。您应该使用SurfaceView,因为它是专门为支持从辅助线程绘图。

You can only draw in the main UI thread. You should use SurfaceView, since it was made specifically to support drawing from secondary threads.

一个此类的目的之一是提供一种表面,其中一个
  辅助线程可以渲染到屏幕。如果你要使用
  这种方式,你需要了解一些线程语义。

One of the purposes of this class is to provide a surface in which a secondary thread can render into the screen. If you are going to use it this way, you need to be aware of some threading semantics.

也看到这部影片:了解Android的教程1.28-介绍SurfaceView

这篇关于如何画在画布上的另一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆