java - Android-Optimize line drawing with Canvas -
i drawing hexagonal grid on entire phone's screen. drawing hexagon drawing 6 lines using canvas
. many lines beingness drawn makes app unresponsive. had android:hardwareaccelerated=false
atleast create work on nexus 4 otherwise app crashed error :
06-22 14:11:46.664: a/libc(5743): fatal signal 6 (sigabrt) @ 0x0000166f (code=-6), thread 5743 (.nadeem.sensus4)
although app doesn't crash now, takes much time draw grid. code of customview draws grid :
public drawview(context context, hexagon hex) { super(context); this.context = context; setlayertype(view.layer_type_hardware, null); this.hex = hex; } @override public void ondraw(canvas canvas) { double xoff = math.cos(math.pi / 6) * hex.radius;//radius 12 double yoff = math.sin(math.pi / 6) * hex.radius; // 3rd of hex // height (int = 0; < 60; ++i) { (int j = 0; j < 40; ++j) { double xpos = j * xoff * 2; if (i % 2 != 0) { // if current line not xpos += xoff; // offset of half width on x axis } double ypos = * yoff * 3; createhexagon(xpos, // x pos hexagon center on scene ypos, canvas); } } } public void createhexagon(double x, double y, canvas canvas) { paint.setcolor(color.black); paint.setstyle(style.stroke); // paint.setstyle(style.fill); (int = 0; < 6; i++) { double angle = 2 * math.pi / 6 * (i + 0.5); double x_i = x + hex.radius * math.cos(angle); double y_i = y + hex.radius * math.sin(angle); if (i == 0) wallpath.moveto((float) x_i, (float) y_i); else wallpath.lineto((float) x_i, (float) y_i); } canvas.drawpath(wallpath, paint); canvas = null; }
i want inquire if there's way increment performance or other alternate way accomplish grid.
do drawing in layers. first time draw, draw hexagons single bitmap. in future draws, draw bitmap screen. add together in whatever else need draw on top of that. save 14000 draw line commands.
your other alternative move opengl drawing. there no way you'll ever 14k lines draw without hardware acceleration real speed.
java android android-canvas
No comments:
Post a Comment