1.自定义View——支撑设置画笔色彩,画笔宽度,画板色彩,铲除画板,查看是否有签名,保存画板图片(仿制粘贴可直接使用)
/***CreatedbyYyyyQon2020/3/5.
*电子签名*/publicclassSignatureViewextendsView{privateContextcontext;//X轴起点privatefloatx;//Y轴起点privatefloaty;//画笔privatefinalPaintpaint=newPaint();//途径privatefinalPathpath=newPath();//画布privateCanvascanvas;//生成的图片privateBitmapbitmap;//画笔的宽度privateintpaintWidth=10;//签名色彩privateintpaintColor=Color.BLACK;//布景色彩privateintbackgroundColor=Color.WHITE;//是否现已签名privatebooleanisTouched=false;//签名开始与结束publicinterfaceTouch{voidOnTouch(booleanisTouch);
}privateTouchtouch;publicSignatureView(Contextcontext){super(context);
init(context);
}publicSignatureView(Contextcontext,@NullableAttributeSetattrs){super(context,attrs);
init(context);
}publicSignatureView(Contextcontext,@NullableAttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);
init(context);
}privatevoidinit(Contextcontext){this.context=context;//抗锯齿paint.setAntiAlias(true);//样式paint.setStyle(Paint.Style.STROKE);//画笔色彩paint.setColor(paintColor);//画笔宽度paint.setStrokeWidth(paintWidth);
}
@OverrideprotectedvoidonSizeChanged(intw,inth,intoldw,intoldh){super.onSizeChanged(w,h,oldw,oldh);//创建于view巨细一致的bitmapbitmap=Bitmap.createBitmap(getWidth(),getHeight(),Bitmap.Config.ARGB_8888);
canvas=newCanvas(bitmap);
canvas.drawColor(backgroundColor);
isTouched=false;
}
@OverridepublicbooleanonTouchEvent(MotionEventevent){if(touch!=null)touch.OnTouch(true);switch(event.getAction()){//手指按下caseMotionEvent.ACTION_DOWN:
touchDwon(event);break;//手指移动caseMotionEvent.ACTION_MOVE:
isTouched=true;if(touch!=null)touch.OnTouch(false);
touchMove(event);break;//手指抬起caseMotionEvent.ACTION_UP:
canvas.drawPath(path,paint);
path.reset();break;
}//更新制作invalidate();returntrue;
}
@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);//画此次笔画之前的签名canvas.drawBitmap(bitmap,0,0,paint);//通过画布制作多点构成的图形canvas.drawPath(path,paint);
}//手指按下的办法privatevoidtouchDwon(MotionEventevent){//重置制作途径path.reset();floatdownX=event.getX();floatdownY=event.getY();
x=downX;
y=downY;//制作起点path.moveTo(downX,downY);
}//手指滑动的办法privatevoidtouchMove(MotionEventevent){//当前的x,y坐标点finalfloatmoveX=event.getX();finalfloatmoveY=event.getY();//之前的x,y坐标点finalfloatpreviousX=x;finalfloatpreviousY=y;//获取绝对值finalfloatdx=Math.abs(moveX-previousX);finalfloatdy=Math.abs(moveY-previousY);if(dx>=3||dy>=3){floatcX=(moveX+previousX)/2;floatcY=(moveY+previousY)/2;
path.quadTo(previousX,previousY,cX,cY);
x=moveX;
y=moveY;
}
}/***设置画笔色彩
*
*@parampaintColor*/publicvoidsetPaintColor(intpaintColor){this.paintColor=paintColor;
paint.setColor(paintColor);
}/***设置画笔宽度
*
*@parampaintWidth*/publicvoidsetPaintWidth(intpaintWidth){this.paintWidth=paintWidth;
paint.setStrokeWidth(paintWidth);
}/***设置画板色彩
*
*@paramcanvasColor*/publicvoidsetCanvasColor(intcanvasColor){this.backgroundColor=canvasColor;
}/***铲除画板*/publicvoidclear(){if(canvas!=null){
isTouched=false;//更新画板paint.setColor(paintColor);
paint.setStrokeWidth(paintWidth);
canvas.drawColor(backgroundColor,PorterDuff.Mode.CLEAR);
invalidate();
}
}/***获取画板的Bitmap
*
*@return*/publicBitmapgetBitmap(){
setDrawingCacheEnabled(true);
buildDrawingCache();
Bitmapbitmap=getDrawingCache();
setDrawingCacheEnabled(false);returnbitmap;
}/***是否有签名
*
*@return*/publicBooleangetSigstatus(){returnisTouched;
}/***保存画板
*
*@parampath保存到途径*/@SuppressLint(“WrongThread”)publicBooleansave(Stringpath)throwsIOException{
Bitmapbitmap=this.bitmap;
ByteArrayOutputStreambos=newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,bos);byte[]buffer=bos.toByteArray();if(buffer!=null){
Filefile=newFile(path);if(file.exists()){
file.delete();
}
OutputStreamoutputStream=newFileOutputStream(file);
outputStream.write(buffer);
outputStream.close();returntrue;
}else{returnfalse;
}
}
}
2.xml布局引用自定义View(留意包名)
android:layout_width=”match_parent”android:layout_height=”match_parent”android:orientation=”vertical”>
android:id=”@+id/signature”android:layout_width=”match_parent”android:layout_height=”0dp”android:layout_weight=”1″android:background=”#fff”/>
android:layout_width=”match_parent”android:layout_height=”50dp”android:layout_margin=”20dp”android:orientation=”horizontal”>
android:id=”@+id/clear”android:layout_width=”0dp”android:layout_height=”match_parent”android:layout_weight=”1″android:gravity=”center”android:text=”铲除”/>
android:id=”@+id/isSignature”android:layout_width=”0dp”android:layout_height=”match_parent”android:layout_marginLeft=”10dp”android:layout_weight=”1″android:gravity=”center”android:text=”是否签名”/>
android:id=”@+id/save”android:layout_width=”0dp”android:layout_height=”match_parent”android:layout_marginLeft=”10dp”android:layout_weight=”1″android:gravity=”center”android:text=”保存”/>
3.Activity调用
/**
*CreatedbyYyyyQon2020/3/9.
*/
publicclassSignatureActivityextendsAppCompatActivity{
@Override
protectedvoidonCreate(@NullableBundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signature);
SignatureViewsignatureView=findViewById(R.id.signature);
//设置画笔色彩(能够不设置–默许画笔宽度10,画笔色彩黑,布景色彩白)
signatureView.setPaintColor(Color.BLACK);
signatureView.setPaintWidth(20);
signatureView.setCanvasColor(Color.WHITE);
//铲除
Buttonclear=findViewById(R.id.clear);
clear.setOnClickListener(view->{
signatureView.clear();
//设置画笔色彩(能够不设置–默许画笔宽度10,画笔色彩黑,布景色彩白)
signatureView.setPaintColor(Color.BLACK);
signatureView.setPaintWidth(20);
signatureView.setCanvasColor(Color.WHITE);
});
//是否含有签名
ButtonisSignature=findViewById(R.id.isSignature);
isSignature.setOnClickListener(view->{
if(signatureView.getSigstatus()){
Toast.makeText(SignatureActivity.this,”有签名”,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SignatureActivity.this,”无签名”,Toast.LENGTH_SHORT).show();
}
});
//保存
Buttonsave=findViewById(R.id.save);
save.setOnClickListener(view->{
try{
if(signatureView.save(“/sdcard/YyyyQ.png”)){
Toast.makeText(SignatureActivity.this,”保存成功”,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SignatureActivity.this,”保存失败”,Toast.LENGTH_SHORT).show();
}
}catch(IOExceptione){
e.printStackTrace();
}
});
}
}