android手写签名android-signaturepad

GitHub地址: https://github.com/gcacace/android-signaturepad

android手写签名android-signaturepad_第1张图片

1 配置:

repositories {
       mavenCentral()
   }
compile 'com.github.gcacace:signature-pad:1.2.1'

2 使用:

Intent intent3 = new Intent(mActivity, SignatureActivity.class);
intent3.putExtra(SignatureActivity.TAG, itemBean);
mActivity.startActivity(intent3);

SignatureActivity (签名的使用方法  使用了DataBinding):

package com.zjsos.www.specialequipment.ui.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.basebean.ItemBean;
import com.jaydenxiao.common.basebean.MsgEvent;
import com.jaydenxiao.common.commonutils.ToastUtil;
import com.zjsos.www.specialequipment.R;
import com.zjsos.www.specialequipment.databinding.ActivitySignatureBinding;

import org.greenrobot.eventbus.EventBus;

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

/**
 * Author:Stephen
 * Blog: https://me.csdn.net/jifenglie
 * Date:2019/6/25
 * Description:
 */

public class SignatureActivity extends BaseActivity {

    private ItemBean itemBean;
    public static final String TAG = SignatureActivity.class.getSimpleName();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_signature;
    }


    @Override
    protected void initView() {
        Intent intent= getIntent();
        if(intent!=null){
            itemBean=  getIntent().getParcelableExtra(TAG);
        }else {
            itemBean=new ItemBean();
        }

        dataBinding.setItemP(new SignatureViewModel());
        File file=new File(itemBean.getValue());
        if (!file.exists()){
            return;
        }
        Bitmap bitmap= BitmapFactory.decodeFile(itemBean.getValue());
        dataBinding.signaturePad.setSignatureBitmap(bitmap);//使用
    }

    public class SignatureViewModel{
        public void clickEvent(View v) {
            switch (v.getId()){
                case R.id.backBt://返回
                    finish();
                    break;
                case R.id.clearBt://清除
                    dataBinding.signaturePad.clear();
                    break;
                case R.id.saveBt://保存
                    Bitmap signatureBitmap = dataBinding.signaturePad.getSignatureBitmap();
                    if (addJpgSignatureToGallery(signatureBitmap)) {
                        ToastUtil.showShort(SignatureActivity.this,"保存成功!");
                        finish();
                    } else {
                        ToastUtil.showShort(SignatureActivity.this,"保存失败!");
                        finish();
                    }
                    break;
            }
        }
    }



    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 boolean addJpgSignatureToGallery(Bitmap signature) {
        boolean result = false;
        try {
            File photo = new File(getAlbumStorageDir("SignaturePad"), String.format("Signature_%d.jpg", System.currentTimeMillis()));
            saveBitmapToJPG(signature, photo);
            itemBean.setValue(photo.getPath());//签名图片保存地址  photo.getPath()
            EventBus.getDefault().post(new MsgEvent(MsgEvent.SIGNATURE,itemBean));//EventBus发送签名图片保存地址  photo.getPath()
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


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





//    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;
//    }
//
//    private void scanMediaFile(File photo) {
//        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//        Uri contentUri = Uri.fromFile(photo);
//        mediaScanIntent.setData(contentUri);
//        sendBroadcast(mediaScanIntent);
//    }
}

自定义布局activity_signature :




    
        
    
    

        

        

            

            

            

        

    


 

你可能感兴趣的:(android开源框架)