Android实现手写签名
本文实例为大家分享了Android手写签名的实现方法,产品要求用户可以在app上签协议。。所以得弄个手写签名版,参考了一些资料自己写了个PaintView去继承View,实现签名功能。
packagecom.****.*****.widget; importandroid.content.Context; importandroid.graphics.Bitmap; importandroid.graphics.Canvas; importandroid.graphics.Color; importandroid.graphics.Paint; importandroid.graphics.Path; importandroid.util.AttributeSet; importandroid.view.MotionEvent; importandroid.view.View; /** *Thisviewimplementsthedrawingcanvas. *<p/> *Ithandlesalloftheinputeventsanddrawingfunctions. *签名版 */ publicclassPaintViewextendsView{ privatePaintpaint; privateCanvascacheCanvas; privateBitmapcachebBitmap; privatePathpath; privateOnMoveLisenerlisener; publicvoidsetSize(intwidth,intheight,OnMoveLisenerlisener){ this.lisener=lisener; init(width,height); } publicPaintView(Contextcontext,AttributeSetattrs){ super(context,attrs); //init(0,0); } publicBitmapgetCachebBitmap(){ returncachebBitmap; } privatevoidinit(intwidth,intheight){ paint=newPaint(); paint.setAntiAlias(true); paint.setStrokeWidth(3); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); path=newPath(); cachebBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); cacheCanvas=newCanvas(cachebBitmap); cacheCanvas.drawColor(Color.WHITE); } publicvoidclear(){ if(cacheCanvas!=null){ paint.setColor(Color.WHITE); cacheCanvas.drawPaint(paint); paint.setColor(Color.BLACK); cacheCanvas.drawColor(Color.WHITE); invalidate(); } } @Override protectedvoidonDraw(Canvascanvas){ //canvas.drawColor(BRUSH_COLOR); canvas.drawBitmap(cachebBitmap,0,0,null); canvas.drawPath(path,paint); } @Override protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){ intcurW=cachebBitmap!=null?cachebBitmap.getWidth():0; intcurH=cachebBitmap!=null?cachebBitmap.getHeight():0; if(curW>=w&&curH>=h){ return; } if(curW<w) curW=w; if(curH<h) curH=h; BitmapnewBitmap=Bitmap.createBitmap(curW,curH,Bitmap.Config.ARGB_8888); CanvasnewCanvas=newCanvas(); newCanvas.setBitmap(newBitmap); if(cachebBitmap!=null){ newCanvas.drawBitmap(cachebBitmap,0,0,null); } cachebBitmap=newBitmap; cacheCanvas=newCanvas; } privatefloatcur_x,cur_y; @Override publicbooleanonTouchEvent(MotionEventevent){ getParent().requestDisallowInterceptTouchEvent(true);//通知父控件勿拦截本控件touch事件 floatx=event.getX(); floaty=event.getY(); switch(event.getAction()){ caseMotionEvent.ACTION_DOWN:{ cur_x=x; cur_y=y; path.moveTo(cur_x,cur_y); break; } caseMotionEvent.ACTION_MOVE:{ path.quadTo(cur_x,cur_y,x,y); cur_x=x; cur_y=y; lisener.hideWords();//隐藏提醒的文字 break; } caseMotionEvent.ACTION_UP:{ cacheCanvas.drawPath(path,paint); path.reset(); break; } } invalidate(); returntrue; } publicinterfaceOnMoveLisener{ voidhideWords();//主界面回调后隐藏提示文字 } }
以上就是本文的全部内容,希望对大家的学习有所帮助。