본문 바로가기

Android/dispatchDraw()

Android ViewGroup.dispatchDraw()

ViewGroup.dispatchDraw()

ViewGroup에 포함되어 있는 이 메소드는 ViewGroup이 다시 그려져야 할 경우에 자동으로 호출된다.
View에 포함된 onDraw()는 해당 View가 다시 그려져야 할 경우에 자동으로 호출된다.
ViewGroup도 View에서 파생되기는 했지만 View처럼 다시 그려져야 할 경우에 onDraw()가 호출되지 않고 dispatchDraw()가 호출되도록 설계되었다. 그러므로 ViewGroup.invalidate()를 호출하면 이어서 dispatchDraw()가 호출된다.

ViewGroup안에 포함되어 있는 자식 View들이 다시 그려져야 할 경우에 dispatchDraw()가 호출되어 모든 자식 View가 다시 그려지도록 되어 있다.

ViewGroup안에 포함된 onDraw()가 호출되도록 설정하려면 setWillNotDrawEnabled(false); 메소드를 호출해주면 된다.

 dispatchDraw()를 오버라이드할 경우에는 다음과 같은 사항에 유의해야 한다.
 다음과 같이 오버라드하면 ViewGroup내의 모든 자식 View들이 모두 그려진 후에 개발자가 그리는 내용이 그 위에 그려진다.
 반대로 super.dispatchDraw(canvas)를 개발자의 그리는 작업 후에 호출하면 그려진 그림 위에 자식 View가 나타난다.

@Override
 protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    /*
    * canvas를 이용하여 그리는 작업
    */
  }

 

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 override onDraw(Canvas) you should clear this flag.

 

 Overrides: dispatchDraw(Canvas canvas) in ViewGroup
protected void dispatchDraw (Canvas canvas)
Since: API Level 1
Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

 

void android.view.View.invalidate()

public void invalidate ()
Since: API Level 1
Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().