package com.ycq.customerview.view;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 签名View,可回退上一笔,可清空,可保存
*/
public class SignatureView extends View {
/**
* 画笔
*/
private Paint mPaint;
/**
* 路径
*/
private Path mPath;
/**
* 所有路径/笔画集合,每一个item为一笔
*/
private final List<Path> mLists = new ArrayList<>();
/**
* 图片保存路径
* Android/data/应用包名/files/Pictures/XXX1659508032166.jpg
*/
private String mSavePath;
public SignatureView(Context context) {
this(context, null);
}
public SignatureView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public SignatureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
//设置白色背景,否则保存的图片是黑色的
setBackgroundColor(Color.WHITE);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(dp2px(8f));
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPath = new Path();
}
private float dp2px(float dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mPath, mPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
float x = event.getX();
float y = event.getY();
//创建新的路径,即新的笔画
Path path = new Path();
path.moveTo(x, y);
mLists.add(path);
//移动到笔画的起点
mPath.moveTo(x, y);
break;
case MotionEvent.ACTION_MOVE:
float x1 = event.getX();
float y1 = event.getY();
if (mLists != null && mLists.size() != 0) {
Path path1 = mLists.get(mLists.size() - 1);
path1.lineTo(x1, y1);
}
//笔画的每一个点
mPath.lineTo(x1, y1);
invalidate();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
/**
* 回退一笔
*/
public void goBackOne() {
//重置路径
mPath.reset();
if (mLists != null && mLists.size() != 0) {
//去掉最后一笔
mLists.remove(mLists.size() - 1);
if (mLists.size() != 0) {
for (Path path : mLists) {
mPath.addPath(path);
}
}
}
invalidate();
}
/**
* 清空
*/
public void clear() {
mPath.reset();
mLists.clear();
invalidate();
}
public Bitmap saveCanvas(Context context) {
return saveCanvas(context, "");
}
public Bitmap saveCanvas(Context context, String picName) {
if (mLists != null && mLists.size() != 0) {
long time = System.currentTimeMillis();//系统时间
File filesDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
StringBuilder sb = new StringBuilder();
sb.append(filesDir.getAbsolutePath());
if (picName.equals("")) {
sb.append("/signature");
} else {
sb.append("/").append(picName);
}
mSavePath = sb.append(time).append(".jpg").toString();
setDrawingCacheEnabled(true);
//生成图片并保存
Bitmap bitmap = getDrawingCache();
bitmap = Bitmap.createBitmap(bitmap);
try {
FileOutputStream fos = new FileOutputStream(new File(mSavePath));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);//保存
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
setDrawingCacheEnabled(false);
Toast.makeText(context, "文件已保存到:" + mSavePath, Toast.LENGTH_LONG).show();
System.out.println("文件已保存到:" + mSavePath);
Bitmap bitmap1 = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(mSavePath);
bitmap1 = BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap1;
}
Toast.makeText(context, "画点什么吧......", Toast.LENGTH_LONG).show();
return null;
}
public String getSavePath() {
return mSavePath;
}
}