2016-11-09 209 views
2

我正在處理我的第一個應用部件。除了一件事外,Everthing很好。我找不到,我怎麼能在RemoteViewsFactroy的getViewAt()方法中使用Glide將圖像加載到ImageView中。我總是得到錯誤,因爲我必須在MainThread中運行(AppWidgetTarget),但我無法從RemoteViewsFactory調用新的Handler(Looper.getMainLooper())。使用Glide爲AppWidget加載圖像

+0

你可以使用通用圖像加載器來實現這一點。你想只使用滑動來加載圖片嗎? – Sangeetha

回答

0

Glide通過提供AppWidgetTarget類來支持此功能。

雖然onUpdate()更新你的widget,創建一個新的AppWidgetTarget,它傳遞RemoteViews對象,你的圖像視圖的ID和appwidgetId,然後,你會在幾乎相同的方式調用滑翔在一個ImageView的你應用程序。

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。這些教程涵蓋了使用滑翔的許多實際方面。

類似地,NotificationTarget目標類也由滑行提供 - 它解決了同樣的問題,但對於通知,而不是appWidgets。