文章目录
实现
可以借助 signature-pad 库提供的 SignaturePad 控件来实现
效果
步骤
1、添加 signature-pad 库的依赖。
dependencies {
implementation 'com.github.gcacace:signature-pad:1.3.1'
}
2、在 layout 文件中使用 SignaturePad 控件,另外添加“清空”和“保存”两个按钮。
<?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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/signature_tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="请在下方签名"
android:textColor="@color/black"
android:textSize="20sp" />
<com.github.gcacace.signaturepad.views.SignaturePad
android:id="@+id/signature_s_signature"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="20dp"
android:background="@color/teal_700"
android:padding="20dp" />
<LinearLayout
android:id="@+id/signature_ll_button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<Button
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_weight="1"
android:background="@color/black"
android:text="清空"
android:textColor="@color/white"
android:textSize="16sp" />
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@color/black"
android:text="保存"
android:textColor="@color/white"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
3、实现清空 SignaturePad 控件内容的功能
调用signaturepad.clear() 方法即可
public void clearSignature() {
SignaturePad.clear();
}
4、实现保存 SignaturePad 控件内容的功能
private void saveSignature() {
//获取bitmap
Bitmap signatureBitmap = SignaturePad.getSignatureBitmap();
//保存bitmap到相册
if (addJpgSignatureToGallery2(signatureBitmap)) {
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
clearSignature();
} else {
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
5、实现兼容Android10以下和Android10以上“保存文件到公共目录”
private boolean addJpgSignatureToGallery2(Bitmap signature) {
boolean result = false;
try {
//新建文件名
String fileName = String.format(Locale.CHINA, "Signature_%d.jpg", System.currentTimeMillis());
String path = fileSaveToPublic(this, fileName, signature);
if(null == path){
result = false;
}else {
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 保存文件到公共目录
* @param context 上下文
* @param fileName 文件名
* @param bitmap 文件
* @return 路径,为空时表示保存失败
*/
public static String fileSaveToPublic(Context context, String fileName, Bitmap bitmap) {
String path = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
//Android 10以下版本
FileOutputStream fos = null;
try {
//设置路径 Pictures/
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//判断目录是否存在
//目录不存在时自动创建
if (folder.exists() || folder.mkdir()) {
File file = new File(folder, fileName);
fos = new FileOutputStream(file);
//写入文件
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
path = file.getAbsolutePath();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
//Android 10及以上版本
//设置路径 Pictures/
String folder = Environment.DIRECTORY_PICTURES;
//设置保存参数到ContentValues中
ContentValues values = new ContentValues();
//设置图片名称
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
//设置图片格式
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
//设置图片路径
values.put(MediaStore.Images.Media.RELATIVE_PATH, folder);
//执行insert操作,向系统文件夹中添加文件
//EXTERNAL_CONTENT_URI代表外部存储器,该值不变
Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream os = null;
try {
if (uri != null) {
//若生成了uri,则表示该文件添加成功
//使用流将内容写入该uri中即可
os = context.getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
path = uri.getPath();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return path;
}