How can I draw a circle on Xamarin?

Hi dear developers,

I use xamarin (monotouch), I want to draw a circular image, for example, a Google+ image with a profile or like others ...

I searched the web but did not find a useful thing.

Anyone help me?

Thank..

+3
source share
2 answers

For your purposes you can use UIViewor UIButton. With UIButtoneasier to handle touch events.

The main idea is to create UIButtonwith certain coordinates and size and set the property to CornerRadiushalf size UIButton(provided that you want to draw a circle, the width and height will be the same).

( ViewDidLoad UIViewController):

// define coordinates and size of the circular view
float x = 50;
float y = 50;
float width = 200;
float height = width;
// corner radius needs to be one half of the size of the view
float cornerRadius = width / 2;
RectangleF frame = new RectangleF(x, y, width, height);
// initialize button
UIButton circularView = new UIButton(frame);
// set corner radius
circularView.Layer.CornerRadius = cornerRadius;
// set background color, border color and width to see the circular view
circularView.BackgroundColor = UIColor.White;
circularView.Layer.CornerRadius = cornerRadius;
circularView.Layer.BorderColor = UIColor.Red.CGColor;
circularView.Layer.BorderWidth = 5;
// handle touch up inside event of the button
circularView.TouchUpInside += HandleCircularViewTouchUpInside;
// add button to view controller
this.View.Add(circularView);

, ( - UIViewController:

private void HandleCircularViewTouchUpInside(object sender, EventArgs e)
{
   // initialize random
   Random rand = new Random(DateTime.Now.Millisecond);
   // when the user 'clicks' on the circular view, randomly change the border color of the view
   (sender as UIButton).Layer.BorderColor = UIColor.FromRGB(rand.Next(255), rand.Next(255), rand.Next(255)).CGColor;
}
+7

All Articles