Android中⼿写签名的实现
前⾔
本来这篇⽂章应该很早就发出来了,因为最近⼀直在忙项⽬,直到现在才发出来。本篇⽂章就是介绍如何在⼿机上进⾏签名。
设计思路
在画板上进⾏签名(其实就是绘制图⽚),裁剪图⽚,然后保存到本地相册。
效果图
代码
1.⾃⼰参考资料写了⼀个SignatureView继承View,实现签名
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 = Theme().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.Color(R.styleable.SignatureView_penColor, Color.BLACK));
mPaint.Color(R.styleable.SignatureView_penColor, Color.BLACK));
} finally {
}
/
/ 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 not found,
* {@link Color#BLACK} is assumed.
*
* @param colorRes
*            the color resource.
*/
public void setPenColorRes(int colorRes) {
try {
setPenColor(getResources().getColor(colorRes));
} catch (Resources.NotFoundException ex) {
setPenColor(getResources().getColor(Color.BLACK));
}
}
/**
* Set the pen color from a given color.
*
* @param color
*            the color.
*/
public void setPenColor(int color) {
mPaint.setColor(color);
}
/**
* Set the minimum width of the stroke in pixel.
*
* @param minWidth
*            the width in dp.
*/
public void setMinWidth(float minWidth) {
mMinWidth = convertDpToPx(minWidth);
}
/**
* Set the maximum width of the stroke in pixel.
*
* @param maxWidth
*            the width in dp.
*/
public void setMaxWidth(float maxWidth) {
public void setMaxWidth(float maxWidth) {
mMaxWidth = convertDpToPx(maxWidth);
}
/**
* Set the velocity filter weight.
*
* @param velocityFilterWeight
*            the weight.
*/
public void setVelocityFilterWeight(float velocityFilterWeight) {
mVelocityFilterWeight = velocityFilterWeight;
}
public void clear() {
mPoints = new ArrayList<TimedPoint>();
mLastVelocity = 0;
mLastWidth = (mMinWidth + mMaxWidth) / 2;
if (mSignatureBitmap != null) {
mSignatureBitmap = null;
ensureSignatureBitmap();
}
setIsEmpty(true);
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled())
return false;
float eventX = X();
float eventY = Y();
switch (Action()) {
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(true);
mPoints.clear();
mLastTouchX = eventX;
mLastTouchY = eventY;
addPoint(new TimedPoint(eventX, eventY));
case MotionEvent.ACTION_MOVE:
resetDirtyRect(eventX, eventY);
addPoint(new TimedPoint(eventX, eventY));
break;
case MotionEvent.ACTION_UP:
resetDirtyRect(eventX, eventY);
addPoint(new TimedPoint(eventX, eventY));
getParent().requestDisallowInterceptTouchEvent(true);
setIsEmpty(false);
break;
android获取真正的签名default:
return false;
}
// invalidate();
invalidate((int) (mDirtyRect.left - mMaxWidth), (int) (p - mMaxWidth), (int) (mDirtyRect.right + mMaxWidth), (int) (mDirtyRect.bottom + mMaxWidth));
(int) (mDirtyRect.right + mMaxWidth), (int) (mDirtyRect.bottom + mMaxWidth));
return true;
}
@Override
protected void onDraw(Canvas canvas) {
if (mSignatureBitmap != null) {
canvas.drawBitmap(mSignatureBitmap, 0, 0, mPaint);
}
}
public void setOnSignedListener(OnSignedListener listener) {
mOnSignedListener = listener;
}
public boolean isEmpty() {
return mIsEmpty;
}
public Bitmap getSignatureBitmap() {
Bitmap originalBitmap = getTransparentSignatureBitmap();
Bitmap whiteBgBitmap = Width(), Height(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(whiteBgBitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(originalBitmap, 0, 0, null);
return whiteBgBitmap;
}
public void setSignatureBitmap(Bitmap signature) {
clear();
ensureSignatureBitmap();
RectF tempSrc = new RectF();
RectF tempDst = new RectF();
int dWidth = Width();
int dHeight = Height();
int vWidth = getWidth();
int vHeight = getHeight();
// Generate the required transform.
tempSrc.set(0, 0, dWidth, dHeight);
tempDst.set(0, 0, vWidth, vHeight);
Matrix drawMatrix = new Matrix();
drawMatrix.setRectToRect(tempSrc, tempDst, Matrix.ScaleToFit.CENTER);
Canvas canvas = new Canvas(mSignatureBitmap);
canvas.drawBitmap(signature, drawMatrix, null);
setIsEmpty(false);
invalidate();
}
public Bitmap getTransparentSignatureBitmap() {
ensureSignatureBitmap();
return mSignatureBitmap;
}
public Bitmap getTransparentSignatureBitmap(boolean trimBlankSpace) {
if (!trimBlankSpace) {
return getTransparentSignatureBitmap();
}
ensureSignatureBitmap();
int imgHeight = Height();
int imgWidth = Width();
int backgroundColor = Color.TRANSPARENT;
int xMin = Integer.MAX_VALUE, xMax = Integer.MIN_VALUE, yMin = Integer.MAX_VALUE, yMax = Integer.MIN_VALUE;
boolean foundPixel = false;
// Find xMin
for (int x = 0; x < imgWidth; x++) {
boolean stop = false;
for (int y = 0; y < imgHeight; y++) {
if (Pixel(x, y) != backgroundColor) {
xMin = x;
stop = true;
foundPixel = true;
break;
}
}
if (stop)
break;
}
/
/ Image
if (!foundPixel)
return null;
// Find yMin
for (int y = 0; y < imgHeight; y++) {
boolean stop = false;
for (int x = xMin; x < imgWidth; x++) {
if (Pixel(x, y) != backgroundColor) {
yMin = y;
stop = true;
break;
}
}
if (stop)
break;
}
// Find xMax
for (int x = imgWidth - 1; x >= xMin; x--) {
boolean stop = false;
for (int y = yMin; y < imgHeight; y++) {
if (Pixel(x, y) != backgroundColor) {
xMax = x;
stop = true;
break;
}
}
if (stop)
break;
}
// Find yMax
for (int y = imgHeight - 1; y >= yMin; y--) {
boolean stop = false;
for (int x = xMin; x <= xMax; x++) {
if (Pixel(x, y) != backgroundColor) {
yMax = y;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。