I'm making an Android app with Kotlin, and need to use Picasso to download images. I saw this Java code below for setting animations to images, but I can't convert it to Kotlin, cause I don't know how to set Callback in "into" function.
Picasso.with(MainActivity.this)
.load(imageUrl)
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
//set animations here
}
@Override
public void onError() {
//do smth when there is picture loading error
}
});
Can someone help me ?
My actual code :
Picasso.with(context)
.load(url)
.into(imageDiapo, com.squareup.picasso.Callback)
Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.
Picasso.with(MainActivity::this)
.load(imageUrl)
.into(imageView, object: com.squareup.picasso.Callback {
override fun onSuccess() {
//set animations here
}
override fun onError(e: java.lang.Exception?) {
//do smth when there is picture loading error
}
})
In Latest version onError
recives an Exception
as parameter and uses get()
instead of with()
:
Picasso.get()
.load(imageUrl)
.into(imageView, object :Callback{
override fun onSuccess() {
Log.d(TAG, "success")
}
override fun onError(e: Exception?) {
Log.d(TAG, "error")
}
})
and On the previous version
Picasso.with(MainActivity::this)
.load(imageUrl)
.into(imageView, object: Callback {
override fun onSuccess() {
Log.d(TAG, "success")
}
override fun onError() {
Log.d(TAG, "error")
}
})
Hi here are some different ways that Picasso provides:
Picasso.with(context).load(path).into(imageView);
2.create a new file inside our utils package, call it picasso.kt and fill it with simple code below:
public val Context.picasso: Picasso
get() = Picasso.with(this)
3. While this corresponds to the receiver object we can invoke following code on any Context:
picasso.load(path).into(imageView)
We can go further and extend ImageView class like:
public fun ImageView.load(path: String, request: (RequestCreator) -> RequestCreator) {
request(getContext().picasso.load(path)).into(this) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With