自定义View:
package top.lc951.myandroid.views;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import top.lc951.myandroid.R;
/**
* 自定义签名View
*
* @author lichong
* 2022年08月03日14:22:22
*/
public class SignatureView extends View {
public static final String TAG = SignatureView.class.getSimpleName();
private Paint mPathPaint = new Paint(); // 声明一个画笔对象
private Path mPath = new Path(); // 声明一个路径对象
private int mPathPaintColor = Color.BLACK; // 画笔颜色
private int mStrokeWidth = 3; // 画笔线宽
private PathPosition mPathPos = new PathPosition(); // 路径位置
private List mPathList = new ArrayList(); // 路径位置列表
private PointF mLastPos; // 上次触摸点的横纵坐标
// 定义一个路径位置实体类,包括上个落点的横纵坐标,以及下个落点的横纵坐标
public class PathPosition {
public PointF prePos; // 上个落点的横纵坐标
public PointF nextPos; // 下个落点的横纵坐标
}
public SignatureView(Context context) {
this(context, null);
}
public SignatureView(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
// 根据SignatureView的属性定义,从布局文件中获取属性数组描述
TypedArray attrArray = getContext().obtainStyledAttributes(attrs, R.styleable.SignatureView);
// 根据属性描述定义,获取布局文件中的画笔颜色
mPathPaintColor = attrArray.getColor(R.styleable.SignatureView_paint_color, Color.BLACK);
// 根据属性描述定义,获取布局文件中的画笔线宽
mStrokeWidth = attrArray.getInt(R.styleable.SignatureView_stroke_width, 3);
attrArray.recycle(); // 回收属性数组描述
}
initView(); // 初始化视图
}
// 初始化视图
private void initView() {
mPathPaint.setStrokeWidth(mStrokeWidth); // 设置画笔的线宽
mPathPaint.setStyle(Paint.Style.STROKE); // 设置画笔的类型。STROK表示空心,FILL表示实心
mPathPaint.setColor(mPathPaintColor); // 设置画笔的颜色
setDrawingCacheEnabled(true); // 开启当前视图的绘图缓存
}
// 清空画布
public void clear() {
mPath.reset(); // 重置路径对象
mPathList.clear(); // 清空路径列表
postInvalidate(); // 立即刷新视图(线程安全方式)
}
// 撤销上一次绘制
public void revoke() {
if (mPathList.size() > 0) {
// 移除路径位置列表中的最后一个路径
mPathList.remove(mPathList.size() - 1);
mPath.reset(); // 重置路径对象
for (int i = 0; i
使用:
if (v.getId() == R.id.btn_save_signature) { // 点击了保存签名按钮
if (TextUtils.isEmpty(mImagePath)) {
Toast.makeText(SignatureActivity.this, "请先开始然后结束签名", Toast.LENGTH_LONG).show();
return;
}
BitmapUtil.notifyPhotoAlbum(SignatureActivity.this, mImagePath); // 通知相册来了张新图片
Toast.makeText(SignatureActivity.this, "已保存签名图片,请到系统相册查看", Toast.LENGTH_LONG).show();
} else if (v.getId() == R.id.btn_begin_signature) { // 点击了开始签名按钮
// 开启签名视图的绘图缓存
signatureView.setDrawingCacheEnabled(true);
} else if (v.getId() == R.id.btn_reset_signature) { // 点击了重置按钮
signatureView.clear(); // 清空签名视图
} else if (v.getId() == R.id.btn_revoke_signature) { // 点击了回退按钮
signatureView.revoke(); // 回退签名视图的最近一笔绘画
} else if (v.getId() == R.id.btn_end_signature) { // 点击了结束签名按钮
if (!signatureView.isDrawingCacheEnabled()) { // 签名视图的绘图缓存不可用
Toast.makeText(SignatureActivity.this, "请先开始签名", Toast.LENGTH_LONG).show();
} else { // 签名视图的绘图缓存当前可用
Bitmap bitmap = signatureView.getDrawingCache(); // 从绘图缓存获取位图对象
// 生成图片文件的保存路径
// mImagePath = String.format("%s/%s.jpg",
// getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString(),
// DateUtil.getNowDateTime());
// BitmapUtil.saveImage(mImagePath, bitmap); // 把位图保存为图片文件
// signatureIvNew.setImageURI(Uri.parse(mImagePath)); // 设置图像视图的路径对象
// 延迟100毫秒后启动绘图缓存的重置任务
new Handler(Looper.myLooper()).postDelayed(() -> {
// 关闭签名视图的绘图缓存
signatureView.setDrawingCacheEnabled(false);
// 开启签名视图的绘图缓存
signatureView.setDrawingCacheEnabled(true);
}, 100);
}
}
}