Adding borders for image rounded image android

前端 未结 4 1087
夕颜
夕颜 2020-12-02 23:33

What i have:: I have a Imageview for which i am making image as a circle using picassso \"enter

4条回答
  •  情歌与酒
    2020-12-03 00:28

    Answers of BlackBelt and Devrath are great. And if you are looking for a Kotlin version of this class, here it is:

    class RoundedBorderTransform(private val radius: Int, private val margin: Int) : com.squareup.picasso.Transformation {
    
    override fun transform(source: Bitmap?): Bitmap {
        val paint = Paint()
        paint.isAntiAlias = true
        paint.shader = BitmapShader(source!!, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
    
        val output = Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(output)
        canvas.drawCircle((source.width - margin) / 2f, (source.height - margin) / 2f, radius - 2f, paint)
    
        if (source != output) {
            source.recycle()
        }
    
        val borderPaint = Paint()
        borderPaint.color = Color.RED
        borderPaint.style = Paint.Style.STROKE
        borderPaint.isAntiAlias = true
        borderPaint.strokeWidth = 2f
        canvas.drawCircle((source.width - margin) / 2f, (source.height - margin) / 2f, radius - 2f, borderPaint)
    
        return output
    }
    
    override fun key(): String {
        return "roundedBorder"
    }
    

    }

提交回复
热议问题