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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions coil-base/src/main/java/coil/drawable/CrossfadeDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import kotlin.math.roundToInt
* @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?,
private val end: Drawable?,
start: Drawable?,
end: Drawable?,
val scale: Scale = Scale.FIT,
val durationMillis: Int = DEFAULT_DURATION,
val fadeStart: Boolean = true
Expand All @@ -43,15 +43,19 @@ class CrossfadeDrawable @JvmOverloads constructor(
private val intrinsicWidth = computeIntrinsicDimension(start?.intrinsicWidth, end?.intrinsicWidth)
private val intrinsicHeight = computeIntrinsicDimension(start?.intrinsicHeight, end?.intrinsicHeight)

private var start: Drawable?
private val end: Drawable?
private var startTimeMillis = 0L
private var maxAlpha = 255
private var state = STATE_START

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

start?.callback = this
end?.callback = this
this.start = start?.mutate()
this.end = end?.mutate()
this.start?.callback = this
this.end?.callback = this
}

override fun draw(canvas: Canvas) {
Expand Down
28 changes: 28 additions & 0 deletions coil-base/src/test/java/coil/drawable/CrossfadeDrawableTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coil.drawable

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
Expand Down Expand Up @@ -159,6 +160,33 @@ class CrossfadeDrawableTest {
assertEquals(Rect(0, 70, 200, 140), end.bounds)
}

@Test
fun `alpha change should not change alpha of original start drawable`() {
val startDrawable = TestBitmapDrawable(100, 100)
val endDrawable = TestBitmapDrawable(100, 100)
assertEquals(0, startDrawable.alpha)

val crossfadeDrawable = CrossfadeDrawable(startDrawable, endDrawable)
crossfadeDrawable.alpha = 255
crossfadeDrawable.draw(Canvas())

assertEquals(0, startDrawable.alpha)
}

@Test
fun `alpha change should not change alpha of original end drawable`() {
val startDrawable = TestBitmapDrawable(100, 100)
val endDrawable = TestBitmapDrawable(100, 100)
assertEquals(0, endDrawable.alpha)

val crossfadeDrawable = CrossfadeDrawable(startDrawable, endDrawable)
crossfadeDrawable.alpha = 255
crossfadeDrawable.stop()
crossfadeDrawable.draw(Canvas())

assertEquals(0, endDrawable.alpha)
}

private class TestDrawable(
private val width: Int,
private val height: Int
Expand Down