/** * 表格code */ private fun drawFormCode(canvas: Canvas) { //绘制表格边框 defaultDrawable.setBounds(space, 0, measuredWidth - space, measuredHeight) defaultBitmap = CodeHelper.drawableToBitmap(defaultDrawable, measuredWidth - 2 * space, measuredHeight) canvas.drawBitmap(defaultBitmap!!, space.toFloat(), 0f, mLinePaint) //绘制表格中心分割线 for (i in 1 until maxLength) { val startX = space + codeItemWidth * i + codeItemSpace * i val startY = 0f val stopY = measuredHeight canvas.drawLine(startX, startY, startX, stopY.toFloat(), mLinePaint) } //绘制当前位置边框 for (i in 0 until maxLength) { if (currentIndex != -1 && currentIndex == i && isCodeFocused) { when (i) { 0 -> { val radii = floatArrayOf(borderRadius, borderRadius, 0f, 0f, 0f, 0f, borderRadius, borderRadius) currentDrawable.cornerRadii = radii currentBitmap = CodeHelper.drawableToBitmap(currentDrawable, (codeItemWidth + borderWidth / 2).toInt(), measuredHeight) } maxLength - 1 -> { val radii = floatArrayOf(0f, 0f, borderRadius, borderRadius, borderRadius, borderRadius, 0f, 0f) currentDrawable.cornerRadii = radii currentBitmap = CodeHelper.drawableToBitmap(currentDrawable, (codeItemWidth + borderWidth / 2 + codeItemSpace).toInt(), measuredHeight) } else -> { val radii = floatArrayOf(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f) currentDrawable.cornerRadii = radii currentBitmap = CodeHelper.drawableToBitmap(currentDrawable, (codeItemWidth + borderWidth).toInt(), measuredHeight) } } val left = if (i == 0) (space + codeItemWidth * i) else ((space + codeItemWidth * i + codeItemSpace * i) - borderWidth / 2) canvas.drawBitmap(currentBitmap!!, left.toFloat(), 0f, mLinePaint) } } }6、绘制方块样式
/** * 方块 code */ private fun drawRectangleCode(canvas: Canvas) { defaultDrawable.cornerRadius = borderRadius defaultBitmap = CodeHelper.drawableToBitmap(defaultDrawable, codeItemWidth, measuredHeight) currentDrawable.cornerRadius = borderRadius currentBitmap = CodeHelper.drawableToBitmap(currentDrawable, codeItemWidth, measuredHeight) for (i in 0 until maxLength) { val left = if (i == 0) { space + i * codeItemWidth } else { space + i * codeItemWidth + codeItemSpace * i } //当前光标样式 if (currentIndex != -1 && currentIndex == i && isCodeFocused) { canvas.drawBitmap(currentBitmap!!, left.toFloat(), 0f, mLinePaint) } //默认样式 else { canvas.drawBitmap(defaultBitmap!!, left.toFloat(), 0f, mLinePaint) } } }7、绘制横线样式
/** * 横线 code */ private fun drawLineCode(canvas: Canvas) { for (i in 0 until maxLength) { //当前选中状态 if (currentIndex == i && isCodeFocused) { mLinePaint.color = borderSelectColor } //默认状态 else { mLinePaint.color = borderColor } val startX: Float = space + codeItemWidth * i + codeItemSpace * i val startY: Float = measuredHeight - borderWidth val stopX: Float = startX + codeItemWidth val stopY: Float = startY canvas.drawLine(startX, startY, stopX, stopY, mLinePaint) } }8、绘制圈圈样式
/** * 圆形 code */ private fun drawCircleCode(canvas: Canvas) { for (i in 0 until maxLength) { //当前绘制的圆圈的左x轴坐标 var left: Float = if (i == 0) { (space + i * codeItemWidth).toFloat() } else { space + i * codeItemWidth + codeItemSpace * i } //圆心坐标 val cx: Float = left + codeItemWidth / 2f val cy: Float = measuredHeight / 2f //圆形半径 val radius: Float = codeItemWidth / 5f //默认样式 if (i >= currentIndex) { canvas.drawCircle(cx, cy, radius, mLinePaint.apply { style = Paint.Style.FILL }) } } }10、绘制输入数据展示
/** * 绘制内容 */ private fun drawContentText(canvas: Canvas) { val textStr = text.toString() for (i in 0 until maxLength) { if (textStr.isNotEmpty() && i < textStr.length) { when (codeMode) { //笔墨 0 -> { val code: String = textStr.toString() val textWidth: Float = mTextPaint.measureText(code) val textHeight: Float = CodeHelper.getTextHeight(code, mTextPaint) val x: Float = space + codeItemWidth * i + codeItemSpace * i + (codeItemWidth - textWidth) / 2 val y: Float = (measuredHeight + textHeight) / 2f canvas.drawText(code, x, y, mTextPaint) } //TODO 拓展 } } } }上面就是对四种样式的绘制,重要观察的API如下: