0.

Yesterday saw the matrix again, how to say, after many years, is still so classic, when I was a child to see, but now there are a lot of smell. What impressed me most when I was a child was the digital rain, which caused me to write code with a black screen. So this morning, idle, took out two hours to write a digital rain control, directly above.

1.

Analyze first, then write. Here I’ve divided the code into two parts, a NumberRain for each column of NumberRainItem, and a NumberRain for all columns combined. NumberRain is a simple control that inherits from LinearLayout and adds a NumberRainItem after calculating the width. It’s easy, so I won’t do that here, but I’ll just talk about the NumberRainItem control.

2.

Each NumberRainItem is divided into two parts, the down-flow state, now called the down-flow state, where the last number is highlighted; The second state is the full screen state and is called the full state. The highlighted numbers in the full state are highlighted from the top down. Think about it and then do it.

3.

In the code

override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) configPaint() canvas? .let { if (isShowAllNumber()) { drawTotalNumbers(it) } else { drawPartNumbers(it) } } }Copy the code

DrawParNumbers () drawParNumbers () drawParNumbers () drawParNumbers () drawParNumbers () drawParNumbers () drawParNumbers () drawParNumbers () Which state to draw is determined by the isShowAllNumber method

private fun isShowAllNumber(): Boolean
{
    return nowHeight >= height
}
Copy the code

Where nowHeight is a member variable, representing the current height of all text, in the dirty state, each time more than one text, will increase the height of a text. Next, let’s look at drawTotalNumbers and drawPartNumbers

private fun drawPartNumbers(canvas: Canvas)
{
    val count = (nowHeight / textSize).toInt()
    nowHeight += textSize
    drawNumbers(canvas, count)

}

private fun drawTotalNumbers(canvas: Canvas)
{
    val count = (height / textSize).toInt()
    drawNumbers(canvas, count)
}
Copy the code

Both of them use drawNumbers, but the difference is that they pass in a different count, where count is the number of characters. So the most important thing is the drawNumbers method

private fun drawNumbers(canvas: Canvas, count: Int) {if (count == 0) {postInvalidateDelayed(startOffset)} else {var offset = 0f for (I in 0 until count) {// generate random text Val text = ((math.random () * 9).toint ()).toString() // If (hightLightNumIndex == I) {paint. Color = hightLightColor paint. hightLightColor) } else { paint.color = normalColor paint.setShadowLayer(10f, 0f, 0f, NormalColor)} // Draw canvas. DrawText (text, 0f, textSize + offset, paint) offset += textSize} isShowAllNumber()) { hightLightNumIndex++ } else { hightLightNumIndex = (++hightLightNumIndex) % count } postInvalidateDelayed(100L) } }Copy the code

This paragraph is mainly to draw the numbers, judge highlighting, it is not difficult, you can see the annotation.

4.

This control is not very difficult, but it feels very interesting to make,NumberRain and custom properties and so on, so you can just look at the code and put the github address at the end github