Skip to content

Instantly share code, notes, and snippets.

@erluxman

erluxman/ImageView.kt

Last active Sep 25, 2018
Embed
What would you like to do?
extension function for loading image with picasso..
fun ImageView.loadImage(url: String ="", @DrawableRes drawable: Int = R.mipmap.ic_launcher, placeHolderUrl: String = "", @NonNull placeHolderDrawable: Int = R.mipmap.ic_launcher, circular: Boolean = false) {
val defUrl = ""
@DrawableRes
val defDrawable = R.mipmap.ic_launcher
when {
//Displaying image from Url
url.isNotEmpty() && circular -> Picasso.get().load(url).placeholder(placeHolderDrawable).transform(CircleTransform()).into(this);
url.isNotEmpty() && !circular -> Picasso.get().load(url).placeholder(placeHolderDrawable).into(this)
//Displaying drawable Image
url.isEmpty() && drawable != defDrawable && circular -> Picasso.get().load(drawable).placeholder(placeHolderDrawable).transform(CircleTransform()).into(this)
url.isEmpty() && drawable != defDrawable && !circular -> Picasso.get().load(drawable).placeholder(placeHolderDrawable).into(this)
//Displaying placeholder Url
url.isEmpty() && drawable == defDrawable && placeHolderUrl != defUrl && circular -> Picasso.get().load(placeHolderUrl).placeholder(placeHolderDrawable).transform(CircleTransform()).into(this)
url.isEmpty() && drawable == defDrawable && placeHolderUrl != defUrl && !circular -> Picasso.get().load(placeHolderUrl).placeholder(placeHolderDrawable).into(this)
//Displaying placeholder Drawable
url.isEmpty() && drawable == defDrawable && placeHolderUrl.isEmpty() && circular -> Picasso.get().load(placeHolderDrawable).transform(CircleTransform()).into(this)
url.isEmpty() && drawable == defDrawable && placeHolderUrl.isEmpty() && !circular -> Picasso.get().load(placeHolderDrawable).into(this)
else -> {
if (circular) Picasso.get().load(defDrawable).transform(CircleTransform()).into(this)
else Picasso.get().load(defDrawable).into(this)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment