版权声明: https://blog.csdn.net/xu_coding/article/details/82056645
随着办公移动化,使用电子签名的场景也越来越多。
电子签名View的核心代码:
public class SignView extends View {
private Paint textPaint;
private ArrayList<Path> paths;
public SignView(Context context) {
this(context,null);
}
public SignView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(getResources().getColor(R.color.black));
textPaint.setStyle(Paint.Style.STROKE);
textPaint.setStrokeWidth(5);
paths = new ArrayList<>();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
for(int i=0;i< paths.size();i++){
canvas.drawPath(paths.get(i),textPaint);
}
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Path path = new Path();
path.moveTo(x,y);
paths.add(path);
break;
case MotionEvent.ACTION_MOVE:
paths.get(paths.size()-1).lineTo(x,y);
invalidate();
break;
}
return true;
}
//撤回一步
public void withDrawoOneStep(){
if(paths.size() > 0){
paths.remove(paths.size() - 1);
}
invalidate();
}
public void clearStep(){
paths.clear();
invalidate();
}
public Bitmap getBitmapFromView(){
this.setDrawingCacheEnabled(true); //开启图片缓存
buildDrawingCache(); //构建图片缓存
Bitmap bitmap = Bitmap.createBitmap(getDrawingCache()); //获取图片缓存
setDrawingCacheEnabled(false); //关闭图片缓存
return bitmap;
}
}
SignLayout 签名控件,封装了 3个button在里面,还可以自行添加背景图片。
获得签名图片,可以根据实际情况添加水印等信息。
public class SignLayout extends FrameLayout {
private ImageButton iv_save,iv_clear,iv_withdraw;
private SignView signView;
private Context mcontext;
private float density;
private OnSaveButtonListener onSaveButtonListener;
public SignLayout(Context context) {
this(context,null);
}
public SignLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mcontext = context;
density = getResources().getDisplayMetrics().density;
init();
}
private void init(){
initSignView();
initButton();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
private void initButton() {
iv_save = new ImageButton(mcontext);
iv_clear = new ImageButton(mcontext);
iv_withdraw = new ImageButton(mcontext);
LinearLayout linearLayout = new LinearLayout(mcontext);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.RIGHT);
LinearLayout.LayoutParams llparms = new LinearLayout.LayoutParams((int) (24*density),(int)( 24*density));
llparms.setMargins(0,(int) (5*density),(int)(20*density),0);
iv_save.setLayoutParams(llparms);
iv_clear.setLayoutParams(llparms);
iv_withdraw.setLayoutParams(llparms);
iv_save.setImageDrawable(getResources().getDrawable(R.drawable.icon_save));
iv_clear.setImageDrawable(getResources().getDrawable(R.drawable.icon_clear));
iv_withdraw.setImageDrawable(getResources().getDrawable(R.drawable.icon_withdraw));
linearLayout.addView(iv_clear);
linearLayout.addView(iv_withdraw);
linearLayout.addView(iv_save);
FrameLayout.LayoutParams flparms = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
flparms.gravity = Gravity.RIGHT|Gravity.TOP;
iv_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onSaveButtonListener!=null){
onSaveButtonListener.onSave(signView.getBitmapFromView());
}
}
});
iv_withdraw.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
signView.withDrawoOneStep();
}
});
iv_clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
signView.clearStep();
}
});
addView(linearLayout,flparms);
}
private void initSignView() {
signView = new SignView(mcontext);
addView(signView);
}
public void setOnSaveButtonListener(OnSaveButtonListener onSaveButtonListener) {
this.onSaveButtonListener = onSaveButtonListener;
}
public interface OnSaveButtonListener{
public void onSave(Bitmap bitmap);
}
}