Detailed explanation of vertical running lamp effect realized by Android custom view

First take a look at the effect picture (the recorded GIF is a little stuck, and the real effect is still very smooth)

Realization idea

It can be concluded from the GIF diagram above that in fact, it is to draw two text messages at the same time, and then continuously change the height of the two text messages from the top through animation, so as to achieve the effect of scrolling.

Concrete implementation

First, define some attributes to be used

Next, parse the attribute value

Override onmeasure method

When the data is empty, call the method of the parent class. After setting the data, calculate the width and height according to different layouts.

Set data

The inittextrect() method calculates the height of a single text and the width of the largest text in the array. The height of the text is used to calculate the position when drawing the text. The maximum width of the text is used in the onmeasure() method.

The settextcurrentornextstatus() method sets the current position, text, and the next position, text. There is also the initialization height of the text from the top.

The startanimation () method starts executing the animation.

Animation implementation

va.setRepeatCount(-1); Animate infinite repetition

The onanimationupdate () method obtains the progress of animation execution, calculates the distance from the text to the top, and calls the postinvalidate () method to refresh the interface.

Onanimationrepeat() method, delay the repeatdelaytime time through the handler, and then re execute the animation.

draw

summary

So far, all the code has been analyzed. In fact, there are many ways to achieve this effect. You can achieve it by inheriting ViewGroup. If you are interested, you can try it yourself. I hope the content of this article can be helpful to your study or work. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>