2013-11-24 65 views
2

我的項目,我從網上下載的圖片,讓我實現了一個簡單的類吧:自定義繪製對象的draw()方法沒有被調用(安卓)

@SuppressWarnings("deprecation") 
public class DynamicDrawable extends BitmapDrawable{ 
    private Drawable drawable; 

    @Override 
    public void draw(Canvas canvas) { 
     // override the draw to facilitate refresh function later 
     if(drawable != null) { 
      drawable.draw(canvas); 
     } 
     Log.d("dnull", String.valueOf(drawable == null)); 
    } 
    protected void setDrawable(Drawable drawable){ 
     this.drawable = drawable; 
    } 
} 

有一個處理程序,獲取並解析圖像,將其異步添加到類,並使視圖無效,我已檢查並正常工作。 drawable變量不爲null。 然後將它添加到ImageView。但是,draw()方法永遠不會被調用。甚至不是在第一次添加時。這裏是圖像進入視圖代碼:

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
View view = convertView; 

if(view == null){ 
    view = inflater.inflate(R.layout.view_concerteposter, parent,false); 
} 

ConcertPoster poster = posters.get(position); 

ImageView iconView = (ImageView)view.findViewById(R.id.ticketBuy_icon); 
TextView titleView = (TextView)view.findViewById(R.id.ticketBuy_name); 
TextView dateView = (TextView)view.findViewById(R.id.ticketBuy_date); 

iconView.setImageDrawable(poster.getImage()); 
System.out.println(poster.getImage()); 
titleView.setText(poster.getTitle()); 
dateView.setText(poster.getDate()); 

return view; 
} 

是的,我已經檢查的對象,他們是正確的,並在他們正確的繪圖資源。

任何幫助,將不勝感激。

+0

看來你期望的draw()方法來調用setWillNotDraw(假)上的自定義視圖的構造當你setImageDrawable()自動調用,但你爲什麼期望?該文檔沒有說明這是預期的行爲。 – mttdbrd

+0

它發生在常規drawable中。另外,它不能被手動調用,因爲Canvas對象被系統持有並且無法獲得。 – Alex

+0

該文檔說:「手動將此視圖(及其所有子項)呈現給定畫布。」據我所知,你應該畫一個你提供的畫布。我要去測試一下,並讓你知道我找到了什麼。 – mttdbrd

回答

0

在convertView的onDraw()方法中,您應手動調用Drawable的draw()方法。

protected void onDraw(Canvas canvas) { 
mDrawable.draw(canvas); 
} 

有關更多信息,請參閱here

相關問題