10

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

Monali
  • 1,966
  • 12
  • 39
  • 59
  • 3
    Out of curiosity, how did you make a circular image view? – Mark Jan 03 '13 at 23:49
  • You can set something like android:background="@drawable/shape_border_radious" in ImageView xml and then create the circular shape – CGR Oct 12 '17 at 21:32

2 Answers2

52

I am curious about how you created a circular ImageView. Can you share that secret ?? As far as creating a circular Bitmap is concerned, create a BitmapShader from the bitmap you want to show. Then create a ShapeDrawable (Oval) and assign the bitmap shader to it. Draw the drawable. Bam! circular image!

Bitmap bitmap = getthebitmapyouwanttoshowinacirclefromsomewhere;
Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

BitmapShader shader = new BitmapShader (bitmap,  TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
        paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);

myImageView.setImageBitmap(circleBitmap);
SKAS
  • 15
  • 5
pumpkee
  • 3,357
  • 3
  • 27
  • 34
  • 1
    i don't know but setting property android:background:@drawable/round tolayout file,where round.xml is s drawable file with shape as root property having value oval will create circular imageview.Please clear some details if you have created circular imageview by any other means. – kaushal trivedi Feb 10 '13 at 12:00
  • 1
    Thanks for the simple and very functional code, works perfectly! – Pelanes Apr 10 '13 at 22:48
  • 2
    This solution is inefficient as it creates an unnecessary off-screen bitmap. Wrap the bitmap in a [RoundedBitmapDrawable](https://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawable.html) from the support library instead. – Alex Lockwood Apr 25 '15 at 18:27
  • I had a problem using this - with pictures which came in as portrait it woked fine, but in images which are landscape the bottom of the picture get's stretched out...any idea what I need to do to fix this? – Kibi Jun 07 '15 at 15:22
  • Oh, so I found the fix...need to fix so that the bitmap you put in the shader is bigger or equal in both dimensions than the target circle bitmap – Kibi Jun 07 '15 at 16:22
  • works well, but anti aliasing doesn't seems to have much effect. Resultant bitmap has jaggies(aliasing)! – Muhammad Babar Jul 14 '16 at 08:14
  • 1
    @AlexLockwood i tried RoundedBitmapDrawable and it's aliasing is even more poor than the above solution. And please elaborate how come above solution creates and off-screen bitmap? and how do the RoundedBitmapDrawable works? – Muhammad Babar Jul 14 '16 at 08:19
  • @AlexLockwood I tried using RoundedBitmapDrawable, but it has no option of setting the circle according to the ImageView height and width.. :( – Sharp Edge Aug 02 '16 at 19:12
0

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

Re'em
  • 1,869
  • 1
  • 22
  • 28