question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Glide] Add Gif support with GlideImage

See original GitHub issue

The LocalGlideProvider only supports Drawable?:

public val LocalGlideRequestBuilder: ProvidableCompositionLocal<RequestBuilder<Drawable>?> =
  staticCompositionLocalOf { null }

By changing this to support GifDrawable?, we can support Glide’s .asGif():


/**
 * Local containing the preferred [RequestOptions] for providing the same instance
 * in our composable hierarchy.
 */
public val LocalGlideGifRequestOptions: ProvidableCompositionLocal<RequestOptions?> =
  staticCompositionLocalOf { null }

/**
 * Local containing the preferred [RequestBuilder] for providing the same instance
 * in our composable hierarchy.staticCompositionLocalOf
 */
public val LocalGlideGifRequestBuilder: ProvidableCompositionLocal<RequestBuilder<GifDrawable>?> =
  staticCompositionLocalOf { null }

/**
 * Local containing the preferred [RequestManager] for providing the same instance
 * in our composable hierarchy.staticCompositionLocalOf
 */
public val LocalGlideGifRequestManager: ProvidableCompositionLocal<RequestManager?> =
  staticCompositionLocalOf { null }

/** A provider for taking the local instances related to the `GlideImage`. */
internal object LocalGlideGifProvider {

  /** Returns the current or default [RequestOptions] for the `GlideImage` parameter. */
  @Composable
  fun getGlideRequestOptions(): RequestOptions {
    return LocalGlideGifRequestOptions.current ?: RequestOptions()
  }

  /** Returns the current or default [RequestBuilder] for the `GlideImage` parameter. */
  @Composable
  fun getGlideRequestBuilder(): RequestBuilder<GifDrawable> {
    return LocalGlideGifRequestBuilder.current
           ?: getGlideRequestManager()
             .asGif()
  }

  /** Returns the current or default [RequestManager] for the `GlideImage` processor. */
  @Composable
  fun getGlideRequestManager(): RequestManager {
    // By default Glide tries to install lifecycle listeners to automatically re-trigger
    // requests when resumed. We don't want that with Compose, since we rely on composition
    // for our 'lifecycle'. We can stop Glide doing this by using the application context.
    return LocalGlideGifRequestManager.current
           ?: GlideApp.with(LocalContext.current.applicationContext)
  }
}

Can we provide a LocalGlideGifProvider, LocalGlideBitmapProvider and LocalGlideDrawableProvider providers?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jaredsburrowscommented, Oct 23, 2022

@skydoves Thanks. I got it working. I appreciate the update.

0reactions
skydovescommented, Oct 23, 2022

The example below works well for me 🤔

  val glide = Glide.with(LocalView.current)
    .asGif()
    .override(135.dp.value.roundToInt())
    .signature(ObjectKey("https://media.giphy.com/media/WNur0b03KntlK/giphy.gif"))

  CompositionLocalProvider(LocalGlideRequestBuilder provides glide) {

    GlideImage(
      imageModel = { "https://media.giphy.com/media/WNur0b03KntlK/giphy.gif" },
      glideRequestType = GlideRequestType.GIF,
      modifier = Modifier
        .padding(1.dp)
        .size(135.dp),
      imageOptions = ImageOptions(contentScale = ContentScale.Crop),
      loading = {
        Box(modifier = Modifier.matchParentSize()) {
          CircularProgressIndicator(
            modifier = Modifier.align(Alignment.Center)
          )
        }
      },
    )
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Show GIF file with Glide (image loading and caching library)
The above answer didn't work for me. The below code works. ImageView imageView = (ImageView) findViewById(R.id.imageView); GlideDrawableImageViewTarget ...
Read more >
Load image and GIF using Glide - Medium
First, we will add Glide on our build.gradle (app level), like this: apply plugin: 'com.android.application' apply plugin: 'kotlin-android'
Read more >
Displaying Images with the Glide Library - CodePath Cliffnotes
Glide is an Image Loader Library for Android developed by bumptech and is a library ... It provides animated GIF support and handles...
Read more >
Glide v4 : Fast and efficient image loading for Android
Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible api that allows developers to plug in ......
Read more >
Loading images for Jetpack Compose using ... - ProAndroidDev
This library also supports loading animated images such as GIFs, WebP and ... You can load images simply by using GlideImage composable function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found