当前位置: 首页 > 工具软件 > Signature Pad > 使用案例 >

android手写签名库android-signaturepad

危寒
2023-12-01

android推荐个手写签名库android-signaturepad,2.3k


签名回调
public interface SignListener {
    public void onSignImage(String path);
}

签名弹框封装类

包含保存为本地文件的功能

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.noemhost.SignListener;
import com.github.gcacace.signaturepad.views.SignaturePad;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class SignatureDialog {

    public static class Builder {

        private SignListener mListener;
        private Context mContext;
        public Builder(Context context, SignListener listener) {
            mContext = context;
            mListener = listener;
        }

        public Dialog create() {
            final Dialog dialog = new Dialog(mContext, R.style.dialog_style);
            dialog.setContentView(R.layout.activity_signature);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            final SignaturePad mSignaturePad = (SignaturePad) dialog.findViewById(R.id.signature_pad);
            dialog.findViewById(R.id.dialog_confirm_cancel_tv).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.findViewById(R.id.dialog_confirm_clear_tv).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSignaturePad.clear();
                }
            });
            dialog.findViewById(R.id.dialog_confirm_confirm_tv).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!mSignaturePad.isEmpty()){
                        Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap();
                        String imagePath = addJpgSignatureToGallery(signatureBitmap);
                        if(!TextUtils.isEmpty(imagePath)){
                            mListener.onSignImage(imagePath);
                            dialog.dismiss();
                        } else {
                            ToastUtils.toastShortMsg((Activity) mContext, "签名保存失败");
                        }
                    } else {
                        ToastUtils.toastShortMsg((Activity) mContext, "请先签名");
                    }
                }
            });

            mSignaturePad.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
                }
            });

            Window window = dialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.getDecorView().setPadding(0, 0, 0, 0);

            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(lp);
            return dialog;
        }

        public Dialog show() {
            Dialog dialog = create();
            dialog.show();
            return dialog;
        }

        public File getAlbumStorageDir(String albumName) {
            File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), albumName);
            if (!file.mkdirs()) {
                Log.e("SignaturePad", "Directory not created");
            }
            return file;
        }



        public String addJpgSignatureToGallery(Bitmap signature) {
            String imgPath = "";
            try {
                File photo = new File(getAlbumStorageDir("SignaturePad"), String.format("Signature_%d.jpg", System.currentTimeMillis()));
                saveBitmapToJPG(signature, photo);
                imgPath = photo.getPath();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return imgPath;
        }

        public void saveBitmapToJPG(Bitmap bitmap, File photo) throws IOException {
            Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(newBitmap);
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bitmap, 0, 0, null);
            OutputStream stream = new FileOutputStream(photo);
            newBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
            stream.close();
        }
    }
}

签名使用
new SignatureDialog.Builder(activity, new SignListener() {
	@Override
    public void onSignImage(String path) {
    	path===为存储到本地的路径                
	}
}).show();

参考:https://blog.csdn.net/jifenglie/article/details/100033539

 类似资料: