之前在网上借鉴一个demo但后来找不到了,就把自己项目放出来,记录一下。
一、首先是布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="65dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<charging.jingmi.nanjing.com.filemanagement.views.SignatureView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/signature_pad"
android:enabled="false"
android:background="@color/gray_background"/>
<TextView android:id="@+id/tv_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="签名区"
android:textColor="@color/black"
android:layout_centerInParent="true"/>
<LinearLayout
android:id="@+id/buttons_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_below="@+id/signature_pad"
android:orientation="horizontal">
<Button
android:id="@+id/clear_button"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:enabled="false"
android:text="清除"
android:textColor="@color/colorAccent"
android:textSize="15sp" />
<Button
android:id="@+id/save_button"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:enabled="false"
android:text="保存"
android:textColor="@color/colorAccent"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
<Button android:id="@+id/btn_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_alignParentBottom="true"
android:text="转base64"
android:textColor="@color/white"
android:background="@drawable/side_nav_bar"/>
二、View文件夹中新建SignatureView定义画笔
public class SignatureView extends View {
// View state
private List<TimedPoint> mPoints;
private boolean mIsEmpty;
private float mLastTouchX;
private float mLastTouchY;
private float mLastVelocity;
private float mLastWidth;
private RectF mDirtyRect;
// Configurable parameters
private int mMinWidth;
private int mMaxWidth;
private float mVelocityFilterWeight;
private OnSignedListener mOnSignedListener;
private Paint mPaint = new Paint();
private Path mPath = new Path();
private Bitmap mSignatureBitmap = null;
private Canvas mSignatureBitmapCanvas = null;
public SignatureView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SignatureView, 0, 0);
// Configurable parameters
try {
mMinWidth = a.getDimensionPixelSize(R.styleable.SignatureView_minWidth, convertDpToPx(3));
mMaxWidth = a.getDimensionPixelSize(R.styleable.SignatureView_maxWidth, convertDpToPx(20));
mVelocityFilterWeight = a.getFloat(R.styleable.SignatureView_velocityFilterWeight, 0.9f);
mPaint.setColor(a.getColor(R.styleable.SignatureView_penColor, Color.BLACK));
} finally {
a.recycle();
}
// Fixed parameters
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeJoin(Paint.Join.ROUND);
//mPaint.setStyle(Paint.Style.FILL); //画笔风格
// mPaint.setAntiAlias(true); //抗锯齿
// mPaint.setStrokeWidth(10); //画笔粗细
// mPaint.setTextSize(100); //绘制文字大小,单位px
//mPaint.setStrokeWidth(Paint.S);
// Dirty rectangle to update only the changed portion of the view
mDirtyRect = new RectF();
clear();
}
/**
* Set the pen color from a given resource. If the resource is no