Skip to content

Commit d10c5ff

Browse files
committed
Add a Transition API and a CrossFade Transition for Compose
Similar to Glide's existing Transitions, but intended for use with Animatable instead of views. I've started with a simple CrossFade.
1 parent cb2b1dc commit d10c5ff

File tree

6 files changed

+352
-55
lines changed

6 files changed

+352
-55
lines changed

integration/compose/api/compose.api

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
public final class com/bumptech/glide/integration/compose/CrossFade : com/bumptech/glide/integration/compose/Transition$Factory {
2+
public static final field $stable I
3+
public static final field Companion Lcom/bumptech/glide/integration/compose/CrossFade$Companion;
4+
public fun <init> (Landroidx/compose/animation/core/AnimationSpec;)V
5+
public fun build ()Lcom/bumptech/glide/integration/compose/Transition;
6+
public fun equals (Ljava/lang/Object;)Z
7+
public fun hashCode ()I
8+
}
9+
10+
public final class com/bumptech/glide/integration/compose/CrossFade$Companion : com/bumptech/glide/integration/compose/Transition$Factory {
11+
public fun build ()Lcom/bumptech/glide/integration/compose/Transition;
12+
}
13+
114
public abstract interface annotation class com/bumptech/glide/integration/compose/ExperimentalGlideComposeApi : java/lang/annotation/Annotation {
215
}
316

