Source code analysis of Android marqueeview

The horse lantern effect can be browsed by the original author https://github.com/sfsheng0322/MarqueeView Let's look at the code of the custom control

The racing lantern view inherits the viewflipper, and its structure can be seen

In fact, the working mechanism of viewflipper is very simple, as shown in the figure above, that is, the sub views added to viewflipper are displayed regularly in order. One of the sub views is set to go state, and the other sub views are set to go state

See some properties of the view, context, collection, animation delay, click event, time interval of the running lantern, animation delay time, font size and color, set the position to the left, align vertically in the middle, and the position constant of the text.

Modify the construction method of marqueeview, and we can quickly generate it by right clicking generate.

TypedArray typedArray = getContext().obtainStyledAttributes(attrs,0);

First, get the attribute set and get an MV interval. The default value is 2000

isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);

Whether to set the delay of animation time

If you set a custom text size, get it, and then convert PX to SP to get the control position. You can set it freely and recycle it later

setFlipInterval(interval); Sets the scrolling interval in milliseconds

One in one out animation

//Start the rotation according to the announcement string. Public void startwithtext (final string notice) exposes a public method, which contains the getviewtreeobserver method for measuring the view. The internal class calls startwithfixedwidth (notice, getwidth()); method

Convert to a DP width to limit the number of words and length. If the string is less than direct add. If it is too long, the remainder is obtained. Then cycle to get the beginning and end of that line. If the number of words in the two lines at the end is still greater than the overall length, take the overall length. If it is less than the overall length, take the end of that line, as shown in the table below. Then start playing

Create and deploy TV for each row

Then add all textview and start playing. The next step is to make a click callback, and then set get the collection of announcements. Finally, don't forget to add at the top of the layout

xmlns:app=" http://schemas.android.com/apk/res-auto "

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