Last active
September 25, 2018 11:11
-
-
Save erluxman/18df338e9d7e5002a5b924dddf464a5a to your computer and use it in GitHub Desktop.
extension function for loading image with picasso..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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