Skip to content

Instantly share code, notes, and snippets.

@ookami-kb
Last active March 1, 2018 12:21
Show Gist options
  • Save ookami-kb/c5a8ae1d0b0881f901939886d692d5a2 to your computer and use it in GitHub Desktop.
Save ookami-kb/c5a8ae1d0b0881f901939886d692d5a2 to your computer and use it in GitHub Desktop.
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