android - measure all child views of RelativeView before drawing -


i have method, should create bitmap relativelayout. relativelayout creates dynamically , place input views circle. looks this:

public class circleview extends relativelayout { static final int centerid = 111; private final int radius; bitmap returnedbitmap; private relativelayout.layoutparams createnewrelativelayoutparams() {           relativelayout.layoutparams lp = new relativelayout.layoutparams(                              relativelayout.layoutparams.wrap_content,                              relativelayout.layoutparams.wrap_content);           lp.addrule(relativelayout.above, centerid);           lp.addrule(right_of, centerid);           return lp; }  private view prepareelementforcircle(view elem, int distx, int disty) {           relativelayout.layoutparams lp = createnewrelativelayoutparams();            elem.measure(0, 0);           int deltax = elem.getmeasuredwidth() / 2;           int deltay = elem.getmeasuredheight() / 2;           lp.setmargins(distx - deltax, 0, 0, radius - disty - deltay);           elem.setlayoutparams(lp);           return elem; }  public circleview(context context, int radius, view[] elements) {           super(context);           this.radius = radius;            relativelayout.layoutparams lpview = new relativelayout.layoutparams(                              relativelayout.layoutparams.fill_parent,                              relativelayout.layoutparams.fill_parent);           this.setlayoutparams(lpview);            view center = new view(context);           center.setid(centerid);           relativelayout.layoutparams lpcenter = new relativelayout.layoutparams(                              0, 0);           lpcenter.addrule(center_horizontal);           lpcenter.addrule(center_vertical);           center.setlayoutparams(lpcenter);           this.addview(center);            this.addview(prepareelementforcircle(elements[0], 0, 0));           if (elements.length % 2 == 0) {                    this.addview(prepareelementforcircle(elements[elements.length / 2],                                       0, 2 * radius));           }           if (elements.length > 2) {                    (int = 1; <= (elements.length - 1) / 2; i++) {                              int y = * 4 * radius / elements.length;                              int x = (int) math.sqrt(math.pow(radius, 2)                                                - math.pow((radius - y), 2));                              this.addview(prepareelementforcircle(elements[i], x, y));                              this.addview(prepareelementforcircle(elements[elements.length                                                - i], -x, y));                    }           } } 

when pass several views circleview , setcontentview of it, works fine. need implement ability rotate circleview. need convert relativelayout bitmap. this:

  public  bitmap getbitmapfromview() {      this.measure(measurespec.makemeasurespec(createnewrelativelayoutparams().width, measurespec.unspecified),             measurespec.makemeasurespec(createnewrelativelayoutparams().height, measurespec.unspecified));     this.layout(0, 0, this.getmeasuredwidth(), this.getmeasuredheight());     this.setgravity(gravity.center);     bitmap returnedbitmap = bitmap.createbitmap(this.getmeasuredwidth(), this.getmeasuredheight(), bitmap.config.argb_8888);     canvas c = new canvas(returnedbitmap);     this.draw(c);     return returnedbitmap; } 

as result i've got view, looks ugly. not every view displays , not in circle. guess measuring of view acts not should be. maybe, should measure children? or maybe call bitmap convertion method in onpredraw()? i've tried both of solutions didn't work me.

i solved

 public  bitmap getbitmapfromview() {         displaymetrics dm = context.getapplicationcontext().getresources().getdisplaymetrics();          this.measure(measurespec.makemeasurespec(dm.widthpixels, measurespec.exactly),                 measurespec.makemeasurespec(dm.heightpixels, measurespec.exactly));         this.layout(0, 0, this.getmeasuredwidth(), this.getmeasuredheight());         this.setgravity(gravity.center);         bitmap returnedbitmap = bitmap.createbitmap(this.getmeasuredwidth(),                 this.getmeasuredheight(), bitmap.config.argb_8888);         canvas c = new canvas(returnedbitmap);         this.draw(c);         return returnedbitmap;     }  

so displaymetrics solved problem.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

java - Using an Integer ArrayList in Android -