本文实例为大家分享了Android手写签名的实现方法,产品要求用户可以在app上签协议。。所以得弄个手写签名版,参考了一些资料自己写了个PaintView去继承View,实现签名功能。
package com.****.*****.widget; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * This view implements the drawing canvas. * * It handles all of the input events and drawing functions. * 签名版 */ public class PaintView extends View { private Paint paint; private Canvas cacheCanvas; private Bitmap cachebBitmap; private Path path; private OnMoveLisener lisener; public void setSize(int width,int height,OnMoveLisener lisener) { this.lisener=lisener; init(width,height); } public PaintView(Context context, AttributeSet attrs) { super(context, attrs); //init(0,0); } public Bitmap getCachebBitmap() { return cachebBitmap; } private void init(int width,int height) { paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(3); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); path = new Path(); cachebBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); cacheCanvas = new Canvas(cachebBitmap); cacheCanvas.drawColor(Color.WHITE); } public void clear() { if (cacheCanvas != null) { paint.setColor(Color.WHITE); cacheCanvas.drawPaint(paint); paint.setColor(Color.BLACK); cacheCanvas.drawColor(Color.WHITE); invalidate(); } } @Override protected void onDraw(Canvas canvas) { // canvas.drawColor(BRUSH_COLOR); canvas.drawBitmap(cachebBitmap, 0, 0, null); canvas.drawPath(path, paint); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0; int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0; if (curW >= w && curH >= h) { return; } if (curW
以上就是本文的全部内容,希望对大家的学习有所帮助。