- 原文鏈接: Callbacks, RemoteViews and Notifications
- 原文作者: Future Studio
- 譯文出自: 小鄧子的簡書
- 譯者: 小鄧子
- 狀態(tài): 完成
Callback與Target
在了解callback之前,值得一提的是Picasso有多種圖像加載方式。大體可分為同步和異步兩種。
fetch(),get()與target之間的區(qū)別與聯(lián)系
.fetch()會在后臺線程中異步加載圖片,但不會展示到ImageView上,也不會返回該圖片對象。它只會把圖像保存到磁盤或內存中。它在后臺緩存那些稍后會用到的圖片,從而降低加載時間。
.get()同步加載圖片,并返回Bitmap對象。確保不要在UI線程調用.get(),否則會造成UI線程的阻塞。
除了使用.into(),這里還有另外一個方法:回調!在Picasso概念中它們被稱為Target。
使用Target實現(xiàn)回調機制
到目前為止,我們一直使用ImageView作為.into()的參數(shù)。這并不是.into()函數(shù)的全部用法。我們還可以實現(xiàn)一個Target接口。
和之前的加載方式一樣,只不過不再將圖像直接展示到ImageView上,而是通過Target回調,返回Bitmap對象(或者異常)。
來看一個示例。Picasso的創(chuàng)建方式和之前的例子一樣:
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.into(target);
有趣的部分在Target實例中:
private Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// loading of the bitmap was a success
// TODO do some action with the bitmap
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
// loading of the bitmap failed
// TODO do some action/warning/error message
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
如果這個任務成功了,回調中將會接收到Bitmap對象和Picasso.LoadedFrom。后者用來判定,圖像來源于內存還是網絡。此時,你能做有關Bitmap的任何操作。
總之,無論何時,你都可以通過.get()來獲得原始Bitmap或者通過實現(xiàn)Target來獲得Drawable。
切記:總是將target實例作為字段屬性存在,而不是匿名類!否則GC會銷毀target對象,并且永遠無法獲得圖像結果(譯者注:這里的target會被作為WeakReference而存在)。
加載圖像至自定義通知欄
一個新特性是加載圖像至RemoteView上。RemoteView是一個非常有用的控件,用來自定義通知欄布局。
讓我們看一個使用RemoteView,自定義通知欄的示例。如果你對自定義通知欄布局感興趣,你可能已經知道了如果構建一個通知欄。希望下面這段代碼能夠對你有所幫助:
// create RemoteViews
final RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remoteview_notification);
remoteViews.setImageViewResource(R.id.remoteview_notification_icon, R.mipmap.future_studio_launcher);
remoteViews.setTextViewText(R.id.remoteview_notification_headline, "Headline");
remoteViews.setTextViewText(R.id.remoteview_notification_short_message, "Short Message");
remoteViews.setTextColor(R.id.remoteview_notification_headline, getResources().getColor(android.R.color.black));
remoteViews.setTextColor(R.id.remoteview_notification_short_message, getResources().getColor(android.R.color.black));
// build notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(UsageExampleTargetsAndRemoteViews.this)
.setSmallIcon(R.mipmap.future_studio_launcher)
.setContentTitle("Content Title")
.setContentText("Content Text")
.setContent(remoteViews)
.setPriority(NotificationCompat.PRIORITY_MIN);
final Notification notification = mBuilder.build();
// set big content view for newer androids
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = remoteViews;
}
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, notification);
這就是用自定義布局創(chuàng)建一個通知的全部代碼。我們不再詳細介紹,因為這并不屬于本系列博客的范疇。我們感興趣的是下一步:加載圖像至ImageView。
Picasso的調用非常簡單。與加載到ImageView上相似,我們也對RemoteView使用.into()函數(shù)。然而,傳入的參數(shù)可能有些不同.into(android.widget.RemoteViews remoteViews, int viewId, int notificationId, android.app.Notification notification)
示例代碼如下:
Picasso
.with(UsageExampleTargetsAndRemoteViews.this)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.into(remoteViews, R.id.remoteview_notification_icon, NOTIFICATION_ID, notification);
也許你不了解每個變量,請回到上面的長代碼塊中理解參數(shù)的含義。示例中的通知欄效果如下:

如果你對圖片加載到Widget上感興趣,可以使用.into()的另一個方法:into(android.widget.RemoteViews remoteViews, int viewId, int[] appWidgetIds)。