我怎样才能在Xamarin画圆圈?

muh*_*hin 3 draw xamarin.ios xamarin

亲爱的开发者你好,

我使用xamarin(monotouch)我想画像谷歌加上个人资料图片或像其他人一样的圆形图像视图...

我在网上搜索但没有找到有用的东西.

来人帮帮我?

谢谢..

Nor*_*asi 8

为了您的目的,您可以使用UIViewUIButton.使用UIButton它更容易处理触摸事件.

基本思想是创建一个UIButton具有特定坐标和大小并将CornerRadius属性设置为大小的一半UIButton(假设您想绘制一个圆,宽度和高度将是相同的).

你的代码可能看起来像这样(在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);
Run Code Online (Sandbox Code Playgroud)

最后实现事件处理程序(在你的某处定义此方法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;
}
Run Code Online (Sandbox Code Playgroud)