欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现手写签名

程序员文章站 2024-03-01 09:49:28
本文实例为大家分享了android手写签名的实现方法,产品要求用户可以在app上签协议。。所以得弄个手写签名版,参考了一些资料自己写了个paintview去继承view,实...

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

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