I have a custom view that implements onMeasure
, onSizeChanged
and onDraw
.
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
Log.v(TAG, "Measures arrived "+xNew+","+yNew);
}
@Override
protected void onDraw(Canvas canvas) {
Log.v(TAG, "Time to draw, canvas size "+canvas.getWidth()+","+canvas.getHeight());
}
I add it to a container:
container.addView(customView, new LayoutParams(LayoutParams.MATCH_PARENT, 50)); // fixed height, 50px
My problem is, I don't understand why sometimes the Canvas
dimensions don't match the dimensions I get in onSizeChanged
once the view has been settled to its final size.
Specifically, in the use case proposed, and focusing only on height, i get:
- onMeasure: mode: EXACTLY, dimensions 50 (correct)
- onSizeChanged: size: 50 (correct)
- onDraw: canvas height: 730px (===screen height)
Why OnDraw's canvas height reports the whole screen? Is it maybe because higher in the hierarchy there may be a clipChildren(false)
?
Note: What I mean by "this happens sometimes" is that this view is a pretty general view (a custom textview
from scratch) that I use in very different places: inside scrollviews, inside adapters, deep-nested, etc..
canvas.getBounds()
is sometimes the appropriate method to call, but I've not tried it where some parent view hadclipChildren="false"
. You might consider storing the values passed toonSizeChanged()
and using those inonDraw()
. – Karakuricanvas_height=729, onSizeChanged_height=50, bounds Rect(0, 50 - 0, 50)
. (Interestingly, if the width is MATCH_PARENT, the clip region Width is 0) – rupps