Android onMeasure and draw() -
i have strange problems.
first problem follow: read how override onmeasure(). did it. expecting when set width/height in onmeasure same in ondraw, apparently not true. except measuredwidth same width in draw phase, again isn't true.
onmeasure called many times. on each call invoke setmeasureddimension calculated width , height. example, first time view should 480 x 245, on second call recalculate again, based on parent of course, , should 90 x 245.
for great surprise android somehow ignoring first call. in case final view size 480 x 245.
second issue follow: view height match_parent, parent height 0. how supposed set right height on onmeasure when don't know ?
any idea how make android not ignore setmeasuredimensions calls , how set match_parent ?
the space that's allocated view doesn't depend on size measured, here's snapshot of process :
view.onmeasure
gets called during measurement of parent. gotmeasurespec
how space @ can take (very summarized).given these specs, determine size of yourself, logically measuring own children , calling
setmeasureddimension
a while after, parent assigns concrete dimensions, based on measured (but means can different). these dimensions, that's 1 have use. callback called @ point
onlayout
, , shall layout children in process, based on dimensions affected you.after this,
view
drawn,view.dispatchdraw
being called , resulting simple viewsondraw
. drawing means drawing children if you'reviewgroup
.when drawing, system passes
canvas
dimensions these of screen, , using translation , clipping, canvas passed along views draw themselves. avoids allocation during draw. reason, if want know space dedicated you, should not usecanvas.getwidth
orcanvas.getheight
dimensions of screen, rather retrieve clipped dimensions (canvas.getclipbounds()
)and finally, when correctly retrieve clip bounds, should same width , height (
view.getwidth
orview.getheight
), might different, example if canvas scaled.
so summarize :
onmeasure
purpose, determine size of children,viewgroup
can compute they're dezired size.onlayout
purpose affect width , height each view, propagating children.ondraw
purpose render view.
Comments
Post a Comment