本文主要是介绍绘制简单条形图(带一个直角的圆角矩形),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 一种方案是:path先添加一个圆角矩形,再添加一个角的正方形
- 另一种直接通过api ,radii 需要传入8个数值,分四组,从左上角开始
addRoundRect(RectF rect, float[] radii, Direction dir)
中间有个小点:如果在一个循环里,同时画柱子和文字,会出现前面3个柱子上的文字被柱子覆盖,只有最后1个柱子才展示文字标记。
比如说1是柱子,2是文字,画个柱子,画个文字;往右边挪挪位置,再画个柱子,再画个文字就是12121212的顺序,会出现前面3个2被1覆盖;使用两个循环,先把柱子全部画完,然后再画文字,就是11112222的顺序,这样是ok的
完整代码
class BarGraphView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0): View(context, attributeSet, defStyle) {private val item1BgColor = 0xFFA28AFF.toInt()private val item2BgColor = 0xFFD4EFFF.toInt()private val item1TextColor = Color.WHITEprivate val item2TextColor = 0xFF3FB6FF.toInt()private val textColor = 0xFF999999.toInt()private val titleColor = 0xFF666666.toInt()private val dividerColor = 0xFFEEEEEE.toInt()private val _11SP = DensityUtil.sp2px(11f).toFloat()private val _12SP = DensityUtil.sp2px(12f).toFloat()private val circleRadius = DensityUtil.dp2px(3.5f).toFloat()private val _8DP = DensityUtil.dp2px(8f).toFloat()private val _17DP = DensityUtil.dp2px(17f).toFloat()private val _24DP = DensityUtil.dp2px(24f).toFloat()private var itemWidth = 0fprivate val mPaint = Paint()private val paintItem1 = Paint()private val paintItem2 = Paint()private val pathItem1 = Path()private val pathItem2 = Path()private val rectFItem1 = RectF()private val rectFItem2 = RectF()private val radiiItem1: FloatArray = floatArrayOf(_8DP, _8DP, _8DP, _8DP, 0f, 0f, _8DP, _8DP)private val radiiItem2: FloatArray = floatArrayOf(_8DP, _8DP, _8DP, _8DP, _8DP, _8DP, 0f, 0f)
// private val rectFTemp = RectF()private var graphHeight = 0fprivate val verticalAxis = arrayListOf<String>("0%", "25%", "50%", "75%", "100%")private val horizontalAxis = arrayListOf<String>("第1节-xxx", "第2节", "第3节", "第4节")private val mData = arrayListOf<RateItem>()init {mPaint.isAntiAlias = truemPaint.style = Paint.Style.FILLmPaint.strokeWidth = 1fpaintItem1.isAntiAlias = truepaintItem1.style = Paint.Style.FILLpaintItem1.color = item1BgColorpaintItem2.isAntiAlias = truepaintItem2.style = Paint.Style.FILLpaintItem2.color = item2BgColorsetLayerType(View.LAYER_TYPE_SOFTWARE, mPaint)setLayerType(View.LAYER_TYPE_SOFTWARE, paintItem1)setLayerType(View.LAYER_TYPE_SOFTWARE, paintItem2)}override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {super.onSizeChanged(w, h, oldw, oldh)"w $w h $h oldw $oldw oldh $oldh".log()graphHeight = h - _17DPitemWidth = 0.08f * w}fun setData(data: List<RateItem>) {mData.clear()mData.addAll(data)invalidate()}override fun onDraw(canvas: Canvas) {canvas.translate(0f, 0.88f * height)//中心点
// mPaint.color = Color.RED
// canvas.drawCircle(0f, 0f, 10f, mPaint)//绘制标题栏的两个圆点mPaint.color = item1BgColorcanvas.drawCircle(circleRadius, -0.78f * height, circleRadius, mPaint)mPaint.color = item2BgColorcanvas.drawCircle(width / 2f - _8DP - circleRadius, -0.78f * height, circleRadius, mPaint)//绘制标题栏的两行文字mPaint.color = titleColormPaint.textSize = _12SPmPaint.typeface = Typeface.DEFAULT_BOLDcanvas.drawText("宝贝xxxxxxxxxx", 2 * circleRadius + _8DP, -0.76f * height, mPaint)canvas.drawText("全国xxxxxxxxxxxxxxxxxxx", width / 2f, -0.76f * height, mPaint)mPaint.textSize = _11SPmPaint.typeface = Typeface.DEFAULTval heightInterval = graphHeight / 6fval widthInterval = width / 4.5fvar stepY = heightInterval//绘制Y轴,百分比for (item in verticalAxis) {stepY -= heightIntervalmPaint.color = dividerColorcanvas.drawLine(0.4f * widthInterval, stepY, width.toFloat(), stepY, mPaint)mPaint.color = textColorcanvas.drawText(item, 0f, stepY + _11SP / 2f, mPaint)"heightInterval $heightInterval widthInterval $widthInterval stepY $stepY ".log()}//把Y轴高度分成100份,用于弥补数据源的非百分比stepY /= 100f//绘制X轴var stepX = -_17DPval halfTextWidth = mPaint.measureText("第1节") / 2ffor (index in 0 until horizontalAxis.size) {if (index >= mData.size) breakstepX += widthInterval//绘制X轴mPaint.textSize = _11SPmPaint.color = textColormPaint.typeface = Typeface.DEFAULTcanvas.drawText(horizontalAxis[index], stepX - (mPaint.measureText(horizontalAxis[index]) / 2f - halfTextWidth), heightInterval - _8DP, mPaint)//绘制条形图,计算x点val mid = stepX + halfTextWidthval left = mid - itemWidthval right = mid + itemWidth//绘制第一个竖状图val item1Height = stepY * mData[index].childRaterectFItem1.set(left, item1Height, mid, 0f)pathItem1.moveTo(mid, 0f)pathItem1.addRoundRect(rectFItem1, radiiItem1, Path.Direction.CCW)
// rectFTemp.set(mid - _8DP, -_8DP, mid, 0f)
// pathItem1.addRect(rectFTemp, Path.Direction.CCW)pathItem1.close()canvas.drawPath(pathItem1, paintItem1)//绘制第二个竖状图val item2Height = stepY * mData[index].averageRaterectFItem2.set(mid, item2Height, right, 0f)pathItem2.moveTo(mid, 0f)pathItem2.addRoundRect(rectFItem2, radiiItem2, Path.Direction.CW)
// rectFTemp.set(mid, -_8DP, mid + _8DP, 0f)
// pathItem2.addRect(rectFTemp, Path.Direction.CW)pathItem2.close()canvas.drawPath(pathItem2, paintItem2)"heightInterval $heightInterval widthInterval $widthInterval stepX $stepX pathItem1 $rectFItem1 pathItem2 $rectFItem2 _17DP$_17DP".log()}//绘制X轴stepX = -_17DPmPaint.textSize = _12SPmPaint.typeface = Typeface.DEFAULT_BOLDfor (index in 0 until horizontalAxis.size) {if (index >= mData.size) breakstepX += widthInterval//绘制条形图,计算x点val mid = stepX + halfTextWidthval left = mid - itemWidthval right = mid + itemWidthval item1Height = stepY * mData[index].childRateval item2Height = stepY * mData[index].averageRate//标记第一个竖状图的分数mPaint.color = item1TextColorval childRateStr = mData[index].childRate.toString()val x1 = (left + mid - mPaint.measureText(childRateStr)) / 2;val y1 = if (item1Height > -_24DP) item1Height else item1Height + _17DPcanvas.drawText(childRateStr, x1, y1, mPaint)//标记第二个竖状图的分数mPaint.color = item2TextColorval averageRateStr = mData[index].averageRate.toString()val x2 = (mid + right - mPaint.measureText(childRateStr)) / 2val y2 = if (item2Height > -_24DP) item2Height + _11SP else item2Height + _17DPcanvas.drawText(averageRateStr, x2, y2, mPaint)"childRateStr $childRateStr averageRateStr $averageRateStr text1 $x1 * $y1 text2 $x2 * $y2".log()
// "childRateStr $childRateStr averageRateStr $averageRateStr heightInterval $heightInterval widthInterval $widthInterval stepX $stepX pathItem1 $rectFItem1 pathItem2 $rectFItem2 _17DP$_17DP".log()}}}
这篇关于绘制简单条形图(带一个直角的圆角矩形)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!