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

Android实现屏幕手写签名

2019年09月06日  | 移动技术网移动技术  | 我要评论

android屏幕手写签名的原理就是把手机屏幕当作画板,把用户手指当作画笔,手指在屏幕上在屏幕上划来划去,屏幕就会显示手指的移动轨迹,就像画笔在画板上写字一样。实现手写签名需要结合绘图的路径工具path,在有按下动作时调用path对象的moveto方法,将路径起始点移动到触摸点;在有移动操作时调用path对象的quadto方法,将记录本次触摸点与上次触摸点之间的路径;在有移动操作与提起动作时调用canvas对象的drawpath方法,将本次触摸绘制在画布上。

layout/activity_signature.xml界面布局代码如下:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:padding="5dp">

 <scrollview
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <linearlayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">

   <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <button
     android:id="@+id/btn_add_signature"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="开始签名"
     android:textcolor="@color/black"
     android:textsize="17sp" />

    <button
     android:id="@+id/btn_reset_signature"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:text="重置"
     android:textcolor="@color/black"
     android:textsize="17sp" />

    <button
     android:id="@+id/btn_revoke_signature"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:text="回退"
     android:textcolor="@color/black"
     android:textsize="17sp" />

    <button
     android:id="@+id/btn_end_signature"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="结束签名"
     android:textcolor="@color/black"
     android:textsize="17sp" />
   </linearlayout>

   <com.fukaimei.touchevent.widget.signatureview
    android:id="@+id/view_signature"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/white"
    app:paint_color="#0000aa"
    app:stroke_width="3" />

   <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <button
     android:id="@+id/btn_save_signature"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="保存图片文件"
     android:textcolor="@color/black"
     android:textsize="17sp" />
   </linearlayout>

   <imageview
    android:id="@+id/iv_signature_new"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/white"
    android:scaletype="fitcenter" />
  </linearlayout>
 </scrollview>

</linearlayout>

signatureactivity.java逻辑代码如下:

package com.fukaimei.touchevent;

import android.graphics.bitmap;
import android.os.bundle;
import android.os.handler;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.imageview;
import android.widget.toast;

import com.fukaimei.touchevent.filedialog.dialog.filesavefragment;
import com.fukaimei.touchevent.util.bitmaputil;
import com.fukaimei.touchevent.widget.signatureview;

public class signatureactivity extends appcompatactivity implements
  onclicklistener, filesavefragment.filesavecallbacks {
 private signatureview view_signature;
 private imageview iv_signature_new;
 private bitmap mbitmap;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_signature);
  view_signature = (signatureview) findviewbyid(r.id.view_signature);
  iv_signature_new = (imageview) findviewbyid(r.id.iv_signature_new);
  findviewbyid(r.id.btn_add_signature).setonclicklistener(this);
  findviewbyid(r.id.btn_end_signature).setonclicklistener(this);
  findviewbyid(r.id.btn_reset_signature).setonclicklistener(this);
  findviewbyid(r.id.btn_revoke_signature).setonclicklistener(this);
  findviewbyid(r.id.btn_save_signature).setonclicklistener(this);
 }

 @override
 public void onclick(view v) {
  if (v.getid() == r.id.btn_save_signature) {
   if (mbitmap == null) {
    toast.maketext(this, "请先开始然后结束签名", toast.length_long).show();
    return;
   }
   filesavefragment.show(this, "jpg");
  } else if (v.getid() == r.id.btn_add_signature) {
   view_signature.setdrawingcacheenabled(true);
  } else if (v.getid() == r.id.btn_reset_signature) {
   view_signature.clear();
  } else if (v.getid() == r.id.btn_revoke_signature) {
   view_signature.revoke();
  } else if (v.getid() == r.id.btn_end_signature) {
   if (view_signature.isdrawingcacheenabled() != true) {
    toast.maketext(this, "请先开始签名", toast.length_long).show();
   } else {
    mbitmap = view_signature.getdrawingcache();
    iv_signature_new.setimagebitmap(mbitmap);
    mhandler.postdelayed(mresetcache, 100);
   }
  }
 }

 private handler mhandler = new handler();
 private runnable mresetcache = new runnable() {
  @override
  public void run() {
   view_signature.setdrawingcacheenabled(false);
   view_signature.setdrawingcacheenabled(true);
  }
 };

 @override
 public boolean oncansave(string absolutepath, string filename) {
  return true;
 }

 @override
 public void onconfirmsave(string absolutepath, string filename) {
  string path = string.format("%s/%s", absolutepath, filename);
  bitmaputil.savebitmap(path, mbitmap, "jpg", 80);
  toast.maketext(this, "成功保存图片文件:" + path, toast.length_long).show();
 }

}

demo程序运行效果界面截图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网