java - Calculate width of Views so that they always fit on screen -
i need dynamically calculate width of bars in diagram drawing.
the minimum width has 4 pixels , maximum 50 pixels.
how calculate width when need more bars dynamically set width , fit on screen?
right hardcoded either 4 or 50 pixels if suggest thankful.
thanks in advance!
edit 1
public void drawbars(int numberofrates) { this.removeallviews(); final viewgroup nullparent = null; this.addview(minflater.inflate(r.layout.layout_bar_diagram, nullparent)); linearlayout parentview = (linearlayout) findviewbyid(r.id.parentview); int width, height, horizontalmargin, verticalmargin, textbottommargin; verticalmargin = calculatedpi(16); if (((numberofrates * calculatedpi(50)) + (numberofrates * calculatedpi(5))) < displaymetrics.widthpixels) { width = calculatedpi(50); horizontalmargin = calculatedpi(5); } else { width = 0; horizontalmargin = 0; } (int = 0; < numberofrates; i++) { if (i == 0) { textbottommargin = 0; height = calculatedpi(50); } else if (i == (numberofrates - 1)) { textbottommargin = calculatedpi(16); height = calculatedpi(150); } else { textbottommargin = calculatedpi(16); height = calculatedpi(100); } textbarview textbarview = new textbarview(getcontext()); textbarview.drawbar(i + 1, width, height, horizontalmargin, verticalmargin, textbottommargin); parentview.addview(textbarview); } invalidate(); }
if have many views
not idea add them viewgroup
. recommend draw widgets on canvas
.
public class customdiagram extends view { private int numberofitems; public customdiagram(context context) { super(context); } public customdiagram(context context, attributeset attrs) { super(context, attrs); } public customdiagram(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); int width = getmeasuredwidth(); // here have full width of view // have numberofitems here } }
Comments
Post a Comment