我该如何在Xamarin中绘制圆形?

3

亲爱的开发者们,

我正在使用xamarin(monotouch),想要绘制像Google Plus个人资料图片或其他类似的圆形图像视图...

我已经在网上搜索了一些东西,但是没有找到有用的内容。

有人可以帮助我吗?

谢谢。


我自己找到了解决方案。感谢我自己 :) - muhammetsahin
解决方案是什么样子的?请发布您的答案。 - testing
2个回答

8

对于你的需求,你可以使用 UIViewUIButton。使用 UIButton 更容易处理触摸事件。

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

你的代码可能看起来像这样(在你的 UIViewControllerViewDidLoad 中):

// 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;
}

1
很高兴听到这个好消息 :-). 你可以创建自己的答案并在几天后接受它。 - Norbert Szenasi

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接