SHARE
TWEET

Untitled

a guest Oct 13th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Load model into ImageView as a circle image with borderSize (optional) using Glide
  3.  *
  4.  * @param model - Any object supported by Glide (Uri, File, Bitmap, String, resource id as Int, ByteArray, and Drawable)
  5.  * @param borderInDip - The border size in density-independent pixels
  6.  * @param borderColor - The border color
  7.  */
  8. fun <T> ImageView.loadCircularImage(
  9.     model: T, borderInDip: Float = 0F, borderColor: Int = R.color.cristo_blue
  10. ) {
  11.     Glide.with(context)
  12.         .asBitmap()
  13.         .load(model)
  14.         .apply(RequestOptions.circleCropTransform())
  15.         .into(object : BitmapImageViewTarget(this) {
  16.             override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
  17.                 setImageDrawable(resource.run {
  18.                     RoundedBitmapDrawableFactory.create(
  19.                         resources, if (context.convertDpToPixel(borderInDip) > 0) {
  20.                             createBitmapWithBorder(context.convertDpToPixel(borderInDip), borderColor, context)
  21.                         } else {
  22.                             this
  23.                         }
  24.                     ).apply {
  25.                             isCircular = true
  26.                         }
  27.                 })
  28.             }
  29.         })
  30. }
  31.  
  32. /**
  33.  * Create a new bordered bitmap with the specified borderSize and borderColor
  34.  *
  35.  * @param borderSize - The border size in pixels
  36.  * @param borderColor - The border color
  37.  * @param context - Android context for resolving the color resource
  38.  * @return A new bordered bitmap with the specified borderSize and borderColor
  39.  */
  40. fun Bitmap.createBitmapWithBorder(borderSize: Float, borderColor: Int = R.color.cristo_blue, context: Context): Bitmap {
  41.     val borderOffset = (borderSize * 2).toInt()
  42.     val halfWidth = width / 2
  43.     val halfHeight = height / 2
  44.     val circleRadius = min(halfWidth, halfHeight).toFloat()
  45.     val newBitmap = Bitmap.createBitmap(
  46.         width + borderOffset, height + borderOffset, Bitmap.Config.ARGB_8888
  47.     )
  48.  
  49.     // Center coordinates of the image
  50.     val centerX = halfWidth + borderSize
  51.     val centerY = halfHeight + borderSize
  52.  
  53.     val paint = Paint()
  54.     val canvas = Canvas(newBitmap).apply {
  55.         // Set transparent initial area
  56.         drawARGB(0, 0, 0, 0)
  57.     }
  58.  
  59.     // Draw the transparent initial area
  60.     paint.isAntiAlias = true
  61.     paint.style = Paint.Style.FILL
  62.     canvas.drawCircle(centerX, centerY, circleRadius, paint)
  63.  
  64.     // Draw the image
  65.     paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
  66.     canvas.drawBitmap(this, borderSize, borderSize, paint)
  67.  
  68.     // Draw the createBitmapWithBorder
  69.     paint.xfermode = null
  70.     paint.style = Paint.Style.STROKE
  71.     paint.color = ContextCompat.getColor(context, borderColor)
  72.     paint.strokeWidth = borderSize
  73.     canvas.drawCircle(centerX, centerY, circleRadius, paint)
  74.     return newBitmap
  75. }
  76.  
  77.  
  78. // -- USAGE -- //
  79.  
  80.     @JvmStatic
  81.     @BindingAdapter("glideCircleUrl", "glideCircleBorderSize", "glideCircleBorderColor", requireAll = false)
  82.     fun withGlideOrEmpty(
  83.         imageView: ImageView, url: String = "", borderSize: Float = 0F, borderColor: Int = R.color.cristo_blue) {
  84.         imageView.loadCircularImage(url, borderSize, borderColor)
  85.     }
  86.    
  87. // -- XML EXAMPLE -- //
  88. // bind:glideCircleBorderColor="@{R.color.dark_blue}"
  89. // bind:glideCircleBorderSize="@{2}"
  90. // bind:glideCircleUrl="@{viewModel.domainObject.imageUrl}"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top