自定义View(初)-直方图

136 阅读1分钟
效果图

Screenshot_2021-10-20-13-40-20-231_com.example.custom1019.jpg `

package com.example.custom1019.test1019

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.view.View
import com.example.custom1019.testmodel.HistogramModel

class HistogramView : View {
    private val dataList: List<HistogramModel> by lazy {
        mutableListOf(
            HistogramModel("Froyo", 1),
            HistogramModel("GB", 8),
            HistogramModel("ICS", 3),
            HistogramModel("JB", 4),
            HistogramModel("KitKat", 7),
            HistogramModel("L", 9),
            HistogramModel("M", 6),
            HistogramModel("M", 6),
            HistogramModel("M", 6),
            HistogramModel("M", 6),
        )
    }
    private var lineWidth: Float = 0f
    private var lineHeight: Float = 0f
    private var spacing: Float = 10f
    private val linePaint: Paint by lazy {
        Paint().also {
            it.strokeWidth = 1f
            it.color = Color.WHITE
        }
    }
    private val rect: Rect by lazy {
        Rect()
    }
    private val rectPaint: Paint by lazy {
        Paint().also {
            it.color = Color.GRAY
            it.style = Paint.Style.FILL
            it.strokeWidth = 1f
            it.isAntiAlias = true
        }
    }
    private val textSize: Float = dip2px(12f)
    private val textPaint: Paint by lazy {
        Paint().also {
            it.isAntiAlias = true
            it.color = Color.WHITE
            it.textSize = textSize
            it.textAlign = Paint.Align.CENTER
        }
    }

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        lineWidth = w.toFloat()
        lineHeight = h.toFloat() - textSize
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        drawSideList(canvas)
        drawBottomText(canvas)
        canvas?.drawLine(0f, 0f, 0f, lineHeight, linePaint)
        canvas?.drawLine(0f, lineHeight - 1f, lineWidth, lineHeight - 1f, linePaint)
    }

    private fun drawSideList(canvas: Canvas?) {
        var sideWith = (lineWidth / dataList.size) - (spacing * 2)
        for (i in dataList.indices) {
            var num = dataList[i].num
            var sideHeight = (num.toFloat() / 10f) * lineHeight//直方图高
            var left = (spacing * (i + 1)) + (sideWith * i)
            var top = lineHeight - sideHeight
            var right = ((spacing * (i + 1)) + (sideWith * i) + sideWith)//在left的基础上添加了一个宽度
            var bottom = lineHeight
            rect.set(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())
            canvas?.drawRect(rect, rectPaint)
        }

    }
    //绘制底部Text
    private fun drawBottomText(canvas: Canvas?) {
        var sideWith = (lineWidth / dataList.size) - (spacing * 2)
        for (i in dataList.indices) {
            var x = (spacing * (i + 1)) + ((sideWith * i) + (sideWith / 2))
            var y = lineHeight + textSize
            canvas?.drawText(dataList[i].name, x,y,textPaint)
        }
    }

    fun dip2px(dipValue: Float): Float {
        val metrics = context.resources.displayMetrics
        return (dipValue * (metrics.densityDpi / 160f))
    }
}

`