java - Creating five rectangles with android UI canvas -
i new android programming , trying create 5 rectangles(two on right side of screen , 3 on left side of screen) on android using canvas drawrect method, shows top left rectangle only. doing wrong here? help.
here's rectangle class:
public class rectangle extends view { public rectangle(context context) { super(context); setfocusable(true); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); float x = getwidth(); float y = getheight(); paint topleftrect = new paint(); paint bottomleftrect = new paint(); paint toprightrect = new paint(); paint midrightrect = new paint(); paint bottomrightrect = new paint(); // draw top left rectangle topleftrect.setstyle(paint.style.fill); //topleftrect.setcolor(color.white); topleftrect.setcolor(color.parsecolor("#ffff99")); // canvas.drawpaint(topleftrect); canvas.drawrect(0, 0, x / 2, y / 2, topleftrect); //draw bottom left rectangle bottomleftrect.setstyle(paint.style.fill); //bottomleftrect.setcolor(color.white); // canvas.drawpaint(bottomleftrect); bottomleftrect.setcolor(color.parsecolor("#ffff99")); canvas.drawrect(0, y / 2, x / 2, 0, bottomleftrect); //draw top tight rectangle toprightrect.setstyle(paint.style.fill); //toprightrect.setcolor(color.white); toprightrect.setcolor(color.parsecolor("#ff6600")); //canvas.drawpaint(toprightrect); canvas.drawrect(x / 2, 0, 0, y / 3, toprightrect); // draw middle right rectangle midrightrect.setstyle(paint.style.fill); //midrightrect.setcolor(color.white); midrightrect.setcolor(color.parsecolor("#66ffff")); // canvas.drawpaint(midrightrect); canvas.drawrect(x / 2, 0, 0, y / 3, midrightrect); //draw bottom right rectangle bottomrightrect.setstyle(paint.style.fill); //bottomrightrect.setcolor(color.white); bottomrightrect.setcolor(color.parsecolor("#cccc00")); // canvas.drawpaint(bottomrightrect); canvas.drawrect(x/2, y/3, 0, 0, bottomrightrect); } }
here's main activity:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new rectangle(this)); //setcontentview(r.layout.activity_main); }
here's image of emulator. getting 1 colored rectangle , should 4 more on bottom left, top right, middle right, , bottom right of screen.
public class rectangle extends view { public rectangle(context context) { super(context); setfocusable(true); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); float x = getwidth(); float y = getheight(); paint topleftrect = new paint(); paint bottomleftrect = new paint(); paint toprightrect = new paint(); paint midrightrect = new paint(); paint bottomrightrect = new paint(); // draw top left rectangle topleftrect.setstyle(paint.style.fill); //topleftrect.setcolor(color.white); topleftrect.setcolor(color.parsecolor("#ffff99")); // canvas.drawpaint(topleftrect); canvas.drawrect(0, 0, x / 2, y / 2, topleftrect); //draw bottom left rectangle bottomleftrect.setstyle(paint.style.fill); //bottomleftrect.setcolor(color.white); // canvas.drawpaint(bottomleftrect); bottomleftrect.setcolor(color.parsecolor("#ff5599")); canvas.drawrect(0, y / 2, x / 2, y, bottomleftrect); //draw top tight rectangle toprightrect.setstyle(paint.style.fill); //toprightrect.setcolor(color.white); toprightrect.setcolor(color.parsecolor("#ff6600")); //canvas.drawpaint(toprightrect); canvas.drawrect(x / 2, 0, x, y / 3, toprightrect); // draw middle right rectangle midrightrect.setstyle(paint.style.fill); //midrightrect.setcolor(color.white); midrightrect.setcolor(color.parsecolor("#66ffff")); // canvas.drawpaint(midrightrect); canvas.drawrect(x / 2, y/3, x, 2*y / 3, midrightrect); //draw bottom right rectangle bottomrightrect.setstyle(paint.style.fill); //bottomrightrect.setcolor(color.white); bottomrightrect.setcolor(color.parsecolor("#cccc00")); // canvas.drawpaint(bottomrightrect); canvas.drawrect(x/2, 2*y/3, x, y, bottomrightrect); } }
Comments
Post a Comment