使用 Glide 将图像 URL 加载到 Kotlin AppWidget

Loading an image URL into a Kotlin AppWidget using Glide

我在互联网上搜索了如何在 Kotlin 中加载远程图像的示例。有很多使用 Glide 和普通 ImageView 的 Kotlin 示例,但是没有任何与 AppWidget 中的远程 ImageViews 相关的东西。

目前最好的资源在这里,但是在Java: https://futurestud.io/tutorials/glide-loading-images-innnto-notifications-and-appwidgets

我在下面添加了 Kotlin 翻译...

class DeviceIntelWidget : AppWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager?, 
    appWidgetIds: IntArray) {
        ...
        val remoteViews = RemoteViews(context.packageName, R.layout.widget)
        val awt: AppWidgetTarget = object : AppWidgetTarget(context.applicationContext, R.id.img, remoteViews, *appWidgetIds) {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                super.onResourceReady(resource, transition)
            }
        };    
        Glide.with(context.applicationContext).asBitmap().load("https://si.com/img.jpg").into(awt)
        ...
    }
}

备注:

  • A​​ppWidgetTarget 用于通过 RemoteViews 在 AppWidget 的 ImageView 中显示下载的位图
  • 调用 load 加载位图并在 onResourceReady
  • 中进行任何 post 处理
  • 实施'com.github.bumptech.glide:glide:4.2.0'强制关闭NoClassDefFoundError: android.support.v4.util.ArrayMap
  • 更新到最新:4.7.1 修复了问题