当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现手写签名

Android实现手写签名

2019年07月24日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了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();//主界面回调后隐藏提示文字
 }
}

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网