java - How to add rectangles on top of existing rectangle in canvas -
i'm trying add red rectangles within existing canvas on top of specific boxes expected result image don't appear @ code shows current undesired outcome when deploy app. code create 4 rectangles on top row , 4 rectangles on bottom row want added on top of boxes 2-6 know code needs added red rectangles on top of boxes 1 & 7. know i'm doing wrong , how fix this? appreciated.
public class rectangletextview extends view { private final paint mblackpaint = new paint(); private final paint mredpaint = new paint(); private final textpaint mtextpaint; public rectangletextview(context context, attributeset attrs) { super(context, attrs); int valueindp = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, 1, getresources().getdisplaymetrics()); int valueinsp = (int) typedvalue.applydimension(typedvalue.complex_unit_sp, 20, getresources().getdisplaymetrics()); mredpaint.setcolor(color.parsecolor("#cc3333")); mblackpaint.setantialias(false); mblackpaint.setcolor(color.black); mblackpaint.setstrokewidth(valueindp); mblackpaint.setstyle(paint.style.stroke); mtextpaint = new textpaint(textpaint.anti_alias_flag); mtextpaint.setcolor(color.black); mtextpaint.settextalign(paint.align.center); mtextpaint.settextsize(valueinsp); mwindowpaint = new paint(); mwindowpaint.setcolor(color.parsecolor("#cc3333")); mwindowpaint.setstrokewidth(valueindp); } private paint mwindowpaint; @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (getwidth() == 0) return; //initialise red rectangles int w = canvas.getwidth(); int h = canvas.getheight(); int rectwidth = w / 5; int space = w / 15; int toprectheight = getpaddingtop(); int bottomrectheight = getpaddingbottom(); //draw end rectangles int msiderectwidth = 10; canvas.drawrect(0, 0, msiderectwidth, getheight(), mredpaint); //draw left end rectangle canvas.drawrect(getwidth() - msiderectwidth, 0, getwidth(), getheight(), mredpaint); //draw right end rectangle //draw grey boxes setbackgroundcolor(color.parsecolor("#808080")); int boxwidth = (getwidth() - msiderectwidth) / 7; //draw text views (int = 0; < 7; i++) { canvas.drawtext(integer.tostring(i + 1), (i * boxwidth + 10) + (boxwidth / 2), ((canvas.getheight() / 2) - ((mtextpaint.descent() + mtextpaint.ascent()) / 2)), mtextpaint); } //draw black lines (int = 1; < 7; i++) { canvas.drawline(msiderectwidth + boxwidth * i, 0, msiderectwidth + boxwidth * i, getheight(), mblackpaint); } //draw red windows (int = 0; < 4; i++) { mwindowpaint.setstyle(paint.style.stroke);//add int left = * (rectwidth + space); int right = left + rectwidth; if (i == 1){ mwindowpaint.setstyle(paint.style.fill); // change } rect rect = new rect(left, 0, right, toprectheight); canvas.drawrect(rect, mwindowpaint); rect rect2 = new rect(left, h - bottomrectheight, right, h); canvas.drawrect(rect2, mwindowpaint); } } }
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <com.apptacularapps.car.rectangletextview android:layout_width="100dp" android:layout_height="45dp" android:paddingtop="10dp" android:paddingbottom="10dp" android:background="#808080" android:gravity="center"/> </relativelayout>
mainactivity.java
import android.os.bundle; import android.support.v7.app.appcompatactivity; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
the problem created 4 rectangles in screen witdh size, not in number cell size. here code:
public class rectangletextview extends view { private final paint mblackpaint = new paint(); private final paint mredpaint = new paint(); private final textpaint mtextpaint; public rectangletextview(context context, attributeset attrs) { super(context, attrs); int valueindp = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, 1, getresources().getdisplaymetrics()); int valueinsp = (int) typedvalue.applydimension(typedvalue.complex_unit_sp, 20, getresources().getdisplaymetrics()); mredpaint.setcolor(color.parsecolor("#cc3333")); mblackpaint.setantialias(false); mblackpaint.setcolor(color.black); mblackpaint.setstrokewidth(valueindp); mblackpaint.setstyle(paint.style.stroke); mtextpaint = new textpaint(textpaint.anti_alias_flag); mtextpaint.setcolor(color.black); mtextpaint.settextalign(paint.align.center); mtextpaint.settextsize(valueinsp); mwindowpaint = new paint(); mwindowpaint.setcolor(color.parsecolor("#cc3333")); mwindowpaint.setstrokewidth(valueindp); } private paint mwindowpaint; rect rect = new rect(); rect rect2 = new rect(); @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (getwidth() == 0) return; //initialise red rectangles int w = canvas.getwidth(); int h = canvas.getheight(); int rectwidth = ((w - 20) / 7) / 5; int space = ((w - 20) / 7) / 15; int toprectheight = getpaddingtop(); int bottomrectheight = getpaddingbottom(); //draw end rectangles int msiderectwidth = 10; canvas.drawrect(0, 0, msiderectwidth, getheight(), mredpaint); //draw left end rectangle canvas.drawrect(getwidth() - msiderectwidth, 0, getwidth(), getheight(), mredpaint); //draw right end rectangle //draw grey boxes setbackgroundcolor(color.parsecolor("#808080")); int boxwidth = (getwidth() - msiderectwidth) / 7; //draw text views (int = 0; < 7; i++) { canvas.drawtext(integer.tostring(i + 1), (i * boxwidth + 10) + (boxwidth / 2), ((canvas.getheight() / 2) - ((mtextpaint.descent() + mtextpaint.ascent()) / 2)), mtextpaint); } //draw black lines (int = 1; < 7; i++) { canvas.drawline(msiderectwidth + boxwidth * i, 0, msiderectwidth + boxwidth * i, getheight(), mblackpaint); } //draw red windows (int index = 0; index < 7; index++) { if (index == 0 || index == 6) { (int = 0; < 3; i++) { mwindowpaint.setstyle(paint.style.stroke);//add int left = (i * (rectwidth + space)) + (index * boxwidth) + 13 + rectwidth/2 + space/2; int right = left + rectwidth; rect.set(left, 0, right, toprectheight); canvas.drawrect(rect, mwindowpaint); if (index == 0 && == 1) { mwindowpaint.setstyle(paint.style.fill); // change } rect2.set(left, h - bottomrectheight, right, h); canvas.drawrect(rect2, mwindowpaint); } } else { (int = 0; < 4; i++) { mwindowpaint.setstyle(paint.style.stroke);//add int left = (i * (rectwidth + space)) + (index * boxwidth) + 13; int right = left + rectwidth; rect.set(left, 0, right, toprectheight); canvas.drawrect(rect, mwindowpaint); rect2.set(left, h - bottomrectheight, right, h); canvas.drawrect(rect2, mwindowpaint); } } } } }
this full code working me. if have question or doubt feel free post :)
Comments
Post a Comment