标签:
主要是介绍使用ZXing库进行条形码和二维码的扫描
本帖里面使用的库是别人精简以后的
在代码注释里面作者 - Ryan.Tang
这里主要是介绍一下关于扫描界面
下面是关于属性的介绍
ScanView核心源码解析
ScanView 自定义属性
<declare-styleable name="ScanView"> <!--指定扫描区域的背景图片--> <attr name="scanBackground" format="reference" /> <!--指定扫描区域的尺寸--> <attr name="scanWidth" format="dimension" /> <attr name="scanHeight" format="dimension" /> <!--指定扫描区域周围的颜色--> <attr name="aroundColor" format="color" /> <!--扫描区域到顶部的距离--> <attr name="scanTopDistance" format="dimension" /> <!--扫描的模式--> <attr name="scanMode" format="enum"> <enum name="none" value="0" /><!--什么都没有--> <enum name="line" value="1" /><!--有扫描线--> <enum name="point" value="2" /><!--有闪烁点--> <enum name="both" value="3" /><!--都有--> </attr> <!--扫描线的高度--> <attr name="lineHeight" format="dimension" /> <!--扫描线的颜色--> <attr name="lineColor" format="color" /> <!--边框宽度--> <attr name="frameBorder" format="dimension" /> <!--边框颜色--> <attr name="frameColor" format="color" /> <!--指定扫描线的图片--> <attr name="lineSrc" format="reference" /> <!--指定闪烁点的颜色--> <attr name="pointColor" format="color" /> <!--扫描模式状态--> <attr name="scanStatus" format="enum"> <enum name="slow" value="1" /><!--慢--> <enum name="normal" value="0" /><!--正常--> <enum name="fast" value="2" /><!--快--> </attr> <!--扫描线的滚动模式--> <attr name="lineScrollMode" format="enum"> <enum name="repeat" value="0" /><!--重复--> <enum name="cycle" value="1" /><!--循环--> </attr> <!--扫描超时时间--> <attr name="scanTimeValue" format="integer" /> <!--扫描时间单位 支持 秒 分钟--> <attr name="scanTimeUnit" format="enum"> <enum name="second" value="0" /> <enum name="minute" value="1" /> </attr> </declare-styleable>
绘制 扫描区域 周围
分为 是否设置背景图片俩种情况
private void drawAround(Canvas canvas) { if (mScanBg == null) { canvas.drawRect(0, 0, screenWidth, scanTopDistance - frameBorder, mPaintShape); canvas.drawRect(0, scanTopDistance - frameBorder, scanHorizontalDsitance - frameBorder, scanTopDistance + scanHeight + frameBorder, mPaintShape); canvas.drawRect(scanHorizontalDsitance + scanWidth + frameBorder, scanTopDistance - frameBorder, screenWidth, scanTopDistance + scanHeight + frameBorder, mPaintShape); canvas.drawRect(0, scanTopDistance + scanHeight + frameBorder, screenWidth, screenHeight, mPaintShape); } else { // 绘制周边 canvas.drawRect(0, 0, screenWidth, scanTopDistance, mPaintShape); canvas.drawRect(0, scanTopDistance, scanHorizontalDsitance, scanTopDistance + scanHeight, mPaintShape); canvas.drawRect(scanHorizontalDsitance + scanWidth, scanTopDistance, screenWidth, scanTopDistance + scanHeight, mPaintShape); canvas.drawRect(0, scanTopDistance + scanHeight, screenWidth, screenHeight, mPaintShape); } }绘制 扫描区域
分为 是否设置背景图片 如果没有设置背景图片 则根据指定大小或者是默认大小绘制一个方框
private void drawScanArea(Canvas canvas) { // 绘制扫描区域 if (mScanBg == null) { // 没有设置背景图片使用默认的方框 mPaintShape.setStyle(Paint.Style.STROKE); mPaintShape.setStrokeWidth(frameBorder); mPaintShape.setColor(frameColor); // 画一个边框 canvas.drawRect(scanHorizontalDsitance - frameBorder, scanTopDistance - frameBorder, scanHorizontalDsitance + scanWidth + frameBorder, scanTopDistance + scanHeight + frameBorder, mPaintShape); } else { // 设置背景 canvas.drawBitmap(mScanBg, scanHorizontalDsitance, scanTopDistance, mPaintBitmap); } }绘制扫描线
private void drawLine(Canvas canvas) { if (mScanLine != null) { canvas.drawBitmap(mScanLine, scanHorizontalDsitance + (scanWidth - mScanLine.getWidth()) / 2, linePositionHeight, mPaintBitmap); } else { mPaintShape.setStyle(Paint.Style.FILL); mPaintShape.setColor(lineColor); canvas.drawLine(scanHorizontalDsitance, linePositionHeight, scanHorizontalDsitance + scanWidth, linePositionHeight + lineHeight, mPaintShape); } switch (lineSrollMode) { case LINE_SCROLL_MODE_REPEAT: if (linePositionHeight > scanTopDistance + scanHeight) linePositionHeight = scanTopDistance; linePositionHeight += lineMoveSpeed; break; case LINE_SCROLL_MODE_CYCLE: if (!upFlag) { linePositionHeight += lineMoveSpeed; } else { linePositionHeight -= lineMoveSpeed; } if (linePositionHeight > scanTopDistance + scanHeight) upFlag = true; if (linePositionHeight < scanTopDistance) upFlag = false; break; } }
在 drawLine 通过控制可以矩形的高度可以实现和UC 扫一扫一样的效果
添加如下代码
mPaintShape.setColor(Color.parseColor("#4455ff55")); canvas.drawRect(scanHorizontalDsitance, scanTopDistance, scanHorizontalDsitance + scanWidth, linePositionHeight, mPaintShape);
绘制闪烁点
private void drawPoint(Canvas canvas) { if (currentPoints.isEmpty()) { lastPoints = null; } else { // 记录当前的数据到上一次位置集合中 lastPoints = currentPoints; // 设置绘制闪烁点的画笔 mPaintShape.setAlpha(0xff); mPaintShape.setColor(pointColor); mPaintShape.setStyle(Paint.Style.FILL); // 循环绘制 for (ResultPoint point : currentPoints) { // 绘制闪烁点 canvas.drawCircle(scanHorizontalDsitance + point.getX(), scanTopDistance + point.getY(), 6.0f, mPaintShape); } } if (lastPoints != null) { // 设置绘制闪烁点的画笔 mPaintShape.setAlpha(0xff / 2); mPaintShape.setColor(pointColor); mPaintShape.setStyle(Paint.Style.FILL); // 循环绘制 for (ResultPoint point : lastPoints) { // 绘制闪烁点 canvas.drawCircle(scanHorizontalDsitance + point.getX(), scanTopDistance + point.getY(), 3.0f, mPaintShape); } } // 清空本次数据 currentPoints.clear(); }闪烁点的数据来源
这个方法内部自动调用
public void addPossibleResultPoint(ResultPoint point) { // 添加该点到集合中 currentPoints.add(point); }
完整库文件下载地址
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/it_transformers/article/details/47294537