我使用異步任務在視圖上繪製位圖,但它沒有畫任何東西!
這是的AsyncTask代碼在AsyncTask中使用canvas.drawBitmap onPostExecute
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進階。
所以我怎樣才能防止再次運行的AsyncTask時無效的看法? –
對不起......我很明白你的意思,但我會更新我的答案,包括在幾分鐘內嘗試的代碼示例 – Kai
如果我使用invalidate,則清除該視圖並且必須再次使用canvas.DrawBtmap繪製位圖,是否正確? –