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. got measurespec 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 views ondraw. drawing means drawing children if you're viewgroup.

  • 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 use canvas.getwidth or canvas.getheight dimensions of screen, rather retrieve clipped dimensions (canvas.getclipbounds())

  • and finally, when correctly retrieve clip bounds, should same width , height (view.getwidth or view.getheight), might different, example if canvas scaled.

so summarize :

  1. onmeasure purpose, determine size of children, viewgroup can compute they're dezired size.

  2. onlayout purpose affect width , height each view, propagating children.

  3. ondraw purpose render view.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -