Has anyone tried to use `animateColorAsState` to t...
# compose
c
Has anyone tried to use
animateColorAsState
to targetColor then back to initialColor with colors that have alpha? I'm running into seemingly a bug? with using alpha colors. It will animate to the desired color, then go back to almost initialColor but with some of the targetColor applied as alpha then back to targetColor and finally it will go to the initialColor again with no alpha.
Copy code
Modifier.pulsateHighlightColor(initialColor: Color): Modifier = composed {
    val highlightColor = blue_12_percent
    val currentTargetColor = remember { mutableStateOf(initialColor) }
    DisposableEffect(Unit) {
        currentTargetColor.value = highlightColor
        onDispose {  }
    }

    val animatedColor = animateColorAsState(
        targetValue = currentTargetColor.value,
        animationSpec = tween(durationMillis = 750),
        finishedListener = {
            currentTargetColor.value = if (currentTargetColor.value == highlightColor) {
                initialColor
            } else {
                highlightColor
            }
        }
    )
    return@composed Modifier.background(animatedColor.value)
}
I've tried infiniteTransition as well and it has the same effect. Solid colors work fine. It seems it's only if you try to use one color as a solid and one that has alpha. Going from one alpha color to another seems to work fine as long as they're the same alpha value. Different alphas also doesn't work
d
Could you log the A, R, G, B channel changes explicitly during the animation between two colors with different alphas?
c
Sure.
Color(1.0, 1.0, 1.0, 1.0, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Color(1.0, 1.0, 1.0, 1.0, sRGB IEC61966-2.1)
Color(1.0, 1.0, 1.0, 1.0, sRGB IEC61966-2.1)
Color(1.0, 1.0, 1.0, 1.0, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1)
Sorry about the formatting! It doesn't appear that there's any difference. I'm going from a pure white to a blue with 12% alpha then back.
Although I'm just logging my mutableStateOf<Color> on recomposition. Not sure if that's what you were looking for
d
It may be helpful to also log when the animation starts, and the initial/target value. The timestamp would be useful as well.
I'm curious about
animatedColor.value
c
Yeah it looks like
animatedColor.value
is doing what I'm seeing 2021-07-07 141018.730 20467-20467/ D/Companion: Animation values: Color(1.0, 1.0, 1.0, 1.0, sRGB IEC61966-2.1) 2021-07-07 141018.798 20467-20467/ D/Companion: Animation values: Color(1.0, 1.0, 1.0, 1.0, sRGB IEC61966-2.1) 2021-07-07 141018.820 20467-20467/ D/Companion: Animation values: Color(0.99607843, 0.99607843, 1.0, 0.99607843, sRGB IEC61966-2.1) 2021-07-07 141018.831 20467-20467/ D/Companion: Animation values: Color(0.9843137, 0.99215686, 1.0, 0.9843137, sRGB IEC61966-2.1) 2021-07-07 141018.842 20467-20467/ D/Companion: Animation values: Color(0.9647059, 0.98039216, 1.0, 0.95686275, sRGB IEC61966-2.1) 2021-07-07 141018.854 20467-20467/ D/Companion: Animation values: Color(0.9254902, 0.9607843, 0.99607843, 0.9137255, sRGB IEC61966-2.1) 2021-07-07 141018.865 20467-20467/ D/Companion: Animation values: Color(0.87058824, 0.92941177, 0.99607843, 0.8509804, sRGB IEC61966-2.1) 2021-07-07 141018.884 20467-20467/ D/Companion: Animation values: Color(0.7921569, 0.88235295, 0.99215686, 0.7607843, sRGB IEC61966-2.1) 2021-07-07 141018.890 20467-20467/ D/Companion: Animation values: Color(0.7019608, 0.8352941, 0.9843137, 0.65882355, sRGB IEC61966-2.1) 2021-07-07 141018.897 20467-20467/ D/Companion: Animation values: Color(0.6117647, 0.78431374, 0.9764706, 0.5568628, sRGB IEC61966-2.1) 2021-07-07 141018.908 20467-20467/ D/Companion: Animation values: Color(0.5294118, 0.7372549, 0.96862745, 0.4627451, sRGB IEC61966-2.1) 2021-07-07 141018.920 20467-20467/ D/Companion: Animation values: Color(0.4627451, 0.7019608, 0.9607843, 0.3882353, sRGB IEC61966-2.1) 2021-07-07 141018.931 20467-20467/ D/Companion: Animation values: Color(0.40784314, 0.6745098, 0.95686275, 0.33333334, sRGB IEC61966-2.1) 2021-07-07 141018.942 20467-20467/ D/Companion: Animation values: Color(0.3647059, 0.6509804, 0.9490196, 0.28627452, sRGB IEC61966-2.1) 2021-07-07 141018.953 20467-20467/ D/Companion: Animation values: Color(0.3254902, 0.6313726, 0.94509804, 0.24705882, sRGB IEC61966-2.1) 2021-07-07 141018.964 20467-20467/ D/Companion: Animation values: Color(0.29411766, 0.6156863, 0.9411765, 0.21568628, sRGB IEC61966-2.1) 2021-07-07 141018.976 20467-20467/ D/Companion: Animation values: Color(0.26666668, 0.6, 0.9372549, 0.19215687, sRGB IEC61966-2.1) 2021-07-07 141018.987 20467-20467/ D/Companion: Animation values: Color(0.23921569, 0.5921569, 0.9372549, 0.17254902, sRGB IEC61966-2.1) 2021-07-07 141018.998 20467-20467/ D/Companion: Animation values: Color(0.21960784, 0.58431375, 0.93333334, 0.15686275, sRGB IEC61966-2.1) 2021-07-07 141019.008 20467-20467/ D/Companion: Animation values: Color(0.20392157, 0.5764706, 0.93333334, 0.14509805, sRGB IEC61966-2.1) 2021-07-07 141019.020 20467-20467/ D/Companion: Animation values: Color(0.19215687, 0.57254905, 0.92941177, 0.13333334, sRGB IEC61966-2.1) 2021-07-07 141019.031 20467-20467/ D/Companion: Animation values: Color(0.18431373, 0.5686275, 0.92941177, 0.12941177, sRGB IEC61966-2.1) 2021-07-07 141019.043 20467-20467/ D/Companion: Animation values: Color(0.1764706, 0.5647059, 0.92941177, 0.1254902, sRGB IEC61966-2.1) 2021-07-07 141019.055 20467-20467/ D/Companion: Animation values: Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1) 2021-07-07 141019.076 20467-20467/ D/Companion: Animation values: Color(0.17254902, 0.5647059, 0.92941177, 0.12156863, sRGB IEC61966-2.1) 2021-07-07 141019.099 20467-20467/ D/Companion: Animation values: Color(0.1764706, 0.5647059, 0.92941177, 0.1254902, sRGB IEC61966-2.1) 2021-07-07 141019.109 20467-20467/ D/Companion: Animation values: Color(0.19607843, 0.57254905, 0.93333334, 0.13725491, sRGB IEC61966-2.1)
the alpha goes from 1 to 12% (the blue) then back down to 12%(blue) instead of 1.
The initialColor is
Copy code
Color(0xFFFFFFFF)
and target
Copy code
Color(0x1f2c90ed)
Not sure where to put the log for when the animation starts.
d
I'm seeing the alpha going from 1f to 0.12ish. Then there seems to be a change in target, as all the ARGB values start changing towards Color(1f, 1f, 1f, 1f)
Just to double check, the intended behavior for the code snippet above is to have the color animating back and forth repeatedly between the initial color and target color?
c
Yep! I'm trying to get a video of it working (when using 2 colors with the same alpha) vs not working (different alphas)
d
I tried to reproduce this locally. What I'm seeing is that during the interpolation, the blue seems to get darker than 12% as it animates towards fully white. Is that the bug you are referring to?
Or is it something else? 🙂
c
Here's my examples
It seems like the alpha isn't fully interpolated out when going to another color without alpha
d
In the "notworking" video, I do see the color arriving at (fully opaquely) white. However the blue gets darker than the initial 12% during the animation, which may appear as though it's going the opposite direction than expected. But that's really just the result of animating the alpha value from 0.12 to 1f.
c
ohhhhh that's interesting.. that makes sense, thanks! I wonder if that's a timing issue then? Where the alpha value getting animated differs from the changing of the color RGB values
Do you have a suggested way of dealing with cases like this? My quick approach was to make sure their alpha values were the same but that's very flimsy.
d
Yea, the alpha is animating independently of the RGB values. My recommendation would be to compositing the translucent blue over the background as the target color. Something like this:
Copy code
Color(0x1f2c90ed).compositeOver(Color(0xffffffff))
💯 1
c
Wow. I was looking for a way to use Porter-Duff and you just unlocked it for me : D Thank you so much!
👍 1
d
BTW, it'd be easier/cleaner to use either
rememberInfiniteTransition
or
Animatable
to achieve the infinite animation between two values than
animateFooAsState
with a periodically changing target
c
Oh yes definitely. I had experimented with that and it's definitely cleaner and easier : - ) Thank you for all your work on this and on the samples. They helped me create some cool list animations
d
Glad to hear that! 😀
210 Views