How to make an ImageView with rounded corners?

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

    In the v21 of the Support library there is now a solution to this: it's called RoundedBitmapDrawable.

    It's basically just like a normal Drawable except you give it a corner radius for the clipping with:

    setCornerRadius(float cornerRadius)
    

    So, starting with Bitmap src and a target ImageView, it would look something like this:

    RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.create(res, src);
    dr.setCornerRadius(cornerRadius);
    imageView.setImageDrawable(dr);
    

提交回复
热议问题