How to make an ImageView with rounded corners?

前端 未结 30 2484
天涯浪人
天涯浪人 2020-11-21 05:39

In Android, an ImageView is a rectangle by default. How can I make it a rounded rectangle (clip off all 4 corners of my Bitmap to be rounded rectangles) in the ImageView?

30条回答
  •  借酒劲吻你
    2020-11-21 06:20

    Kotlin

    import android.graphics.BitmapFactory
    import android.os.Bundle
    import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory
    import kotlinx.android.synthetic.main.activity_main.*
    
    val bitmap = BitmapFactory.decodeResource(resources, R.drawable.myImage)
    val rounded = RoundedBitmapDrawableFactory.create(resources, bitmap)
    rounded.cornerRadius = 20f
    profileImageView.setImageDrawable(rounded)
    

    To make ImageView Circular we can change cornerRadius with:

    rounded.isCircular = true
    

提交回复
热议问题