View的onDraw函數

自己定義一個View,重寫onDraw方法(畫一個紅色的圓),然後add到某一個holder中,結果顯示不出來。

顯然是onDraw函數沒有調用。


1.  手動觸發

onDraw是一個觸發事件產生的調用,一般只能通過特定的方法觸發調用:invalidate和postInvalidate


2. 可以在構造函數裏設置該view的backgroundcolor,即setBackgroundColor(Color.rgb(0, 255, 0)); 會自動觸發onDraw函數

3. 使用setWillNotDraw(false),即不攔截onDraw函數的調用

public void setWillNotDraw (boolean willNotDraw)
Since: API Level 1
If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you overrideonDraw(Canvas) you should clear this flag.
willNotDraw whether or not this View draw on its own


參考

http://blog.sina.com.cn/s/blog_4b50130d0100u0uk.html
http://blog.csdn.net/hearrt/article/details/7001358


發佈了66 篇原創文章 · 獲贊 16 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章