417
public final class com/bumptech/glide/integration/compose/GlideImageKt {
5-
public static final fun GlideImage (Ljava/lang/Object;Ljava/lang/String;Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/bumptech/glide/integration/compose/Placeholder;Lcom/bumptech/glide/integration/compose/Placeholder;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)V
18+
public static final fun GlideImage (Ljava/lang/Object;Ljava/lang/String;Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/bumptech/glide/integration/compose/Placeholder;Lcom/bumptech/glide/integration/compose/Placeholder;Lcom/bumptech/glide/integration/compose/Transition$Factory;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;III)V
619
public static final fun GlideSubcomposition (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
720
public static final fun placeholder (I)Lcom/bumptech/glide/integration/compose/Placeholder;
821
public static final fun placeholder (Landroid/graphics/drawable/Drawable;)Lcom/bumptech/glide/integration/compose/Placeholder;
@@ -54,3 +67,14 @@ public final class com/bumptech/glide/integration/compose/RequestState$Success :
5467
public fun toString ()Ljava/lang/String;
5568
}
5669

70+
public abstract interface class com/bumptech/glide/integration/compose/Transition {
71+
public abstract fun getDrawCurrent ()Lkotlin/jvm/functions/Function5;
72+
public abstract fun getDrawPlaceholder ()Lkotlin/jvm/functions/Function5;
73+
public abstract fun stop (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
74+
public abstract fun transition (Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
75+
}
76+
77+
public abstract interface class com/bumptech/glide/integration/compose/Transition$Factory {
78+
public abstract fun build ()Lcom/bumptech/glide/integration/compose/Transition;
79+
}
80+

integration/compose/src/main/java/com/bumptech/glide/integration/compose/GlideImage.kt

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.bumptech.glide.integration.compose
22

3-
import android.graphics.drawable.BitmapDrawable
4-
import android.graphics.drawable.ColorDrawable
53
import android.graphics.drawable.Drawable
64
import androidx.annotation.DrawableRes
75
import androidx.compose.foundation.Image
@@ -15,8 +13,6 @@ import androidx.compose.ui.Modifier
1513
import androidx.compose.ui.graphics.Color
1614
import androidx.compose.ui.graphics.ColorFilter
1715
import androidx.compose.ui.graphics.DefaultAlpha
18-
import androidx.compose.ui.graphics.asImageBitmap
19-
import androidx.compose.ui.graphics.painter.BitmapPainter
2016
import androidx.compose.ui.graphics.painter.ColorPainter
2117
import androidx.compose.ui.graphics.painter.Painter
2218
import androidx.compose.ui.layout.ContentScale
@@ -26,10 +22,7 @@ import androidx.compose.ui.platform.LocalInspectionMode
2622
import com.bumptech.glide.Glide
2723
import com.bumptech.glide.RequestBuilder
2824
import com.bumptech.glide.RequestManager
29-
import com.bumptech.glide.integration.ktx.InternalGlideApi
3025
import com.bumptech.glide.load.DataSource
31-
import com.google.accompanist.drawablepainter.DrawablePainter
32-
import com.google.accompanist.drawablepainter.rememberDrawablePainter
3326

3427
/** Mutates and returns the given [RequestBuilder] to apply relevant options. */
3528
public typealias RequestBuilderTransform<T> = (RequestBuilder<T>) -> RequestBuilder<T>
@@ -77,6 +70,13 @@ public typealias RequestBuilderTransform<T> = (RequestBuilder<T>) -> RequestBuil
7770
* opposed to resource id or [Drawable]), this [Placeholder] will not be used unless the `error`
7871
* [RequestBuilder] also fails. This parameter does not override error [RequestBuilder]s, only error
7972
* resource ids and/or [Drawable]s.
73+
* @param transition An optional [Transition.Factory] that can animate the transition from a
74+
* placeholder to a loaded image. The transition will only be called once, when the load transitions
75+
* from showing the placeholder to showing the first resource. The transition will persist across
76+
* multiple resources if you're using thumbnail, but will not be called for each successive resource
77+
* in the request chain. The transition factory will not be called across different requests if
78+
* multiple are made. The transition will not be called if you use [placeholder] or [failure] with
79+
* the deprecated [Composable] API. See [CrossFade]
8080
*/
8181
// TODO(judds): the API here is not particularly composeesque, we should consider alternatives
8282
// to RequestBuilder (though thumbnail() may make that a challenge).
@@ -95,6 +95,7 @@ public fun GlideImage(
9595
// See http://shortn/_x79pjkMZIH for an internal discussion.
9696
loading: Placeholder? = null,
9797
failure: Placeholder? = null,
98+
transition: Transition.Factory? = null,
9899
// TODO(judds): Consider defaulting to load the model here instead of always doing so below.
99100
requestBuilderTransform: RequestBuilderTransform<Drawable> = { it },
100101
) {
@@ -144,6 +145,7 @@ public fun GlideImage(
144145
contentScale,
145146
alpha,
146147
colorFilter,
148+
transition,
147149
)
148150
)
149151
}
@@ -173,12 +175,6 @@ internal class GlideSubcompositionScopeImpl(
173175
override val painter: Painter
174176
get() = drawable?.toPainter() ?: ColorPainter(Color.Transparent)
175177

176-
private fun Drawable.toPainter(): Painter =
177-
when (this) {
178-
is BitmapDrawable -> BitmapPainter(bitmap.asImageBitmap())
179-
is ColorDrawable -> ColorPainter(Color(color))
180-
else -> DrawablePainter(mutate())
181-
}
182178
}
183179

184180
/**
@@ -253,7 +249,6 @@ public sealed class RequestState {
253249
* load never starting, or in an unreasonably large amount of memory being used. Loading overly
254250
* large images in memory can also impact scrolling performance.
255251
*/
256-
@OptIn(InternalGlideApi::class)
257252
@ExperimentalGlideComposeApi
258253
@Composable
259254
public fun GlideSubcomposition(
@@ -325,7 +320,7 @@ private fun PreviewResourceOrDrawable(
325320
throw IllegalArgumentException("Composables should go through the production codepath")
326321
}
327322
Image(
328-
painter = rememberDrawablePainter(drawable),
323+
painter = remember(drawable) { drawable.toPainter() },
329324
modifier = modifier,
330325
contentDescription = contentDescription,
331326
)

0 commit comments

Comments
 (0)