Translations: 简体中文
The main job of Target is to display Image. It is also responsible for providing SizeResolver, ScaleDecider, ResizeOnDrawHelper, LifecycleResolver and other properties when building ImageRequest. These properties will be used as default values.
Displaying Image to the Compose component does not require you to actively set the target, AsyncImage and AsyncImagePainter will be set, you only need to set other parameters, as follows:
AsyncIage(
rqeuest = ComposableImageRequest("https://example.com/image.jpg") {
placeholder(Res.drawable.placeholder)
crossfade()
},
contentDescription = "photo",
)
Tip
placeholder(Res.drawable.placeholder)
needs to import the sketch-compose-resources
module
When displaying Image to View, you need to actively set Target
Sketch provides ImageViewTarget to display images to [ImageView], as follows:
ImageRequest(context, "https://www.example.com/image.jpg") {
placeholder(R.drawable.placeholder)
crossfade()
target(imageView)
}.enqueue(request)
Sketch also provides RemoteViewsTarget 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()
ImageRequest(context, "https://www.example.com/image.jpg") {
resize(100.dp2px, 100.dp2px, scale = START_CROP)
target(
RemoteViewsTarget(
remoteViews = remoteViews,
imageViewId = R.id.remoteViewsNotificationImage,
ignoreNullDrawable = true,
onUpdated = {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1101, notification)
}
)
)
}.enqueue()
Tip
- As shown above RemoteViewsTarget 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.