zihadrizkyef

Untitled

Jul 21st, 2022
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. fun ImageView.loadImageOrInitial(
  2.     url: String?,
  3.     text: String?,
  4.     textSize: Float = 16F,
  5.     textColor: Int = Color.BLACK,
  6.     backgroundColor: Int = Color.TRANSPARENT,
  7.     maxChar: Int = 2,
  8.     allCaps: Boolean = true,
  9.     progressStroke: Float = 5F,
  10.     progressRadius: Float = 30F,
  11. ) {
  12.     val progressDrawable = CircularProgressDrawable(context)
  13.     progressDrawable.strokeWidth = progressStroke.dp
  14.     progressDrawable.centerRadius = progressRadius.dp
  15.     progressDrawable.start()
  16.  
  17.     Glide.with(this)
  18.         .load(url)
  19.         .placeholder(progressDrawable)
  20.         .addListener(object : RequestListener<Drawable> {
  21.             override fun onLoadFailed(
  22.                 e: GlideException?,
  23.                 model: Any?,
  24.                 target: Target<Drawable>?,
  25.                 isFirstResource: Boolean
  26.             ): Boolean {
  27.                 val context = this@loadImageOrInitial.context
  28.                 val textDrawable = InitialsDrawable(
  29.                     context,
  30.                     text ?: "",
  31.                     textSize,
  32.                     textColor,
  33.                     backgroundColor,
  34.                     maxChar,
  35.                     allCaps,
  36.                 )
  37.                 textDrawable.setBounds(0, 0, 40, 40)
  38.                 this@loadImageOrInitial.setImageDrawable(textDrawable)
  39.                 return true
  40.             }
  41.  
  42.             override fun onResourceReady(
  43.                 resource: Drawable?,
  44.                 model: Any?,
  45.                 target: Target<Drawable>?,
  46.                 dataSource: DataSource?,
  47.                 isFirstResource: Boolean
  48.             ): Boolean {
  49.                 return false
  50.             }
  51.         })
  52.         .into(this)
  53. }
Advertisement
Add Comment
Please, Sign In to add comment