How to make an ImageView with rounded corners?

前端 未结 30 2563
天涯浪人
天涯浪人 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:18

    As all the answers seemed too complicated for me just for round corners I thought and came to another solution which I think is worth to share, just with XML in case you have some space around the image:

    Create a bordered shape with transparent content like this:

    
    
        
        
     
    

    Then in a RelativeLayout you can first place your image and then in the same location above the shape with another ImageView. The cover-shape should be larger in size by the amount of the border width. Be careful to take a larger corner radius as the outer radius is defined but the inner radius is what covers your image.

    Hope it helps somebody, too.

    Edit as per CQM request the relative layout example:

    
    
    
        
    
        
    
    
    

提交回复
热议问题