示例#1
0
            UIImage DrawShape(ShapeType shapeType, CGSize size, UIColor strokeColor, UIColor fillColor)
            {
                float   width  = (float)size.Width;
                float   height = (float)size.Height;
                var     stroke = 1;
                var     scale  = (width / 2) - (stroke * 2);
                var     center = new Vector2(width / 2, height / 2);
                UIImage starImage;

                UIGraphics.BeginImageContext(new CGSize(width, height));
                using (CGContext g = UIGraphics.GetCurrentContext())
                {
                    g.SetLineWidth(stroke);
                    FillColor.ToUIColor().SetFill();
                    StrokeColor.ToUIColor().SetStroke();
                    var shapePoints = new Vector2[0];
                    switch (shapeType)
                    {
                    case ShapeType.Oval:
                        var radiusX = (width / 3) - stroke;
                        var radiusY = (height / 2) - stroke;
                        g.AddEllipseInRect(new CGRect(0, 0, radiusX * 2, radiusY * 2));
                        break;

                    case ShapeType.Circle:
                        var radius = Math.Min(width, height) / 2 - stroke;
                        g.AddEllipseInRect(new CGRect(0, 0, radius * 2, radius * 2));
                        break;

                    case ShapeType.Line:
                        shapePoints = CreateLineGeometry(scale, center);
                        break;

                    case ShapeType.Hexagon:
                        shapePoints = CreateHexagonGeometry(scale, center);
                        break;

                    case ShapeType.Rectangle:
                        shapePoints = CreateRectangleGeometry(scale, center);
                        break;

                    case ShapeType.Square:
                        shapePoints = CreateSquareGeometry(scale, center);
                        break;

                    case ShapeType.Trapezoid:
                        shapePoints = CreateTrapezoidGeometry(scale, center);
                        break;

                    case ShapeType.Triangle:
                        shapePoints = CreateTriangleGeometry(scale, center);
                        break;

                    case ShapeType.Star:
                        shapePoints = CreateStarGeometry(scale, center);
                        break;

                    default:

                        break;
                    }
                    if (shapePoints.Count() > 0)
                    {
                        var points = new List <CGPoint>();

                        foreach (var item in shapePoints)
                        {
                            points.Add(new CGPoint(item.X, item.Y));
                        }

                        var path = new CGPath();

                        path.AddLines(points.ToArray());

                        path.CloseSubpath();

                        g.AddPath(path);
                    }
                    g.DrawPath(CGPathDrawingMode.FillStroke);
                    starImage = UIGraphics.GetImageFromCurrentImageContext();
                }

                return(starImage);
            }