Last active
March 1, 2018 12:21
-
-
Save ookami-kb/c5a8ae1d0b0881f901939886d692d5a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class RoundedImageView(context: Context, attributeSet: AttributeSet?) : View(context, attributeSet), Target { | |
constructor(context: Context) : this(context, null) | |
private var drawable: Drawable? = null | |
set(value) { | |
field = value | |
postInvalidate() | |
} | |
fun loadImage(url: String?) { | |
if (url == null) { | |
drawable = null | |
} else { | |
Picasso.with(context) | |
.load(url) | |
.placeholder(R.drawable.image_stub) | |
.error(R.drawable.image_stub) | |
.into(this) | |
} | |
} | |
override fun onDraw(canvas: Canvas?) { | |
drawable?.setBounds(0, 0, width, height) | |
drawable?.draw(canvas) | |
} | |
override fun onPrepareLoad(placeHolderDrawable: Drawable?) { | |
drawable = placeHolderDrawable | |
} | |
override fun onBitmapFailed(errorDrawable: Drawable?) { | |
drawable = errorDrawable | |
} | |
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) { | |
val roundedDrawable = RoundedBitmapDrawableFactory.create(resources, bitmap) | |
roundedDrawable.cornerRadius = dip(DEFAULT_RADIUS).toFloat() | |
drawable = roundedDrawable | |
} | |
companion object { | |
private const val DEFAULT_RADIUS = 4 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment