2013-07-27 75 views
1

我使用異步任務在視圖上繪製位圖,但它沒有畫任何東西!
這是的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進階。

回答

2

你說你正在繪製一個視圖,但是從你的代碼看,你沒有invalidate繪圖操作後的視圖。因此,您必須修改AsyncTask以獲取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(); 
     } 
    } 
} 
+0

所以我怎樣才能防止再次運行的AsyncTask時無效的看法? –

+0

對不起......我很明白你的意思,但我會更新我的答案,包括在幾分鐘內嘗試的代碼示例 – Kai

+0

如果我使用invalidate,則清除該視圖並且必須再次使用canvas.DrawBtmap繪製位圖,是否正確? –

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