【问题标题】:Load image with Glide for AppWidget使用 Glide 为 AppWidget 加载图像
【发布时间】:2016-11-09 12:17:49
【问题描述】:

我正在开发我的第一个应用小部件。 一切都很好,除了一件事。我不知道,如何使用 Glide 在 RemoteViewsFactroy 的 getViewAt() 方法中将图像加载到 ImageView 中。我总是出错,因为我必须在 MainThread 遇到(AppWidgetTarget),但我不能从 RemoteViewsFactory 调用 new Handler(Looper.getMainLooper())。

【问题讨论】:

  • 您可以使用通用图像加载器来实现这一点。您想仅使用滑行加载图像吗?

标签: android android-appwidget android-appwidget-list


【解决方案1】:

Glide 通过提供 AppWidgetTarget 类对此提供支持。

在onUpdate() 中更新您的小部件时,您创建一个新的 AppWidgetTarget,将 RemoteViews 对象、您的图像视图的 id 和 appwidgetId 传递给它,然后以几乎与您调用 glide 相同的方式调用应用中的图像视图。

    appWidgetTarget = new AppWidgetTarget( context, rv, R.id.custom_view_image, appWidgetIds );

    Glide
            .with( context.getApplicationContext() ) // safer!
            .load( GlideExampleActivity.eatFoodyImages[3] )
            .asBitmap()
            .into( appWidgetTarget );

上面的示例代码取自wonderful set of glide tutorials。这些教程涵盖了使用 glide 的许多实际方面。

同样,glide 也提供了一个NotificationTarget 目标类 - 它解决了同样的问题,但用于通知而不是 appWidgets。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 2016-05-09
    • 2017-12-17
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多