https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
b

Benjamin Deroche

03/22/2022, 9:53 AM
I tried to make a "pulse" animation but the result is really bad, the icon inside the button keep "shaking" due to some rounding errors in the size and padding calculation.
Box(
    modifier = Modifier.fillMaxSize()
) {
    val infiniteTransition: InfiniteTransition = rememberInfiniteTransition()
    val initialSize: Float = 72f
    val pulsingSize: Float = 88f
    val size: Float by infiniteTransition.animateFloat(
        initialValue = initialSize,
        targetValue = pulsingSize,
        animationSpec = infiniteRepeatable(
            animation = tween(
                durationMillis = 1000,
                easing = LinearEasing
            ),
            repeatMode = RepeatMode.Reverse
        )
    )
    val paddingReduction: Float = (size - initialSize) / 2
    FloatingActionButton(
        modifier = Modifier
            .align(Alignment.BottomCenter)
            .padding((16f - paddingReduction).dp)
            .size(size.dp),
        onClick = {}
    ) {
        Icon(
            imageVector = Icons.Default.ArrowUpward,
            contentDescription = "To the top!"
        )
    }
}
f

f.babic

03/22/2022, 9:54 AM
Can you just use DP as the state you animate? So animateDp or something like that in the API instead of animateFloat? Also - try not to use linear easing but something else, because that's usually the least smooth easing 😄
b

Benjamin Deroche

03/22/2022, 10:02 AM
Like that? It seems even worst 😟
val infiniteTransition: InfiniteTransition = rememberInfiniteTransition()
val initialSize: Dp = 72.dp
val pulsingSize: Dp = 88.dp
val size: Dp by infiniteTransition.animateValue(
    initialValue = initialSize,
    targetValue = pulsingSize,
    typeConverter = Dp.VectorConverter,
    animationSpec = infiniteRepeatable(
        animation = tween(
            durationMillis = 1000,
            easing = FastOutSlowInEasing
        ),
        repeatMode = RepeatMode.Reverse
    )
)
FloatingActionButton(
    modifier = Modifier
        .align(Alignment.BottomCenter)
        .padding(16.dp - (size - initialSize) / 2)
        .size(size),
    backgroundColor = backgroundColor,
    contentColor = contentColor,
    onClick = {}
) {
    Icon(
        imageVector = Icons.Default.ArrowUpward,
        contentDescription = "To the top!"
    )
}
f

f.babic

03/22/2022, 10:05 AM
Can you debug to check the padding values? maybe it's got to do with diving by (2) rather than (2f) so the rounding could go wrong there
b

Benjamin Deroche

03/22/2022, 10:19 AM
I/System.out: Size: 72.0.dp | Padding: 16.0.dp I/System.out: Size: 87.12841.dp | Padding: 8.435795.dp I/System.out: Size: 87.72229.dp | Padding: 8.138855.dp I/System.out: Size: 78.69273.dp | Padding: 12.653633.dp I/System.out: Size: 86.10176.dp | Padding: 8.94912.dp I/System.out: Size: 87.75508.dp | Padding: 8.122459.dp I/System.out: Size: 87.99959.dp | Padding: 8.000206.dp I/System.out: Size: 87.81478.dp | Padding: 8.092609.dp I/System.out: Size: 87.195595.dp | Padding: 8.402203.dp I did the maths, the values are right so far, so the "noise" come from the final dp -> pixel conversion by Compose framework
Not sure if I can get around this problem using different composition stack, like make the FloatingActionButton centered in another Box that is 120dp and aligned bottom
f

f.babic

03/22/2022, 10:26 AM
Yeah I remember that the approach you're taking worked before the transition API changes: https://www.raywenderlich.com/13282144-jetpack-compose-animations-tutorial-getting-started it could be just an issue of adding padding and the size. Maybe the order of the params matters, so having padding after size could help - to make sure the padding is applied after the size is already drawn, but I'm not a 100% sure 😢
b

Benjamin Deroche

03/22/2022, 10:46 AM
The problem in my case is that, unlike in this tutorial, the icon inside my button stay the same. It's not supposed to move but due to the animation, the icon "shake" a lot. The border of the button is animated smoothly but not its icon.
👍 1
Ok I found a workaround, using
Modifier.scale()
instead of
Modifier.size()
on the Button. The icon inside the button grow too, so it's not exactly the animation I was trying to achieve, but at least it's smooth.
👍 1
f

f.babic

03/22/2022, 10:59 AM
Woo!
a

andrew

03/22/2022, 2:20 PM
If you need a pulse animation, this is my implementation from my work project:
@Composable
fun Pulse(
    modifier: Modifier = Modifier,
    tint: Color = LocalContentColor.current,
    active: Boolean = true,
    bounded: Boolean = true,
    count: Int = 3,
    duration: Int = 1000,
) {
    if (!active) return

    val infiniteTransition = rememberInfiniteTransition()
    val pulses = buildList {
        for (i in 0 until count) {
            add(infiniteTransition.animateFloat(
                initialValue = 0F,
                targetValue = 1F,
                animationSpec = infiniteRepeatable(
                    animation = tween(duration * count, easing = FastOutSlowInEasing),
                    initialStartOffset = StartOffset(i * duration),
                )
            ))
        }
    }

    Canvas(modifier.then(if (bounded) Modifier.clipToBounds() else Modifier)) {
        val (a, b) = size
        val r = sqrt(a.pow(2) + b.pow(2)) / 2

        pulses.forEach {
            val progress = it.value

            scale(progress) {
                drawCircle(tint.copy(alpha = (1F - progress) * 0.12F), r)
            }
        }
    }
}
Seems like you're using this in a fab, could probably throw this in a box and use it
FloatingActionButton(
                {},
                Modifier.align(Alignment.BottomEnd).navigationBarsPadding().padding(16.dp),
                backgroundColor = Theme.colors.primary,
            ) {
                Icon(Icons.Household.ic_fan_mode_on)
                Pulse(Modifier.size(56.dp))
            }
b

Benjamin Deroche

03/22/2022, 4:34 PM
@andrew Thank you for the code sample, will help a lot for the next steps!
a

andrew

03/22/2022, 4:35 PM
That is the effect you want to achieve, right? @Benjamin Deroche?
b

Benjamin Deroche

03/22/2022, 4:41 PM
@andrew This animation is a bit "light" for my use case, I need something more flashy, I was thinking more about something like that:

https://www.youtube.com/watch?v=SDJUbYU3Pzc&t=409s

But the code you shared is still a good step forward and will help me a lot. Thank you.
a

andrew

03/22/2022, 4:41 PM
You can modify the colors, etc easily
You would just put both inside a box, set the bounds of pulse, and put the fab on top 🙂
And it’s probably gonna be more performant, no layout happening on each pulse expansion, all transforms, canvas, etc, so no jumping around
You could also modify the pulse to allow children, I might do that too for my own uses
b

Benjamin Deroche

03/23/2022, 9:35 AM
Yes thank this was really helpful to get me started
I don't know if you've seen it but
drawCircle
accept an optional parameter
alpha
so you don't have to use
tint.copy
You can also simplify the function you used to build the list:
val pulses = List(count) { i ->
    infiniteTransition.animateFloat(
        initialValue = 0F,
        targetValue = 1F,
        animationSpec = infiniteRepeatable(
            animation = tween(duration * count, easing = FastOutSlowInEasing),
            initialStartOffset = StartOffset(i * duration),
        )
    )
}
a

andrew

03/23/2022, 2:38 PM
I am used to copying the color, but that works too
I also interchangeably use buildlist 😛