Forum Xamarin Xamarin.Android

How create circle image dynamically not using XML in Xamarin Android?

before ask this question i found it on google but there is lot of solution for Xamarin.form but i want to in Xamarin.Android
Right now i m getting images from gallery using this code

Intent = new Intent();
StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);

then set image using this code

imgConfirmPic = FindViewById (Resource.Id.imgCamera);

so i got image in Square shape but i want to put image in circle shape.

Plz anyone have idea about it let me know

Thanx in advance



  • BatzeeBatzee LKMember ✭✭
    edited May 2016

    I have achieved this by Creating my own class called RoundedImageView that Extends ImageView
    And Used it like this in my Layout

    <YourAppNameSpace.RoundedImageForLoginPage android:src="@drawable/icon" android:layout_width="70dp" android:layout_height="70dp" android:gravity="center" android:id="@+id/imgProfile" android:visibility="gone" android:layout_gravity="center_horizontal" />

    Or if you want to use it programmatically you can use it like this

    RoundedImageView imageProfile = new RoundedImageView (context);

    And use it like a normal Image View
    Below is the code for the Class

    class RoundedImageView : ImageView { protected RoundedImageView (IntPtr javaReference, JniHandleOwnership transfer): base(javaReference, transfer) { }
    public RoundedImageView (Context context) : base(context)

        public RoundedImageView (Context context, IAttributeSet attrs) : base(context, attrs)
        public RoundedImageView (Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
        public RoundedImageView (Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes)
            : base(context, attrs, defStyleAttr, defStyleRes)
        private void init()
            SetLayerType(LayerType.Software, null);
        protected override void OnDraw(Canvas canvas)
            float radius = 150.0f; // angle of round corners
            Path clipPath = new Path();
            RectF rect = new RectF(0, 0, this.Width, this.Height);
            clipPath.AddRoundRect(rect, radius, radius, Path.Direction.Cw);
        public static Bitmap getCroppedBitmap(Bitmap bmp, int radius)
            Bitmap sbmp;
            if (bmp.Width != radius || bmp.Height != radius)
                sbmp = Bitmap.CreateScaledBitmap(bmp, radius, radius, false);
                sbmp = bmp;
            Bitmap output = Bitmap.CreateBitmap(sbmp.Width, sbmp.Height, Bitmap.Config.Argb8888);
            Canvas canvas = new Canvas(output);
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, sbmp.Width, sbmp.Height);
            paint.AntiAlias = true;
            paint.FilterBitmap = true;
            paint.Dither = true;
            canvas.DrawARGB(0, 0, 0, 0);
            paint.Color = (Color.ParseColor("#BAB399"));
            canvas.DrawCircle(sbmp.Width / 2 + 0.0f, sbmp.Height / 2 + 0.0f, sbmp.Width / 2 + 0.0f, paint);
            paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn));
                canvas.DrawBitmap(sbmp, rect, rect, paint);
            catch (Exception ex)
                Log.Debug("Round Image Crash", ex.ToString());
            return output;
Sign In or Register to comment.