android - How do you set the size and position of a ViewGroup that is inside a RelativeLayout? (All Java, no XML) -
i've been trying understand how draw graphics on android using hierarchy of custom views, custom viewgroups, , relativelayouts. i'm having hard time figuring out how have parents tell children position , @ size. think working when custom viewgroup tells children size, can't figure out how have relativelayout tell viewgroup children size , position be. right now, following code draws circle in top , center of screen. (this android 3.0 on xoom). size of circle 295x295, i'm assuming somehow default size , position of layout? can give me code needed programatically pick size , location of viewgroup contains view contains circle? edges of circle on 4 sides getting trimmed off so, know relativelayout cutting off rest of viewgroup printing. if have viewgroup measure() view @ smaller size, print @ size in left corner of same rectangle in top center of screen.
my activity's oncreate:
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); customviewgroup custvg = new customviewgroup(this); relativelayout rel = new relativelayout(getapplicationcontext()); rel.setlayoutparams(new layoutparams(layoutparams.fill_parent, layoutparams.fill_parent)); rel.addview(custvg); relativelayout.layoutparams params2 = new relativelayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); params2.addrule(relativelayout.align_parent_right); rel.addview(new customviewgroup(getapplicationcontext()),1, new viewgroup.layoutparams(layoutparams.match_parent,layoutparams.match_parent)); setcontentview(rel); }
my custom made viewgroup:
customview cust; int _height; int _width; public customviewgroup(context context) { super(context); cust = new customview(context); canvas canvas = new canvas(); this.addview(cust,new relativelayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent)); } @override public void ondraw(canvas canvas){ this.drawchild(canvas,cust,0); } @override protected void onlayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { cust.layout(this.getleft(), this.gettop(), this.getright(), this.getbottom()); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec){ //measure child this.measurechild(cust,widthmeasurespec, heightmeasurespec); _height = view.measurespec.getsize(heightmeasurespec); _width = view.measurespec.getsize(widthmeasurespec); setmeasureddimension(_height, _width); }
my custom view
public customview(context context){ super(context); } @override public void ondraw(canvas canvas) { float noteradius = _width/2; paint paint = new paint(); paint.setantialias(true); paint.setcolor(color.yellow); paint.setstyle(paint.style.stroke); float strokewidth = paint.getstrokewidth(); paint.setstrokewidth(strokewidth); canvas.drawcircle(_height/2, _width/2, noteradius, paint); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { _height = view.measurespec.getsize(heightmeasurespec); _width = view.measurespec.getsize(widthmeasurespec); log.i("customview onmeasure","width: " + _width + " height: " + _height); setmeasureddimension(_height, _width); }
sorry long. thanks!
okay - figured out problem. 1) missing <uses-sdk android:minsdkversion="11" />
line in manifest wasn't filling screen of xoom.
2) didn't realize don't have use match_parent or wrap_content layout params. instance following create circle of size 500x500 , put in bottom left of screen (it goes off screen expected).
relativelayout.layoutparams params2 = new relativelayout.layoutparams(500, 500); params2.leftmargin = 0; params2.topmargin = 200; rel.addview(new customviewgroup(this),1,params2);
Comments
Post a Comment