5

I am working on a Xamarin Android application, and I need to make a Circular ImageView.

How can this be achieved?

Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
Dhruv Gohil
  • 335
  • 4
  • 12
  • What have you tried to do so far? We don't want to waste your time by suggesting things you know don't work for you. – Wai Ha Lee Aug 19 '15 at 14:37

5 Answers5

6

I use the RoundedImageView library. It's written in Java, but you can write a binding to it without much problems. Once you do, you can simply add this to your .axml:

<RoundedImageView
    local:riv_corner_radius="15dp"
    local:riv_oval="false"
    android:scaleType="centerCrop"
    android:layout_width="30dp"
    android:layout_height="30dp" />

Edit for future readers: I wrote a port of the RoundedImageView for Xamarin.Android, based on the library linked on this post. The source code can be found here and the NuGet package here. A MvxRoundedImageView is also included for use with MvvmCross.

William Barbosa
  • 4,936
  • 2
  • 19
  • 37
  • it is not a good idea to do this when you need a lot of round shapes. another way would be masking a normal image view like it is done with googles "round" avatar views in hangouts and other places. – DooMMasteR Aug 19 '15 at 14:39
  • Do you have any benchmarks of this running on large applications and failling to make such claim? I used this inside a quite big RecyclerView and I got no performance issues whatsoever. Besides, OP didn't specify the amount of shapes he needed, so I believe this will do the job – William Barbosa Aug 19 '15 at 14:42
  • I had problems on slower dualcore devices with listviews (multiple thousands of entries which does not really matter) and an sql-cursor adapter scrolling would begin to lag and timing analysis lead to the conclusion that simpel overlaying was faster... my comment should only point at that possible bottleneck, not any real issue, as this solution is at least a lot cleaner an better readable than overlaying shapes – DooMMasteR Aug 19 '15 at 14:54
  • @WilliamBarbosa I got this error when compiling: `error APT0000: No resource identifier found for attribute 'riv_corner_radius' in package 'com.my.app'`. Am I missing something? – Felix May 09 '17 at 14:07
1

Refer link: https://github.com/jamesmontemagno/CircleImageView-Xamarin.Android/blob/master/CircleImageSample/Resources/layout/Main.axml?

  <refractored.controls.CircleImageView 
android:id="@+id/ImageProfile"
   android:layout_width="80dp" 
android:layout_height="80dp"
   android:scaleType="fitCenter" 
android:src="@drawable/app_icon"
   android:layout_gravity="center_horizontal"/>

=========================================================================== Add reference of Refractored.controls.CircleImageView to your project from nuget package.

Ruchira
  • 101
  • 1
  • 7
1

Xamarin component is Available for the same, you can check it here

Rajat
  • 73
  • 6
0
public class CircleDrawable : Drawable
{
    Bitmap bmp;
    BitmapShader bmpShader;
    Paint paint;
    RectF oval;

    public CircleDrawable (Bitmap bmp)
    {
        this.bmp = bmp;
        this.bmpShader = new BitmapShader (bmp, Shader.TileMode.Clamp, Shader.TileMode.Clamp);
        this.paint = new Paint () { AntiAlias = true };
        this.paint.SetShader (bmpShader);
        this.oval = new RectF ();
    }

    public override void Draw (Canvas canvas)
    {
        canvas.DrawOval (oval, paint);
    }

    protected override void OnBoundsChange (Rect bounds)
    {
        base.OnBoundsChange (bounds);
        oval.Set (0, 0, bounds.Width (), bounds.Height ());
    }

    public override int IntrinsicWidth {
        get {
            return bmp.Width;
        }
    }

    public override int IntrinsicHeight {
        get {
            return bmp.Height;
        }
    }

    public override void SetAlpha (int alpha)
    {

    }

    public override int Opacity {
        get {
            return (int)Format.Opaque;
        }
    }

    public override void SetColorFilter (ColorFilter cf)
    {

    }
}

Source: https://github.com/xamarin/xamarin-store-app/blob/master/XamarinStore.Droid/Views/CircleDrawable.cs

Daniel Luberda
  • 7,374
  • 1
  • 32
  • 40
0

Refer these links:

How to create a circular ImageView in Android?

How to make an ImageView with rounded corners?

The above code works for native android. You need to tweak the code to convert into c# syntax and adopt for xamarin android. For your convenience I have changed the code to c#.

public class ImageHelper
{
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height
            , Android.Graphics.Bitmap.Config.Argb8888);
    Canvas canvas = new Canvas(output);
    Color color = Color.DodgerBlue;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.Width, bitmap.Height);
    RectF rectF = new RectF(rect);
    float roundPx = pixels;

    paint.AntiAlias = true;
    canvas.DrawARGB(0, 0, 0, 0);
    paint.Color = color;
    canvas.DrawRoundRect(rectF, roundPx, roundPx, paint);

    paint.SetXfermode(new PorterDuffXfermode(Android.Graphics.PorterDuff.Mode.SrcIn));
    canvas.DrawBitmap(bitmap, rect, rect, paint);

    return output;
}
Community
  • 1
  • 1
Anees Deen
  • 1,385
  • 2
  • 17
  • 31