- 原文鏈接: Callbacks, RemoteViews and Notifications
- 原文作者: Future Studio
- 譯文出自: 小鄧子的簡書
- 譯者: 小鄧子
- 狀態(tài): 完成
Callback與Target
在了解callback之前验烧,值得一提的是Picasso有多種圖像加載方式虐拓。大體可分為同步和異步兩種沼沈。
fetch()丈冬,get()與target之間的區(qū)別與聯(lián)系
.fetch()
會(huì)在后臺(tái)線程中異步加載圖片嘱函,但不會(huì)展示到ImageView
上,也不會(huì)返回該圖片對(duì)象埂蕊。它只會(huì)把圖像保存到磁盤或內(nèi)存中往弓。它在后臺(tái)緩存那些稍后會(huì)用到的圖片,從而降低加載時(shí)間蓄氧。
.get()
同步加載圖片函似,并返回Bitmap
對(duì)象。確保不要在UI線程調(diào)用.get()
喉童,否則會(huì)造成UI線程的阻塞撇寞。
除了使用.into()
,這里還有另外一個(gè)方法:回調(diào)!在Picasso概念中它們被稱為Target
蔑担。
使用Target實(shí)現(xiàn)回調(diào)機(jī)制
到目前為止牌废,我們一直使用ImageView
作為.into()
的參數(shù)。這并不是.into()
函數(shù)的全部用法啤握。我們還可以實(shí)現(xiàn)一個(gè)Target接口
鸟缕。
和之前的加載方式一樣,只不過不再將圖像直接展示到ImageView
上排抬,而是通過Target
回調(diào)懂从,返回Bitmap
對(duì)象(或者異常)。
來看一個(gè)示例蹲蒲。Picasso的創(chuàng)建方式和之前的例子一樣:
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.into(target);
有趣的部分在Target
實(shí)例中:
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) {
}
};
如果這個(gè)任務(wù)成功了番甩,回調(diào)中將會(huì)接收到Bitmap
對(duì)象和Picasso.LoadedFrom。后者用來判定悠鞍,圖像來源于內(nèi)存還是網(wǎng)絡(luò)对室。此時(shí),你能做有關(guān)Bitmap
的任何操作咖祭。
總之掩宜,無論何時(shí),你都可以通過.get()
來獲得原始Bitmap
或者通過實(shí)現(xiàn)Target
來獲得Drawable
么翰。
切記:總是將target實(shí)例作為字段屬性存在牺汤,而不是匿名類!否則GC會(huì)銷毀target對(duì)象浩嫌,并且永遠(yuǎn)無法獲得圖像結(jié)果(譯者注:這里的target
會(huì)被作為WeakReference
而存在)檐迟。
加載圖像至自定義通知欄
一個(gè)新特性是加載圖像至RemoteView上。RemoteView
是一個(gè)非常有用的控件码耐,用來自定義通知欄布局追迟。
讓我們看一個(gè)使用RemoteView
,自定義通知欄的示例骚腥。如果你對(duì)自定義通知欄布局感興趣敦间,你可能已經(jīng)知道了如果構(gòu)建一個(gè)通知欄。希望下面這段代碼能夠?qū)δ阌兴鶐椭?/p>
// 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)建一個(gè)通知的全部代碼束铭。我們不再詳細(xì)介紹廓块,因?yàn)檫@并不屬于本系列博客的范疇。我們感興趣的是下一步:加載圖像至ImageView
契沫。
Picasso的調(diào)用非常簡單带猴。與加載到ImageView
上相似,我們也對(duì)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);
也許你不了解每個(gè)變量靶病,請(qǐng)回到上面的長代碼塊中理解參數(shù)的含義。示例中的通知欄效果如下:
如果你對(duì)圖片加載到Widget
上感興趣贷掖,可以使用.into()
的另一個(gè)方法:into(android.widget.RemoteViews remoteViews, int viewId, int[] appWidgetIds)
嫡秕。