muhammetsahin
muhammetsahin

Reputation: 191

How can i draw circle at Xamarin?

Hello dear developers,

Im using xamarin (monotouch) i want to draw circle image view like google plus profile image or like otherones...

I was search on net but didnt find useful thing.

Somebody help me?

Thank you..

Upvotes: 3

Views: 5536

Answers (2)

Norbert Szenasi
Norbert Szenasi

Reputation: 993

For your purposes you can use UIView or UIButton. With UIButton it is easier to handle touch events.

The basic idea is to create a UIButton with specific coordinates and size and set the CornerRadius property to be one half of the size of the UIButton (assuming you want to draw a circle, width and height will be the same).

Your code could look something like this (in ViewDidLoad of your 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);

At last implement the event handler (define this method somewhere in your 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;
}

Upvotes: 8

Lex Li
Lex Li

Reputation: 63295

Xamarin.iOS is a wrapper over Cocoa Touch on UI side,

https://developer.apple.com/technologies/ios/cocoa-touch.html

So to draw circle you need to use the Cocoa Touch API, aka CoreGraphics,

http://docs.xamarin.com/videos/ios/getting-started-coregraphics/

Upvotes: 1

Related Questions