在AsyncTask onPostExecute中使用canvas.drawBitmap

时间:2013-07-27 10:06:03

标签: android performance android-asynctask android-canvas

我使用异步任务在视图上绘制位图,但它什么都没画! 这是asynctask代码

class BitmapWorker extends AsyncTask<String, Void, Void>
{

    private Canvas canvas;
    private Rect rcText;
    private Paint paint;
    private Options options;
    private Options opt;
    public BitmapWorker(Canvas canvas,Rect rcText,Paint paint)
    {
        this.canvas = canvas;
        this.rcText = rcText;//the bitmap must draw on it's rect
        this.paint = paint;
    }

    @Override
    protected Void doInBackground(String... params)
    {
        options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(m_AttachSource, options);
        opt = new Options();
        opt.inPurgeable = true;
        opt.inSampleSize = calculateInSampleSize(options, INSAMPLESIZE_THIMBPIC_WIDTH, INSAMPLESIZE_THIMBPIC_HEIGHT);

        LoadThumbPic(m_AttachSource, opt);
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        Boolean hasBitmap = false;
        while(!hasBitmap)
        {
            if(m_PictureMessageTumbPic.get() != null)
            {
                canvas.drawBitmap(m_PictureMessageTumbPic.get(), null, rcText, paint);
                hasBitmap = true;
            }
            else
            {
                Options opt = new Options();
                opt.inPurgeable = true;
                opt.inSampleSize = calculateInSampleSize(options, INSAMPLESIZE_THIMBPIC_WIDTH, INSAMPLESIZE_THIMBPIC_HEIGHT);
                LoadThumbPic(m_AttachSource, opt);
                canvas.drawBitmap(m_PictureMessageTumbPic.get(), null, rcText, paint);
                hasBitmap = true;
            }
        }
    }
}

tnx 4 adv。

2 个答案:

答案 0 :(得分:2)

您说您正在绘制视图,但是从您的代码中可以看出,绘图操作后您没有invalidate视图。因此,您必须修改AsyncTask以接纳View,并在更新Canvas后调用其invalidate()方法。

请记住,现代操作系统会缓存图形元素以提高性能,因此您必须使用它提供的机制来通知它更新是有序的。

试试这个(没有运行代码,可能有愚蠢的错误):

class BitmapWorker extends AsyncTask<String, Void, Void>
{

    private Canvas canvas;
    private Rect rcText;
    private Paint paint;
    private Options options;
    private Options opt;
    private View view;
    public BitmapWorker(Canvas canvas,Rect rcText,Paint paint, View view)
    {
        this.canvas = canvas;
        this.rcText = rcText;//the bitmap must draw on it's rect
        this.paint = paint;
        this.view = view;
    }

    @Override
    protected Void doInBackground(String... params)
    {
        options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(m_AttachSource, options);
        opt = new Options();
        opt.inPurgeable = true;
        opt.inSampleSize = calculateInSampleSize(options, INSAMPLESIZE_THIMBPIC_WIDTH, INSAMPLESIZE_THIMBPIC_HEIGHT);

        LoadThumbPic(m_AttachSource, opt);
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        Boolean hasBitmap = false;
        while(!hasBitmap)
        {
            if(m_PictureMessageTumbPic.get() != null)
            {
                canvas.drawBitmap(m_PictureMessageTumbPic.get(), null, rcText, paint);
                hasBitmap = true;
            }
            else
            {
                Options opt = new Options();
                opt.inPurgeable = true;
                opt.inSampleSize = calculateInSampleSize(options, INSAMPLESIZE_THIMBPIC_WIDTH, INSAMPLESIZE_THIMBPIC_HEIGHT);
                LoadThumbPic(m_AttachSource, opt);
                canvas.drawBitmap(m_PictureMessageTumbPic.get(), null, rcText, paint);
                hasBitmap = true;
            }
        }

        if(hasBitmap) {
            view.invalidate();
        }
    }
}

答案 1 :(得分:1)

使用onPostExecute方法进行此操作:

  • 获得表面支架。
  • 从持有人初始化画布。
  • 让持有人解锁你的画布并张贴画布。
    
protected void onPostExecute(Bitmap result) {

    SurfaceHolder holder = getSurfaceHolder();
    Canvas canvas = null;

    try {
        canvas = holder.lockCanvas();
        if (canvas != null) {
            canvas.drawBitmap(result, 50, 50, paint);
        }

    } finally {
        if (canvas != null)
            holder.unlockCanvasAndPost(canvas);
    }
}