1

I has my own View subclass that does Layout (I call it ViewGallery), my problem is that views that I draw manually wont appear at screen, here's it onDraw method.

@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;
    }
}

As far as I know that should draw the inner view in the specified clipped Canvas.

Why the view is still empty?

Also I tried extends ViewGroup so I pass itself as parameter in a Adapter, but default ViewGroup.LayoutParams doesn't has left (or x) properties that I need to handle properly translation of views. But when subclassing it the onDraw never get called and childs still wont appear.

4

1 回答 1

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);

当然,在这种情况下,它只是在给定位置绘制的视图的位图。

于 2013-07-02T02:17:29.087 回答