美文网首页
Android自定义view实现电子签名

Android自定义view实现电子签名

作者: 小城哇哇 | 来源:发表于2022-11-02 21:58 被阅读0次

首先new一个类继承于View

public class SignatureView extends View

自定义view,采用画笔绘制一张图片
定义一个画笔滑动的宽度
还需要对画笔进行跟踪,以便内容区域可以容纳笔划

private static final float STROKE_WIDTH = 5f;
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;

通过使最小可能区域无效来优化绘制

private Paint paint = new Paint();
private Path path = new Path();

private float lastTouchX;
private float lastTouchY;
private final RectF dirtyRect = new RectF();

构造方法里面初始化画笔

public SignatureView(Context context, AttributeSet attrs) {
    super(context, attrs);

    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(STROKE_WIDTH);
}

处理手势,触发画笔路径

@Override
public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            lastTouchX = eventX;
            lastTouchY = eventY;
            // 现在还没有终点,所以不要浪费周期使其失效。
            return true;

        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
            //开始跟画笔区域。
            resetDirtyRect(eventX, eventY);

            // 当硬件跟踪事件的速度快于事件的交付速度时
            // 事件将包含这些跳过点的历史记录。
            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
                float historicalX = event.getHistoricalX(i);
                float historicalY = event.getHistoricalY(i);
                expandDirtyRect(historicalX, historicalY);
                path.lineTo(historicalX, historicalY);
            }

            // 回放历史记录后,将线路连接到触点。
            path.lineTo(eventX, eventY);
            break;

        default:

            return false;
    }

    // 包括一半笔划宽度以避免剪裁
    invalidate(
            (int) (dirtyRect.left - HALF_STROKE_WIDTH),
            (int) (dirtyRect.top - HALF_STROKE_WIDTH),
            (int) (dirtyRect.right + HALF_STROKE_WIDTH),
            (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

    lastTouchX = eventX;
    lastTouchY = eventY;

    return true;
}
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
}

抬起时调用,以确包含所有路径

private void expandDirtyRect(float historicalX, float historicalY) {
    if (historicalX < dirtyRect.left) {
        dirtyRect.left = historicalX;
    } else if (historicalX > dirtyRect.right) {
        dirtyRect.right = historicalX;
    }
    if (historicalY < dirtyRect.top) {
        dirtyRect.top = historicalY;
    } else if (historicalY > dirtyRect.bottom) {
        dirtyRect.bottom = historicalY;
    }
}

到这里,手写功能就已经能使用了,接下来是做一些处理
在运动事件发生时重置
lastTouchX和lastTouchY是在动作结束时设置的

private void resetDirtyRect(float eventX, float eventY) {
    dirtyRect.left = Math.min(lastTouchX, eventX);
    dirtyRect.right = Math.max(lastTouchX, eventX);
    dirtyRect.top = Math.min(lastTouchY, eventY);
    dirtyRect.bottom = Math.max(lastTouchY, eventY);
}

清除签名

那写错了怎么办,肯定要有清除签名啦

public void clear() {
    path.reset();
    // 重新绘制整个视图
    invalidate();
}

获取图片缓存

public Bitmap getBitmapFromView(){
    this.setDrawingCacheEnabled(true);  //开启图片缓存
    buildDrawingCache();  //构建图片缓存
    Bitmap bitmap = Bitmap.createBitmap(getDrawingCache()); 
    setDrawingCacheEnabled(false);  //关闭图片缓存
    return bitmap;
}

效果如下:


来自:https://juejin.cn/post/7030816751839346719

相关文章

网友评论

      本文标题:Android自定义view实现电子签名

      本文链接:https://www.haomeiwen.com/subject/vvaetdtx.html