2017-06-01 68 views
0

我必須編寫一個允許用戶將簽名添加到文檔的android應用程序。向文檔添加簽名的Android應用程序

我不知道該怎麼做。 是否有允許做它或我可以在我的應用程序中使用的API?

+1

請編輯你的問題解釋,在**詳細的編程術語**,什麼「添加簽名到文件」的意思。 – CommonsWare

+0

你可能需要手勢生成器。 我覺得這個鏈接。 http://android-developers.blogspot.com/2009/10/gestures-on-android-16.html 將對您有用。如果你需要再次檢查簽名。 – headshoT

回答

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)初始化視圖在機器人​​

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; 
} 

如果您需要任何更多的幫助,您可以在這裏留言。