为文档

时间:2017-06-01 12:47:20

标签: java android

我必须编写一个允许用户在文档中添加签名的Android应用程序。

我不知道怎么做。 是否有api java允许这样做或我可以在我的应用程序中使用的东西?

1 个答案:

答案 0 :(得分:0)

您可以做的是在文档下方添加一个签名板,如下所示: -

步骤1)在build.gradel compile 'com.github.gcacace:signature-pad:1.2.0

中添加

2)将其添加到文档下方的布局文件中

<com.github.gcacace.signaturepad.views.SignaturePad
            android:id="@+id/signature_pad"
            android:layout_width="fill_parent"
            android:layout_height="160dp"
            android:layout_marginBottom="52dp"
            android:background="@drawable/border"
            app:penMinWidth="5dp" />

3)在android mSignaturePad = (SignaturePad) findViewById(R.id.signature_pad);

中初始化视图

4)创建将签名保存为位图createBitmapSvg();的函数,并将其放在代码

下面
private void createBitmapSvg()
{
    Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap();
    if (addJpgSignatureToGallery(signatureBitmap))
    {
        //Toast.makeText(AgreementSignature.this, "Signature saved into the Gallery", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(AgreementSignature.this, "Unable to store the signature", Toast.LENGTH_SHORT).show();
    }
    if (addSvgSignatureToGallery(mSignaturePad.getSignatureSvg()))
    {
        //Toast.makeText(AgreementSignature.this, "SVG Signature saved into the Gallery", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(AgreementSignature.this, "Unable to store the SVG signature", Toast.LENGTH_SHORT).show();
    }
}

public boolean addJpgSignatureToGallery(Bitmap signature) {
    boolean result = false;
    try {

        String path = Environment.getExternalStorageDirectory()
                + "/Android/data/"
                + getApplicationContext().getPackageName()
                + "/Files";

        File dir = new File(path);
        if (!dir.exists())
            dir.mkdirs();

        File photo = new File(Environment.getExternalStorageDirectory() + "/Android/data/" + context.getPackageName() + "/" + String.format("Signature_%d.jpg", System.currentTimeMillis()));
        saveBitmapToJPG(signature, photo);
        scanMediaFile(photo);
        result = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

private void scanMediaFile(File photo) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(photo);
    mediaScanIntent.setData(contentUri);
    AgreementSignature.this.sendBroadcast(mediaScanIntent);
}

public boolean addSvgSignatureToGallery(String signatureSvg) {
    boolean result = false;
    try {
        File svgFile = new File(getAlbumStorageDir("SignaturePad"), String.format("Signature_%d.svg", System.currentTimeMillis()));
        OutputStream stream = new FileOutputStream(svgFile);
        OutputStreamWriter writer = new OutputStreamWriter(stream);
        writer.write(signatureSvg);
        writer.close();
        stream.flush();
        stream.close();
        scanMediaFile(svgFile);
        result = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

如果您需要更多帮助,可以在这里发表评论。