I want to draw a RelativeLayout
with all it's children to Canvas
.
This is what I been trying to do:
// from View constructor
container = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.containerLayout, null);
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
container.layout(0,0, w, h);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRGB(0, 0, 0);
container.draw(canvas);
}
I debug and saw that onSizeChanged
is called with good values. Also this code works for ImageView
, so how is RelativeLayout
different?
My problem is that it doesn't draw on screen, I don't seen any of its children. How to solve this?
Adding RelativeLAyout.mesure() solves the problem.
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
container.measure(w, h);
container.layout(0,0, w, h);
}