Android implements handwritten signature _android

Source: Internet
Author: User

This article provides an example of how the Android handwriting signature is implemented, and the product requires users to sign an agreement on the app. So have to get a handwritten signature version, refer to some material oneself wrote a paintview to inherit view, realize the signature function.

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.
 * Signature Version */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, 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? CAC
  Hebbitmap.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);/notification
  Parent control do not block this control touch event 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 ();//Hide the reminder text break;
    Case MOTIONEVENT.ACTION_UP: {cachecanvas.drawpath (path, paint);
    Path.reset ();
   Break
 
  } invalidate ();
 return true; } public interfaceonmovelisener{void Hidewords ()//main interface callback hides prompt text}} 

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.