Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutate crossfade start/end drawables #572

Merged
merged 4 commits into from
Nov 5, 2020

Conversation

Raenar4k
Copy link
Contributor

Fixes #571

@@ -24,14 +24,14 @@ import kotlin.math.roundToInt
*
* NOTE: The animation can only be executed once as the [start] drawable is dereferenced at the end of the transition.
*
* @param start The [Drawable] to crossfade from.
* @param start The [Drawable] to crossfade from. Will be mutated to prevent modifying alpha of original drawable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to mention mutation in the docs.

* @param end The [Drawable] to crossfade to.
* @param scale The scaling algorithm for [start] and [end].
* @param durationMillis The duration of the crossfade animation.
* @param fadeStart If false, the start drawable will not fade out while the end drawable fades in.
*/
class CrossfadeDrawable @JvmOverloads constructor(
private var start: Drawable?,
start: Drawable?,
private val end: Drawable?,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably mutate end as well since it could be loaded from resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, didn't think about that! Since we can provide something like error placeholder. Tested it in sample, same bug
#571 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Not sure if tests are good enough, they are testing drawable changes, but with a bit of a hack - since percent of animation is tied to SystemClock and can't be influenced directly.

private var startTimeMillis = 0L
private var maxAlpha = 255
private var state = STATE_START

init {
require(durationMillis > 0) { "durationMillis must be > 0." }

this.start = start?.mutate()
start?.callback = this
end?.callback = this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be this.start and this.end? drawable.mutate is supposed to return the same drawable, but there's nothing to prevent a bad drawable from returning a new drawable.

@colinrtwhite
Copy link
Member

Thanks for finding + fixing this!

@Raenar4k Raenar4k changed the title Mutate crossfade start drawable Mutate crossfade start/end drawables Nov 4, 2020
@Raenar4k Raenar4k force-pushed the mutate_crossfade_drawable branch from dc45bb0 to faff35d Compare November 4, 2020 23:03
@Raenar4k Raenar4k force-pushed the mutate_crossfade_drawable branch from faff35d to fb0c458 Compare November 4, 2020 23:11
Copy link
Member

@colinrtwhite colinrtwhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

@colinrtwhite colinrtwhite merged commit f724df5 into coil-kt:master Nov 5, 2020
IljaKosynkin pushed a commit to IljaKosynkin/coil that referenced this pull request Nov 6, 2020
* Add failing test for crossfade drawable alpha bug (coil-kt#571)

* Change CrossfadeDrawable constructor to mutate start drawable (coil-kt#571)

* Add test for both start/end drawables in CrossfadeDrawableTest (coil-kt#571)

* Change CrossfadeDrawable constructor to mutate both start/end drawables (coil-kt#571)

Remove mention of drawable mutation from CrossfadeDrawable doc (coil-kt#571)
colinrtwhite pushed a commit that referenced this pull request Oct 5, 2022
* Add failing test for crossfade drawable alpha bug (#571)

* Change CrossfadeDrawable constructor to mutate start drawable (#571)

* Add test for both start/end drawables in CrossfadeDrawableTest (#571)

* Change CrossfadeDrawable constructor to mutate both start/end drawables (#571)

Remove mention of drawable mutation from CrossfadeDrawable doc (#571)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using crossfade together with placeholder changes placeholder drawable alpha
2 participants