1

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

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 高级版

4

2 回答 2

2

您说您正在绘制到视图,但从您的代码看来,您在绘制操作后并没有使视图无效。因此,您必须修改AsyncTask以获取, 并在更新 Canvas 后View调用其方法。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();
        }
    }
}
于 2013-07-27T11:35:33.570 回答
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);
    }
}
于 2014-07-26T19:29:12.197 回答