almost Marquee with Jetpack Compose. Try how you can construct and use a… | by Victor Brandalise | Nov, 2022 will cowl the newest and most present steering virtually the world. admittance slowly fittingly you comprehend competently and accurately. will mass your data precisely and reliably
Just a few weeks in the past, I used to be constructing a finances tracker with Compose and wanted a marquee textual content, or in different phrases, a element that might scroll the textual content if the textual content did not match inside its bounds.
Jetpack Compose does not present such a factor, so after some analysis I discovered this element.
It really works usually, however the code shouldn’t be that simple to know. As we speak we’ll overview the code to know the way it works and attempt to enhance it.
If the textual content suits inside its bounds, then the MarqueeText
works like a standard Textual content
element, if it does not, then one other Textual content
The element is created to offer that cool hover impact.
we are able to see the MarqueeText
the element has many fields, they’re virtually the identical fields as a standard element Textual content
element has as a result of the marquee mainly wraps it. The one distinction is gradientEdgeColor
which defines the colour on the element’s borders.
the createText
lambda is used to create the Textual content
element, is outlined as a lambda as a result of a number of cases of the Textual content
the element might be created.
then the offset
variable represents the x-offset of the primary textual content element, textLayoutInfoState
is used to retailer details about the textual content and width of the container.
Within the subsequent half we have now the code that performs the animation.
You could be asking “why was withFrameNanos
used right here?” There are different methods to attain comparable conduct, however let’s perceive why withFrameNanos
Specifically. Based on their documentation:
Suspended till a brand new body is requestedinstantly summon
onFrame
with the body time in nanoseconds within the body ship calling context, then resumes with the results of onFrame.
frameTimeNanos
must be used when calculating frame-by-frame animation time deltas, as it may be normalized to the goal time for the body, not essentially a direct “now” worth.
As you possibly can see, this operate sleeps till a brand new body is requested, which suggests you may have an opportunity to replace your animation earlier than a brand new body is drawn.
You probably have a 60fps display, which means the display refreshes 60 instances per second. For those who replace your animation 80 instances per second, you’re losing assets as a result of some updates won’t ever draw. The identical applies should you draw 15 instances per second, the consumer will discover a jerky animation.
withFrameNanos
solves that downside by suspending till a brand new body is requested so you do not have to fret concerning the body charge of the display.
The next a part of the code makes use of a SubcomposeLayout
to put the elements. It first begins by subcomposing the primary textual content element and measures it as if there have been no width restrictions. We have to do that to know whether or not or not the textual content will wrap throughout the constraints.
Variables are then outlined for the gradient and the second textual content element, they are often overridden as a result of they might not be wanted if the textual content conforms to their constraints.
If the textual content suits your constraints, then mainText
is up to date to fill the utmost width. additionally units textLayoutInfoState
to null in order that the animation doesn’t run.
If the textual content doesn’t match inside your constraints, the gradient and second textual content elements are set.
Lastly the elements are organized.
Initially, the marquee element appears advanced, however analyzing it little by little we are able to see that it’s not that advanced.
Whereas doing this evaluation, I discovered some issues that might be improved within the code. I’ll solely point out just a few of the adjustments I made, if you wish to overview all of them, you could find a hyperlink to the supply code on the finish of the article.
Inside LaunchedEffect
block there are two whereas loops that look suspicious, the primary is whereas(true)
, it may be eliminated with out affecting something. the second is whereas (!animation.isFinishedFromNanos(playTime))
as we all know animation by no means ends so i modified it to whereas (isActive)
this returns true whereas Coroutine is lively.
I additionally eliminated the delay, I do not suppose it’s a necessity for a marquee.
We are able to additionally change the MarqueeText
element to observe the slot sample. I created a brand new element known as Marquee
and altered his signature to simply accept any sort of content material.
That method you possibly can cross the Marquee
any content material you need, even when it isn’t textual content.
That is the ultimate outcome
I want the article almost Marquee with Jetpack Compose. Try how you can construct and use a… | by Victor Brandalise | Nov, 2022 provides keenness to you and is beneficial for including collectively to your data
Marquee with Jetpack Compose. Check out how to build and use a… | by Victor Brandalise | Nov, 2022