美文网首页
自定义简单手写签名

自定义简单手写签名

作者: 涂涂家的小七呀 | 来源:发表于2019-08-26 11:30 被阅读0次

记录一个简单的签名view

/**
     * This view implements the drawing canvas.
     * <p>
     * It handles all of the input events and drawing functions.
     */
    class PaintView extends View {
        private Paint paint;
        private Canvas canvas;
        private Bitmap bitmap;
        private Path path;

        public Bitmap getCachebBitmap() {
            return bitmap;
        }

        public PaintView(Context context) {
            super(context);
            init();
        }

        private void init() {
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStrokeWidth(3);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
            path = new Path();
            bitmap = Bitmap.createBitmap(p.width, (int) (p.height * 0.8), Config.ARGB_8888);
            canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);
        }

        public void clear() {
            if (canvas != null) {
                NameWrite = false;
                paint.setColor(BACKGROUND_COLOR);
                canvas.drawPaint(paint);
                paint.setColor(Color.BLACK);
                canvas.drawColor(Color.WHITE);
                invalidate();
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(bitmap, 0, 0, null);
            canvas.drawPath(path, paint);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {

            int curW = bitmap != null ? bitmap.getWidth() : 0;
            int curH = bitmap != null ? bitmap.getHeight() : 0;
            if (curW >= w && curH >= h) {
                return;
            }

            if (curW < w)
                curW = w;
            if (curH < h)
                curH = h;

            Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Config.ARGB_8888);
            Canvas newCanvas = new Canvas();
            newCanvas.setBitmap(newBitmap);
            if (bitmap != null) {
                newCanvas.drawBitmap(bitmap, 0, 0, null);
            }
            bitmap = newBitmap;
            canvas = newCanvas;
        }

        private float cur_x, cur_y;

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    cur_x = x;
                    cur_y = y;
                    path.moveTo(cur_x, cur_y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    isWrite = true;//是否签名的标志
                    path.quadTo(cur_x, cur_y, x, y);
                    cur_x = x;
                    cur_y = y;
                    break;
                case MotionEvent.ACTION_UP:
                    canvas.drawPath(path, paint);
                    path.reset();
                    break;
            }
            invalidate();
            return true;
        }
    }

不多说,直接cv,这是个很简单的一个手写签名实现方式。

相关文章

网友评论

      本文标题:自定义简单手写签名

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