Android textview custom digital scrolling animation

background

In the development needs, when there are total revenue, total number of users and other figures to be displayed, in order to better provide the display effect to users, they often want to add cool digital scrolling animation to make the rigid and calm figures flexible and give people a value-added intuitive feeling.

brief introduction

Numberrollingview is a user-defined textview with digital scrolling animation. Set the digital string by using setcontent (string STR) method. Pass in the corresponding amount digital string (such as "9686.86") or integer digital string (such as "968"), and the set number must be positive. When the initialization of the display page is completed, you can see the effect of digital scrolling.

design sketch:

use

1. Configure the custom attribute of numberrollingview in attr.xml

Framenum: the number of frames of animation. The default is 30 frames; Texttype: content format, including money (amount, two decimal places) and num (integer). The default is 0 (amount format); Usecommaformat: whether to set a comma for every three digits. The default value is true (set comma display); Runwhenchange: whether to use animation when the content changes. Otherwise, animation will not be used. The default is true (animation is used when the content changes);

2. Reference view in XML layout file

The former is the amount number view and the latter is the integer number view.

Call in 3.Java file

4. Cancel the automatic formatting of numbers (set a comma for every three digits)

By default, the displayed numbers are formatted, and a comma is added for every three digits, which makes the numbers look clear and not cumbersome. If you don't want to format numbers, you can set the usecommaformat property to false in the layout file, or set setusecommaformat (Boolean usecommaformat) to false in the code, so that the displayed numbers will not have commas.

5. Set whether the content changes or not to perform scrolling animation

By default, numberrollingview will automatically judge whether the incoming content has changed. When the content has changed, the digital scrolling animation will be executed. When the content has not changed, the digital scrolling animation will not be executed. If you want to execute the scrolling animation regardless of whether the content changes, you can set the runwhenchange property to false in the layout file, or set setrunwhenchange (Boolean runwhenchange) to false in the code, so that the scrolling animation will be executed regardless of whether the content changes.

6. Set the number of animation frames

By default, the number of frames of digital scrolling animation is 30. If you need to modify the number of animation frames, you can set the framenum attribute to the number of frames you want in the layout file or set setframenum (int framenum) to the number of frames you want in the code.

7. Number string formatting (add a comma every three digits)

8. How to start the digital animation of the amount

If the incoming amount contains formatted characters such as "," or "-" symbols, the symbols will be removed; Convert the format of the incoming number into the amount format of double type. If there is any conversion exception, it will be displayed directly; At the same time, judge the size of the incoming number. If it is 0, it will be displayed directly. If it is not 0, set the size of each frame interval and send a message message to the handler to update the content.

9. How to start digital animation

Like the above amount numbers, format symbols are removed first, and then type conversion is performed to convert them to integer format. If conversion exceptions occur, they will be displayed directly; In order to be more user-friendly, we compare the incoming number with the number of frames. If the number is less than the number of frames, it will be displayed directly. Of course, you want to display the animation regardless of the size of the number, just slightly modify the acquisition of the following frame interval; If it is greater than or equal to the set number of frames, set the size of each frame interval and send a message message to the handler to update the content.

Project address: Portal

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>