Kotlin 自定義View — 液晶數(shù)字顯示

效果預(yù)覽

液晶數(shù)字.png

上代碼

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import java.util.*

/**
 * 仿液晶數(shù)字顯示
 */
class DigitalView : View {
    private val paint: Paint = Paint()
    private var strokeColor = Color.BLACK
    private var hiddenColor = Color.LTGRAY
    var digital: Int? = null
        set(value) {
            field = value
            bitSet.set(0, 7, false)
            field?.let {
                when (it) {
                    0 -> {
                        bitSet.set(0, 6, true)
                        bitSet.set(6, false)
                    }

                    1 -> bitSet.set(1, 3, true)

                    2 -> {
                        bitSet.set(0, 2, true)
                        bitSet.set(3, 5, true)
                        bitSet.set(6, true)
                    }

                    3 -> {
                        bitSet.set(0, 4, true)
                        bitSet.set(6, true)
                    }

                    4 -> {
                        bitSet.set(1, 3, true)
                        bitSet.set(5, true)
                        bitSet.set(6, true)
                    }

                    5 -> {
                        bitSet.set(2, 4, true)
                        bitSet.set(0, true)
                        bitSet.set(5, true)
                        bitSet.set(6, true)
                    }
                    6 -> {
                        bitSet.set(2, 7, true)
                        bitSet.set(0, true)
                    }

                    7 -> {
                        bitSet.set(0, 3, true)
                    }

                    8 -> bitSet.set(0, 7, true)

                    9 -> {
                        bitSet.set(0, 7, true)
                        bitSet.set(4, false)
                    }
                }
                invalidate()
            }
        }
    private val bitSet = BitSet(8)
    private val strokePathArr = Array(7) { Path() }

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

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context?, attributeSet: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attributeSet, defStyleAttr, defStyleRes)

    init {
        paint.flags = paint.flags or Paint.ANTI_ALIAS_FLAG
        paint.color = strokeColor
        bitSet.set(0, 7, false)
        strokePathArr[0]
        hiddenColor = Color.parseColor("#22CCCCCC")
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = View.MeasureSpec.getMode(widthMeasureSpec)
        var widthSize = MeasureSpec.getSize(widthMeasureSpec)
        if (widthMode == MeasureSpec.AT_MOST && widthSize == 0) {
            widthSize = resources.displayMetrics.densityDpi * 20
        }
        val paddingBetweenBlock = (widthSize / 100).coerceAtLeast(1)
        val small = widthSize.toFloat() / 9
        val heightSize = widthSize * 16 / 9
        if (heightMeasureSpec != MeasureSpec.getSize(widthMeasureSpec)) {
            setMeasuredDimension(widthSize, heightSize)
            Log.d("TAG", "setMeasuredDimension: ")
            strokePathArr.forEach {
                it.reset()
            }
            strokePathArr[0].apply {
                moveTo(small + paddingBetweenBlock, small)
                lineTo(2 * small, 0F + paddingBetweenBlock)
                lineTo(widthSize - 2 * small, 0F + paddingBetweenBlock)
                lineTo(widthSize - small - paddingBetweenBlock, small)
                lineTo(widthSize - 2 * small, 2 * small - paddingBetweenBlock)
                lineTo(2 * small, 2 * small - paddingBetweenBlock)
                close()
            }

            strokePathArr[1].apply {
                moveTo(widthSize - small, small + paddingBetweenBlock)
                lineTo(widthSize.toFloat() - paddingBetweenBlock, 2 * small)
                lineTo(widthSize.toFloat() - paddingBetweenBlock, 7 * small)
                lineTo(widthSize - small, 8 * small - paddingBetweenBlock)
                lineTo(widthSize - 2 * small + paddingBetweenBlock, 7 * small)
                lineTo(widthSize - 2 * small + paddingBetweenBlock, 2 * small)
                close()
            }

            strokePathArr[2].apply {
                moveTo(widthSize - small, 8 * small + paddingBetweenBlock)
                lineTo(widthSize.toFloat() - paddingBetweenBlock, 9 * small)
                lineTo(widthSize.toFloat() - paddingBetweenBlock, 14 * small)
                lineTo(widthSize - small, 15 * small - paddingBetweenBlock)
                lineTo(widthSize - 2 * small + paddingBetweenBlock, 14 * small)
                lineTo(widthSize - 2 * small + paddingBetweenBlock, 9 * small)
                close()
            }

            strokePathArr[3].apply {
                moveTo(small + paddingBetweenBlock, 15 * small)
                lineTo(2 * small, 14 * small + paddingBetweenBlock)
                lineTo(widthSize - 2 * small, 14 * small + paddingBetweenBlock)
                lineTo(widthSize - small - paddingBetweenBlock, 15 * small)
                lineTo(widthSize - 2 * small, 16 * small - paddingBetweenBlock)
                lineTo(2 * small, 16 * small - paddingBetweenBlock)
                close()
            }
            strokePathArr[4].apply {
                moveTo(small, 8 * small + paddingBetweenBlock)
                lineTo(2 * small - paddingBetweenBlock, 9 * small)
                lineTo(2 * small - paddingBetweenBlock, 14 * small)
                lineTo(small, 15 * small - paddingBetweenBlock)
                lineTo(0F + paddingBetweenBlock, 14 * small)
                lineTo(0F + paddingBetweenBlock, 9 * small)
                close()
            }
            strokePathArr[5].apply {
                moveTo(small, small + paddingBetweenBlock)
                lineTo(2 * small - paddingBetweenBlock, 2 * small)
                lineTo(2 * small - paddingBetweenBlock, 7 * small)
                lineTo(small, 8 * small - paddingBetweenBlock)
                lineTo(0F + paddingBetweenBlock, 7 * small)
                lineTo(0F + paddingBetweenBlock, 2 * small)
                close()
            }
            strokePathArr[6].apply {
                moveTo(small + paddingBetweenBlock, 8 * small)
                lineTo(2 * small, 7 * small + paddingBetweenBlock)
                lineTo(widthSize - 2 * small, 7 * small + paddingBetweenBlock)
                lineTo(widthSize - small - paddingBetweenBlock, 8 * small)
                lineTo(widthSize - 2 * small, 9 * small - paddingBetweenBlock)
                lineTo(2 * small, 9 * small - paddingBetweenBlock)
                close()
            }

        } else {
            Log.d("TAG", "onMeasure: ")
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        strokePathArr.forEachIndexed { index, it ->
            paint.color = if (bitSet.get(index)) {
                strokeColor
            } else {
                hiddenColor
            }
            canvas?.drawPath(it, paint)
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容