2013-07-01 5 views
1

레이아웃 (ViewGallery라고 부름)을 수행하는 자체 뷰 하위 클래스가 있습니다. 내 문제는 수동으로 그리는 뷰가 화면에 표시되지 않는다는 것입니다. 여기에 onDraw 메서드가 있습니다.자식 뷰 그리기

@Override 
protected void onDraw(Canvas canvas) { 
    for(Child child : visibleChilds){ 
     canvas.save(); 
     canvas.clipRect(child.bounds); 
     child.view.draw(canvas); 
     canvas.restore(); 
    } 
} 

private List<Child> visibleChilds = new ArrayList<ViewGallery.Child>(); 

private static class Child { 
    private View view; 
    private Rect bounds; 

    public Child(View view, Rect rect) { 
     this.view = view; 
     bounds = rect; 
    } 
} 

필자는 지정된 클리핑 된 캔버스에서 내부 뷰를 그려야한다는 것을 알고 있습니다.

보기가 아직 비어있는 이유는 무엇입니까?

또한 ViewGroup을 확장하여 어댑터의 매개 변수로 전달했지만 기본 ViewGroup.LayoutParams에는보기 변환을 올바르게 처리해야하는 속성 (왼쪽 또는 x)이 없습니다. 하지만 하위 클래스를 만들 때 onDraw는 호출되지 않으며 자식은 여전히 ​​표시되지 않습니다.

+0

onDraw()가 아닌 children을 그리려면 dispatchDraw()를 재정의해야합니다. onDraw()에서 할 수 있지만 먼저 setWillNotDraw (false)를 호출해야합니다. ViewGroup.drawChild()를 사용하여 각 자식을 제대로 그려야합니다. –

+0

ViewGroup에서 재정의 할 dispatchDraw가 없으므로 setWillNotDraw 방식을 사용하려고합니다. 또한 ViewGroup이 addViews를 처리 할 것인지 또는이를 재정의해야합니까? –

+0

예 dispatchDraw() 메소드가 있습니다 : http://developer.android.com/reference/android/view/ViewGroup.html#dispatchDraw(android.graphics.Canvas) –

답변

0

질문을 올바르게 이해할 수 있을지 잘 모르겠습니다. 그러나 캔버스에서 뷰를 그리려면 드로잉 캐시를 활성화하고 비트 맵을 가져 와서 그려야합니다.

예 :

  // you have to enable setDrawingCacheEnabled, or the getDrawingCache will return null 
      view.setDrawingCacheEnabled(true); 

      // we need to setup how big the view should be..which is exactly as big as the canvas 
      view.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.AT_MOST)); 
      // assign the layout values to the textview 
      view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 


      mBitmap = view.getDrawingCache(); 
      canvas.drawBitmap(mBitmap, x, y, mPaint); 
      // disable drawing cache 
      view.setDrawingCacheEnabled(false); 

는 당연히이 경우에 그냥 지정된 위치에 그려진 뷰의 비트 맵 수 있습니다.