Android View
有三個版本invalidate()
:一個使整個視圖失效,另外兩個只使其一部分失效。但它只有一個onDraw()
,它繪製整個畫布。系統必須有一些使用暗示我只想使部分視圖無效,但我不清楚它是什麼。android.graphics.View無效(int,int,int,int)畫布的一部分,但onRedraw()整個畫布?
我有一個視圖可以在onDraw()
中進行自定義繪圖。我有辦法找出畫布的哪些部分是無效的,所以我只畫這些?
Android View
有三個版本invalidate()
:一個使整個視圖失效,另外兩個只使其一部分失效。但它只有一個onDraw()
,它繪製整個畫布。系統必須有一些使用暗示我只想使部分視圖無效,但我不清楚它是什麼。android.graphics.View無效(int,int,int,int)畫布的一部分,但onRedraw()整個畫布?
我有一個視圖可以在onDraw()
中進行自定義繪圖。我有辦法找出畫布的哪些部分是無效的,所以我只畫這些?
當的Android已經準備好了渲染的變化屏幕,它通過創建屏幕上所有需要重繪的單個矩形區域的聯合(所有已被無效的區域)來實現。
當您調用視圖的onDraw(Canvas canvas)方法時,您可以檢查Canvas是否有剪輯邊界。
如果存在非空剪輯邊界,則可以使用此信息來確定您將要和不需要繪製的內容,從而節省時間。
如果剪輯邊界爲空,則應該假定Android要求您繪製視圖的整個區域。
事情是這樣的:
private Rect clipBounds = new Rect();
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
boolean isClipped = canvas.getClipBounds(clipBounds);
// If isClipped == false, assume you have to draw everything
// If isClipped == true, check to see if the thing you are going to draw is within clipBounds, else don't draw it
}
There must be some use that the system makes of the hint that I only want to
invalidate part of the view, but I'm unclear on what it is.
是的。方法invalidate(int l,int t,int r,int b)有四個參數供視圖的父視圖用來計算mLocalDirtyRect,它是View類的一個字段。而mLocalDirtyRect由View類的getHardwareLayer()方法時使用,這裏是它的描述:
/**
* <p>Returns a hardware layer that can be used to draw this view again
* without executing its draw method.</p>
*
* @return A HardwareLayer ready to render, or null if an error occurred.
*/
HardwareLayer getHardwareLayer() {
表示Android可以刷新不調用視圖的OnDraw()方法,您的視圖的一部分。所以你不需要自己嘗試繪製視圖的一部分,因爲當你告訴視圖的髒部分時,Android會爲你做。
最後,我想你可以參考查看和ViewGroup中的源代碼的更多細節,這裏要說的是,你可以在網上閱讀鏈接: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/View.java https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/ViewGroup.java