Make ImageView with Round Corner Using picasso

后端 未结 7 979
刺人心
刺人心 2020-12-15 03:14

I know there are lots of link available to make ImageView Round Corner. But I\'m Using Picasso Library for Image Loading.. I refer the link to get

相关标签:
7条回答
  • 2020-12-15 03:41

    Kotlin version of RoundCornerTransform based on @stevyhacker 's answer.

    • Support rectangle instead of square image.
    • No extra 3rd party library needed.

    =============================================

    import android.graphics.*
    import com.squareup.picasso.Transformation
    
    class RoundCornersTransform(private val radiusInPx: Float) : Transformation {
    
        override fun transform(source: Bitmap): Bitmap {
            val bitmap = Bitmap.createBitmap(source.width, source.height, source.config)
            val canvas = Canvas(bitmap)
            val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
            val shader = BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
            paint.shader = shader
            val rect = RectF(0.0f, 0.0f, source.width.toFloat(), source.height.toFloat())
            canvas.drawRoundRect(rect, radiusInPx, radiusInPx, paint)
            source.recycle()
    
            return bitmap
        }
    
        override fun key(): String {
            return "round_corners"
        }
    
    }
    

    Usage:

        Picasso.get()
            .load(imageUrl)
            .memoryPolicy(MemoryPolicy.NO_CACHE)
            .placeholder(R.drawable.image_placeholder)
            .transform(RoundCornersTransform(32.0f))
            .into(imageView, callback)
    
    0 讨论(0)
提交回复
热议问题