<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android繪製簡單條形圖的具體程式碼,供大家參考,具體內容如下
中間有個小點:如果在一個迴圈裡,同時畫柱子和文字,會出現前面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.WHITE private 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 = 0f private 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 = 0f private 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 = true mPaint.style = Paint.Style.FILL mPaint.strokeWidth = 1f paintItem1.isAntiAlias = true paintItem1.style = Paint.Style.FILL paintItem1.color = item1BgColor paintItem2.isAntiAlias = true paintItem2.style = Paint.Style.FILL paintItem2.color = item2BgColor setLayerType(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 - _17DP itemWidth = 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 = item1BgColor canvas.drawCircle(circleRadius, -0.78f * height, circleRadius, mPaint) mPaint.color = item2BgColor canvas.drawCircle(width / 2f - _8DP - circleRadius, -0.78f * height, circleRadius, mPaint) //繪製標題列的兩行文字 mPaint.color = titleColor mPaint.textSize = _12SP mPaint.typeface = Typeface.DEFAULT_BOLD canvas.drawText("寶貝xxxxxxxxxx", 2 * circleRadius + _8DP, -0.76f * height, mPaint) canvas.drawText("全國xxxxxxxxxxxxxxxxxxx", width / 2f, -0.76f * height, mPaint) mPaint.textSize = _11SP mPaint.typeface = Typeface.DEFAULT val heightInterval = graphHeight / 6f val widthInterval = width / 4.5f var stepY = heightInterval //繪製Y軸,百分比 for (item in verticalAxis) { stepY -= heightInterval mPaint.color = dividerColor canvas.drawLine(0.4f * widthInterval, stepY, width.toFloat(), stepY, mPaint) mPaint.color = textColor canvas.drawText(item, 0f, stepY + _11SP / 2f, mPaint) "heightInterval $heightInterval widthInterval $widthInterval stepY $stepY ".log() } //把Y軸高度分成100份,用於彌補資料來源的非百分比 stepY /= 100f //繪製X軸 var stepX = -_17DP val halfTextWidth = mPaint.measureText("第1節") / 2f for (index in 0 until horizontalAxis.size) { if (index >= mData.size) break stepX += widthInterval //繪製X軸 mPaint.textSize = _11SP mPaint.color = textColor mPaint.typeface = Typeface.DEFAULT canvas.drawText(horizontalAxis[index], stepX - (mPaint.measureText(horizontalAxis[index]) / 2f - halfTextWidth), heightInterval - _8DP, mPaint) //繪製條形圖,計算x點 val mid = stepX + halfTextWidth val left = mid - itemWidth val right = mid + itemWidth //繪製第一個豎狀圖 val item1Height = stepY * mData[index].childRate rectFItem1.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].averageRate rectFItem2.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 = -_17DP mPaint.textSize = _12SP mPaint.typeface = Typeface.DEFAULT_BOLD for (index in 0 until horizontalAxis.size) { if (index >= mData.size) break stepX += widthInterval //繪製條形圖,計算x點 val mid = stepX + halfTextWidth val left = mid - itemWidth val right = mid + itemWidth val item1Height = stepY * mData[index].childRate val item2Height = stepY * mData[index].averageRate //標記第一個豎狀圖的分數 mPaint.color = item1TextColor val childRateStr = mData[index].childRate.toString() val x1 = (left + mid - mPaint.measureText(childRateStr)) / 2; val y1 = if (item1Height > -_24DP) item1Height else item1Height + _17DP canvas.drawText(childRateStr, x1, y1, mPaint) //標記第二個豎狀圖的分數 mPaint.color = item2TextColor val averageRateStr = mData[index].averageRate.toString() val x2 = (mid + right - mPaint.measureText(childRateStr)) / 2 val y2 = if (item2Height > -_24DP) item2Height + _11SP else item2Height + _17DP canvas.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() } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45