'How to set bitmap in circular imageview?

I have 1 circular imageview and I want to place bitmap inside the imageview. but when I set the compressed bitmap image it is always rectangle. Please help me to set bitmap in circular imageview.

Thanks



Solution 1:[1]

Androidx released native support for this.

add to your build.gradle:

dependencies { 
  ...
  implementation "androidx.core:core-ktx:1.7.0"
  }

usage (kotlin is shown here, java is also supported):

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;



val bitmap: Bitmap = ... // get from somewhere

val roundedBitmapWrapper: RoundedBitmapDrawable =
          RoundedBitmapDrawableFactory.create(Resources.getSystem(), bitmap)

roundedBitmapWrapper.setCircular(true) // important! if you don't call this, the wrapper will just show the original rectangle bitmap


// and then you can display the roundedBitmap on an ImageView like this:
val imageView: ImageView = ... // get from somewhere
imageView.setImageDrawable(roundedBitmapWrapper)

P.S., the class RoundedBitmapDrawable has a lot of other cool stuff, for example if you don't want the bitmap to be completely circle, but instead just give some rounded-corners effect for the original rectangle bitmap, this can also be supported. check out the official doc at https://developer.android.com/reference/androidx/core/graphics/drawable/RoundedBitmapDrawable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Re'em