Translations: 简体中文
Target is used to receive the result ImageResult of ImageRequest and apply the result to the target
From the previous Getting Started document, we can know that ImageRequest is divided into DisplayRequest, LoadRequest, DownloadRequest Three, they all have different ImageResult implementations, so Target also has three corresponding implementations:
- DisplayTarget: Receives results of Drawable type, dedicated to DisplayRequest
- LoadTarget: Receives Bitmap type results, dedicated to LoadRequest
- DownloadTarget: Receives results of type DownloadData, dedicated to DownloadRequest
The following demonstrates creating a custom DisplayTarget:
DisplayRequest(context, "https://www.example.com/image.jpg") {
target(
onStart = { placeholder: Drawable? ->
// Handle the placeholder drawable.
},
onSuccess = { result: Drawable ->
// Handle the successful result Drawable.
},
onError = { error: Drawable? ->
// Handle the error drawable.
}
)
}.enqueue(request)
LoadTarget and DownloadTarget are used in much the same way as DisplayTarget.
DisplayTarget is usually used to apply Drawable to View, so Sketch provides ViewDisplayTarget and ImageViewDisplayTarget to simplify use
DisplayRequest also provides the target(ImageView) method to simplify binding to ImageView, as follows:
DisplayRequest(context, "https://www.example.com/image.jpg") {
target(imageView)
}.enqueue()
Sketch provides RemoteViewsDisplayTarget to display images to RemoteViews, as follows:
val remoteViews =
RemoteViews(context.packageName, R.layout.remote_views_notification)
val notification = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID).apply {
setSmallIcon(R.mipmap.ic_launcher)
setContent(remoteViews)
}.build()
DisplayRequest(context, "https://www.example.com/image.jpg") {
resize(100.dp2px, 100.dp2px, scale = START_CROP)
target(
RemoteViewsDisplayTarget(
remoteViews = remoteViews,
imageViewId = R.id.remoteViewsNotificationImage,
ignoreNullDrawable = true,
onUpdated = {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1101, notification)
}
)
)
}.enqueue()
- As shown above RemoteViewsDisplayTarget only converts the Drawable to Bitmap and calls the setImageViewBitmap method of RemoteViews to set the Bitmap
- So you still need to refresh the notification or AppWidget in the onUpdated function to display the Bitmap on the screen