ImageView rounded corners

后端 未结 17 2039
长发绾君心
长发绾君心 2020-11-27 15:33

I wanted image to have rounded corners. I implement this xml code and use this in my image view. but image overlap the shape. I am downloading the image through async task.<

17条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 16:15

    Now we no need to use any third party lib or custom imageView

    Now We can use ShapeableImageView

    SAMPLE CODE

    First add below dependencies in your build.gradle file

    implementation 'com.google.android.material:material:1.2.0-alpha05'
    

    Make ImageView Circular from coding

    Add ShapeableImageView in your layout

    
    

    Kotlin code to make ImageView Circle

    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import com.google.android.material.shape.CornerFamily
    import kotlinx.android.synthetic.main.activity_main.*
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
    
    //        50dp
    
            val radius = resources.getDimension(R.dimen.image_corner_radius)
            myShapeableImageView.shapeAppearanceModel = myShapeableImageView.shapeAppearanceModel
                .toBuilder()
                .setTopRightCorner(CornerFamily.ROUNDED, radius)
                .setTopLeftCorner(CornerFamily.ROUNDED, radius)
                .setBottomLeftCorner(CornerFamily.ROUNDED, radius)
                .setBottomRightCorner(CornerFamily.ROUNDED, radius)
                .build()
    
                // or  You can use setAllCorners() method
    
            myShapeableImageView.shapeAppearanceModel = myShapeableImageView.shapeAppearanceModel
                .toBuilder()
                .setAllCorners(CornerFamily.ROUNDED, radius)
                .build()
    
    
        }
    }
    

    OUTPUT

    Make ImageView Circle from using a style

    First, create a below style in your style.xml

    
    

    Now use that style in your layout like this

    
    

    OUTPUT

    Please find the complete exmaple here how to use ShapeableImageView

提交回复
热议问题