在AsyncTask onPostExecute中使用canvas.drawBitmap [英] use canvas.drawBitmap in AsyncTask onPostExecute

查看:82
本文介绍了在AsyncTask onPostExecute中使用canvas.drawBitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i use async task to draw bitmap on view,but it's draw nothing!
this is the asynctask code

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.

tnx 4 adv.

推荐答案

您说的是绘制视图,但是从您的代码来看,您似乎没有

You said that you are drawing to a view, but from your code, it appears that you have not invalidate the view after the drawing operation. So you have to modify the AsyncTask to take in the View, and call its invalidate() method after you've updated the Canvas.

请记住,现代OS会缓存图形元素以提高性能,因此您必须使用它提供的机制来通知它更新已到.

Remember that modern OS caches graphics elements to improve performance, so you have to use the mechanism it provided to notify it that an update is in order.

尝试一下(未运行代码,可能有愚蠢的错误):

Try this (have not run the code, may have silly errors):

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();
        }
    }
}

这篇关于在AsyncTask onPostExecute中使用canvas.drawBitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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