android 签名板_使用签名板库的Android签名捕获示例

本教程展示了如何在Android应用中实现数字签名功能,使用Signature Pad库添加签名视图到布局,设置监听器捕获签名,以及通过getSignatureBitmap()和getSignatureSvg()等方法获取签名数据。示例代码包括在activity_main.xml和MainActivity.java中添加所需代码。
摘要由CSDN通过智能技术生成

android 签名板

Here you will get android signature capture example.

在这里,您将获得android签名捕获示例。

You may have seen many eCommerce companies like Amazon provides a facility for digital signature. You can implement that feature in your app easily by using a library called Signature Pad.

您可能已经看到像Amazon这样的许多电子商务公司提供了数字签名功能。 您可以使用名为Signature Pad的库轻松地在应用程序中实现该功能。

Video Demo

影片示范

Below tutorial shows you how to do this.

下面的教程显示了如何执行此操作。

Android签名捕获示例 (Android Signature Capture Example)

Add to Layout

添加到布局

You can simply add a signature drawing view to your xml layout by using following tag.

您可以使用以下标记简单地将签名绘图视图添加到xml布局中。

<com.github.gcacace.signaturepad.views.SignaturePad
         android:layout_width="match_parent"
         android:layout_height="200dp"
         android:id="@+id/signaturePad"/>

Apply Listener

应用监听器

You can simply apply a listener to Signature Pad widget in following way.

您可以通过以下方式简单地将侦听器应用于Signature Pad小部件。

signaturePad = (SignaturePad) findViewById(R.id.signaturePad);
 signaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {
  
     @Override
     public void onStartSigning() {
         //Event triggered when the pad is touched
     }
  
    @Override
    public void onSigned() {
        //Event triggered when the pad is signed
    }
  
    @Override
    public void onClear() {
        //Event triggered when the pad is cleared
    }
 });

Get Data

获取数据

You can get signature data by following methods.

您可以通过以下方法获取签名数据。

  • getSignatureBitmap() – Gets signature bitmap with a white background.

    getSignatureBitmap() –获取带有白色背景的签名位图。

  • getTransparentSignatureBitmap() – Gets signature bitmap with a transparent background.

    getTransparentSignatureBitmap() –获取具有透明背景的签名位图。

  • getSignatureSvg() – Gets signature Scalable Vector Graphics document.

    getSignatureSvg() –获取签名可缩放矢量图形文档。

For clearing the signature pad area use clear() method.

要清除签名板区域,请使用clear()方法。

In below example I have just captured the signature and not saved it anywhere. You can get signature data using above methods and then save anywhere like in database or server.

在下面的示例中,我刚刚捕获了签名,并且没有将其保存在任何地方。 您可以使用上述方法获取签名数据,然后将其保存在数据库或服务器中的任何位置。

Android专案 (Android Project)

First create an android project with package name com.signaturecapture or you can use your existing project.

首先创建一个名为com.signaturecapture的android项目,或者您可以使用现有项目。

Now we have to add the Signature Pad library to our project. Add following line in your app level build.gradle file under dependencies section and then sync the project.

现在,我们必须将Signature Pad库添加到我们的项目中。 在依赖级别部分的应用程序级别build.gradle文件中添加以下行,然后同步项目。

compile 'com.github.gcacace:signature-pad:1.2.1'

Add following code in respective files.

在相应的文件中添加以下代码。

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context="com.signaturecapture.MainActivity"
     android:orientation="vertical">
  
     <com.github.gcacace.signaturepad.views.SignaturePad
         android:layout_width="match_parent"
         android:layout_height="200dp"
         android:id="@+id/signaturePad"/>
  
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">
         <Button
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:id="@+id/saveButton"
             android:text="Save"
             android:layout_weight="1"/>
  
         <Button
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:id="@+id/clearButton"
             android:text="Clear"
             android:layout_weight="1"/>
     </LinearLayout>
  
 </LinearLayout>

MainActivity.java

MainActivity.java

package com.signaturecapture;
  
 import android.content.pm.ActivityInfo;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;
  
 import com.github.gcacace.signaturepad.views.SignaturePad;
  
 public class MainActivity extends AppCompatActivity {
     SignaturePad signaturePad;
     Button saveButton, clearButton;
  
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
  
         signaturePad = (SignaturePad)findViewById(R.id.signaturePad);
         saveButton = (Button)findViewById(R.id.saveButton);
         clearButton = (Button)findViewById(R.id.clearButton);
  
         //disable both buttons at start
         saveButton.setEnabled(false);
         clearButton.setEnabled(false);
  
         //change screen orientation to landscape mode
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  
         signaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {
             @Override
             public void onStartSigning() {
  
             }
  
             @Override
             public void onSigned() {
                 saveButton.setEnabled(true);
                 clearButton.setEnabled(true);
             }
  
             @Override
             public void onClear() {
                 saveButton.setEnabled(false);
                 clearButton.setEnabled(false);
             }
         });
  
         saveButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //write code for saving the signature here
                 Toast.makeText(MainActivity.this, "Signature Saved", Toast.LENGTH_SHORT).show();
             }
         });
  
         clearButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 signaturePad.clear();
             }
         });
     }
 }

Save and run your project.

保存并运行您的项目。

Screenshot

屏幕截图

使用签名板库的Android签名捕获示例

Comment below if you have any queries related to above android signature capture example.

如果您有任何与上述android签名捕获示例相关的查询,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/02/android-signature-capture-example.html

android 签名板

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值