项目作者: chRyNaN

项目描述 :
Asynchronous loading of Drawable Functions for the Glide library
高级语言: Kotlin
项目地址: git://github.com/chRyNaN/glide-drawable.git
创建时间: 2018-10-17T15:27:51Z
项目社区:https://github.com/chRyNaN/glide-drawable

开源协议:Apache License 2.0

下载


glide-drawable

Lazy and asynchronous loading of Drawables with the Glide library.

Building

Base Library:

  1. implementation 'com.github.chRyNaN.glide-drawable:library:VERSION'

Drawable Function Implementations:

  1. implementation 'com.github.chRyNaN.glide-drawable:feature:VERSION'

Using the library

  • Pass in a function that returns a Drawable instance into Glide. The function will be resolved asynchronously and the Drawable will be cached using Glide.

    1. Glide.with(context)
    2. .loadFunction { getMyDrawable() }
    3. .into(imageView)
    4. // Or
    5. Glide.with(context)
    6. .load(myDrawableFunctionClassImplementation)
    7. .into(imageView)
  • Register the DrawableFunction class:

    1. @GlideModule
    2. class SearchAppGlideModule : AppGlideModule() {
    3. override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
    4. registry.prepend(DrawableFunction::class.java, Drawable::class.java, DrawableFunctionLoaderFactory())
    5. }
    6. }

This approach is different from using the load(getMyDrawable()) function because that would resolve the drawable synchronously when the function is called. Passing a function to load, load(f: () -> Drawable), resolves the Drawable asynchronously.

Creating a reusable DrawableFunction

  • Create the implementation class:

    1. class ApplicationIconDrawableFunction(
    2. private val packageManager: PackageManager,
    3. private val packageName: String
    4. ) : DrawableFunction {
    5. companion object {
    6. private const val PACKAGE_ID_PREFIX = "application:packageName:"
    7. }
    8. override val uniqueKey: Key
    9. get() = ObjectKey("$PACKAGE_ID_PREFIX$packageName")
    10. override val function: () -> Drawable
    11. get() = { packageManager.getApplicationIcon(packageName) }
    12. }
  • Register the class:

    1. @GlideModule
    2. class SearchAppGlideModule : AppGlideModule() {
    3. override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
    4. registry.prepend(ApplicationIconDrawableFunction::class.java, Drawable::class.java, DrawableFunctionLoaderFactory())
    5. }
    6. }

Feature Library

Some common implementations of DrawableFunction are provided through the feature library. This library is optional but is useful if those implementations are needed.
The ApplicationIconDrawableFunction shown above, is provided in the feature library.