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

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布局中。

1
2
3
4
<com.github.gcacace.signaturepad.views.SignaturePad
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/signaturePad"/>
1
2
3
4
< 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小部件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
   }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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文件中添加以下行,然后同步项目。

Add following code in respective files.

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

activity_main.xml

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<? 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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();
            }
        });
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 签名板_使用签名板库的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 签名板