安卓模仿签名

创建画笔:

paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
path = new Path();
cachebBitmap = Bitmap.createBitmap(MeasureSpec.getSize(widthMeasureSpec),MeasureSpec.getSize(heightMeasureSpec), Bitmap.Config.ARGB_8888);
cacheCanvas = new Canvas(cachebBitmap);
cacheCanvas.drawColor(Color.TRANSPARENT);

转存失败重新上传取消

在 onTouchEvent 处理:

float x = event.getX();
float y = event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN: {if(isListener!=null){isListener.sign();}cur_x = x;cur_y = y;path.moveTo(cur_x, cur_y);break;}case MotionEvent.ACTION_MOVE: {path.quadTo(cur_x, cur_y, x, y);cur_x = x;cur_y = y;break;}case MotionEvent.ACTION_UP: {cacheCanvas.drawPath(path, paint);path.reset();break;}
}invalidate();return true;

转存失败重新上传取消

在 onSizeChanged 实时更新变化:

转存失败重新上传取消

int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0;
int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0;
if (curW >= w && curH >= h) {return;
}if (curW < w)curW = w;
if (curH < h)curH = h;Bitmap newBitmap = Bitmap.createBitmap(curW, curH,Bitmap.Config.ARGB_8888);
Canvas newCanvas = new Canvas();
newCanvas.setBitmap(newBitmap);
if (cachebBitmap != null) {newCanvas.drawBitmap(cachebBitmap, 0, 0, null);
}
cachebBitmap = newBitmap;
cacheCanvas = newCanvas;

最后画出轨迹:

转存失败重新上传取消

canvas.drawBitmap(cachebBitmap, 0, 0, null);
canvas.drawPath(path, paint);

画出轨迹后,通过

mView.setDrawingCacheEnabled(true);
saveSign(mView.getDrawingCache());
mView.setDrawingCacheEnabled(false);
将签名截图保存到本地、
 
mView.setDrawingCacheEnabled(true);
saveSign(mView.getDrawingCache());
mView.setDrawingCacheEnabled(false);

画出的字迹可以清除:
 
if (cacheCanvas != null) {
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
cacheCanvas.drawPaint(paint);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
invalidate();
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部