How to draw sharp curves and angled lines on Canvas in Android -


the image below shows output on canvas. curves , angled lines not sharp horizontal or vertical straight lines. have android:hardwareaccelerated="true" , anti_alias_flag on canvas well. please see code below. how can improve quality of these curves (same straight lines)? using nexus s. there way improve resolution of canvas.

screenshot of result

enter image description here

closeup of result

closeup of issue

path path1 = new path(); path path2 = new path(); public paint fillpaint = null; // called in constructor public void createpath() {     //path 1 samll 1     point[] araay = new point[]{new point(36,72),new point(29,46), new point(41,70),new point(42,54),new point(41,38),new point(29,64),new point(36,36)};     addbeziers(path1, araay, 72, 36);      addline(path1,72, 36 );     addline(path1, 72, 72 );      addline(path1, 36, 72 );      //path 2 big 1     araay = new point[]{new point(144,320),new point(109,200), new point(171,308),new point(178,240),new point(171,172),new point(109,282),new point(144,160)};     addbeziers(path2, araay, 320, 144);     addline(path2, 216, 144 );     addline(path2, 216, 216 );     addline(path2, 144, 320);      fillpaint = new paint(paint.anti_alias_flag);     fillpaint.setcolor(color.white);     fillpaint.setflags(paint.anti_alias_flag | paint.dither_flag);     fillpaint.setantialias(true);     fillpaint.setdither(true);     fillpaint.setstrokejoin(paint.join.round);     fillpaint.setstrokecap(paint.cap.round);     fillpaint.setstyle(paint.style.fill);  }   // add lines path protected path addline(path path, int endx, int endy) {     //path.moveto(startx, starty);      path.lineto(endx, endy);     return path; }  // add curves path protected path addbeziers(path path, point[] points, int lastx, int lasty) {      if (points[0].x != lastx && points[0].y != lasty)         path.moveto(points[0].x, points[0].y);      int index = 1;      path.cubicto(points[index].x, points[index].y, points[index + 1].x,         points[index + 1].y, points[index + 2].x, points[index + 2].y);     index = index + 3;     path.cubicto(points[index].x, points[index].y, points[index + 1].x,         points[index + 1].y, points[index + 2].x, points[index + 2].y);      return path; }  //draw on canvas @override public void ondraw(canvas canvas) {      canvas.drawpath(path1, fillpaint);     canvas.drawpath(path2, fillpaint);     super.ondraw(canvas); } 

enter image description here

is there way improve resolution of canvas.

you can create canvas bitmap of size like, draw contents using canvas, , draw bitmap initial canvas @ scaled resolution, in order fit contents on screen.

why shouldn't it

the screen of device, , screen in world, have square pixels in square grid (not worrying subpixels). means lines neither horizontal nor vertical have artifacts. using anti-aliasing, do, common technique mitigate visual artifacts, still there.

increasing resolution of canvas not in way, because single pixel on canvas mapped single pixel on device screen.

in opinion, you're worrying much. when viewing result on device screen @ regular eye-distance, unsharpness of edges not noticeable. when capture screenshot of device screen contents, , examine on larger screen, unsharpness of edges barely noticeable, , not ever worry about. when zoom in, can see it, you're no longer expecting sharp edges.


Comments