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.onmeasuregets called during measurement of parent. gotmeasurespechow space @ can take (very summarized).given these specs, determine size of yourself, logically measuring own children , calling
setmeasureddimensiona 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,
viewdrawn,view.dispatchdrawbeing called , resulting simple viewsondraw. drawing means drawing children if you'reviewgroup.when drawing, system passes
canvasdimensions 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.getwidthorcanvas.getheightdimensions of screen, rather retrieve clipped dimensions (canvas.getclipbounds())and finally, when correctly retrieve clip bounds, should same width , height (
view.getwidthorview.getheight), might different, example if canvas scaled.
so summarize :
onmeasurepurpose, determine size of children,viewgroupcan compute they're dezired size.onlayoutpurpose affect width , height each view, propagating children.ondrawpurpose render view.
Comments
Post a Comment