android - canvas 画线

访问量: 161

refer to:  idcard lib的代码。 faceid库.

有个项目,需要用到身份证识别。

身份证识别的主窗口,需要一张背景图,加上边框。

所以,这个视图,需要用到canvas 来画图。

代码如下(注意代码中的注释, 包含了颜色说明,strokeWidth等):

    @Override       // 复写这个方法。
    protected void onDraw(Canvas canvas) {       

        /**
         * 0X: 16进制颜色
         * FF: 透明度
         * 00D3FF:  hex颜色
         */
        /*
        int finderColor = 0XFF00D3FF;
        mDrawPaint.setStyle(Paint.Style.STROKE);  // 设置样式,线是实心的。stoke: 描边。 fill: 填充
        mDrawPaint.setColor(finderColor);  // 设置颜色
        mDrawPaint.setStrokeWidth(3);    //边的宽度

       //  drawLine:  左,上,右,下四个边界.
        // 四个蓝色的角
        // left top
        canvas.drawLine(mShowRect.left, mShowRect.top, mShowRect.left + length, mShowRect.top, mDrawPaint);
        canvas.drawLine(mShowRect.left, mShowRect.top, mShowRect.left, mShowRect.top + length, mDrawPaint);

        // right top
        canvas.drawLine(mShowRect.right, mShowRect.top, mShowRect.right - length, mShowRect.top, mDrawPaint);
        canvas.drawLine(mShowRect.right, mShowRect.top, mShowRect.right, mShowRect.top + length, mDrawPaint);

        // left bottom
        canvas.drawLine(mShowRect.left, mShowRect.bottom, mShowRect.left + length, mShowRect.bottom, mDrawPaint);
        canvas.drawLine(mShowRect.left, mShowRect.bottom, mShowRect.left, mShowRect.bottom - length, mDrawPaint);

        // right bottom
        canvas.drawLine(mShowRect.right, mShowRect.bottom, mShowRect.right - length, mShowRect.bottom, mDrawPaint);
        canvas.drawLine(mShowRect.right, mShowRect.bottom, mShowRect.right, mShowRect.bottom - length, mDrawPaint);
    }

订阅/RSS Feed

Subscribe

分类/category