android - using custom view "SignatureView" to capture user signature -
im trying have signatureview capture user's signature after fill form, codes not making draw. found drawing codes on youtube , tried them not draw anything, although scroll lock working. following code
the xml signatureview
<signatureview android:background="#ffffff" android:layout_width="match_parent" android:layout_height="400dp" android:id="@+id/signatureview"/>
the code signatureview enable drawing
public class signatureview extends view { public static int brush_size = 5; public static final int default_color = color.black; public static final int defaul_bg_color = color.white; private static final float touch_tolerance = 4; private float mx, my; private path mpath; private paint mpaint; private arraylist<fingerpath> paths = new arraylist<>(); private int currentcolor; private int backgroundcolor = defaul_bg_color; private int strokewidth; private bitmap mbitmap; private canvas mcanvas; private paint mbitmappaint = new paint(paint.dither_flag); public signatureview(context context){ this(context, null); } public signatureview(context context, attributeset attrs){ super(context, attrs); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(default_color); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setxfermode(null); mpaint.setalpha(0xff); } public void init(displaymetrics metrics){ int height = metrics.heightpixels; int width = metrics.widthpixels; mbitmap = bitmap.createbitmap(width, height, bitmap.config.argb_8888); mcanvas = new canvas(mbitmap); currentcolor = default_color; strokewidth = brush_size; } protected void ondraw(canvas canvas){ canvas.save(); mcanvas.drawcolor(backgroundcolor); (fingerpath fp : paths){ mpaint.setcolor(fp.color); mpaint.setstrokewidth(fp.strokewidth); } canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); canvas.restore(); } private void touchstart(float x, float y){ mpath = new path(); fingerpath fp = new fingerpath(currentcolor, strokewidth, mpath); paths.add(fp); mpath.reset(); mpath.moveto(x, y); mx = x; = y; } private void touchmove(float x, float y){ float dx = math.abs(x - mx); float dy = math.abs(y - my); if (dx >= touch_tolerance || dy >= touch_tolerance){ mpath.quadto(mx, my, (x + mx) / 2, (y + my) /2); mx = x; = y; } } private void touchup(){ mpath.lineto(mx, my); } @override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); getparent().requestdisallowintercepttouchevent(true); switch(event.getaction()){ case motionevent.action_down : touchstart(x, y); invalidate(); break; case motionevent.action_move : touchmove(x, y); invalidate(); break; case motionevent.action_up : touchup(); invalidate(); break; } return true; }
the fingerpath class
public class fingerpath { public int color; public int strokewidth; public path path; public fingerpath(int color, int strokewidth, path path){ this.color = color; this.strokewidth = strokewidth; this.path = path; } }
the java class page
public class application_applicant extends appcompatactivity { private signatureview sv; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_app_applicant); sv = (signatureview)findviewbyid(r.id.signatureview); displaymetrics metrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(metrics); sv.init(metrics); } }
please have myview
class in apis demo of android link: https://android.googlesource.com/platform/development/+/master/samples/apidemos/src/com/example/android/apis/graphics/fingerpaint.java
you can find demos in project. it's useful
Comments
Post a Comment