Android实现手写签名

所属分类: 软件编程 / Android 阅读数: 410
收藏 0 赞 0 分享

本文实例为大家分享了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.
 * <p/>
 * 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 < 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;
 }
 
 private float cur_x, cur_y;
 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 
 getParent().requestDisallowInterceptTouchEvent(true);// 通知父控件勿拦截本控件touch事件
 
 float x = event.getX();
 float y = event.getY();
 
 switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN: {
  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;
  lisener.hideWords();//隐藏提醒的文字
  break;
  }
 
  case MotionEvent.ACTION_UP: {
  cacheCanvas.drawPath(path, paint);
  path.reset();
  break;
  }
 }
 
 invalidate();
 
 return true;
 }
 
 
 public interface OnMoveLisener{
 void hideWords();//主界面回调后隐藏提示文字
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多