public static UIImage GetUIImage(string resource, double width, double height, Xamarin.Forms.Color color) { if (color == Xamarin.Forms.Color.Default) { return(GetUIImage(resource, width, height)); } var g = SvgLoader.GetResourceAndLoadSvg(resource); var transform = Transform.AspectFillRect(g.ViewBox, new Rect(0, 0, width, height)); var transGraphic = g.TransformGeometry(transform); var canvas = Platforms.Current.CreateImageCanvas( new NGraphics.Size(width, height), UIScreen.MainScreen.Scale); var nColor = new NGraphics.Color(color.R, color.G, color.B, color.A); foreach (var element in transGraphic.Children) { ApplyColor(element, nColor); element.Draw(canvas); } return(canvas.GetImage().GetUIImage()); }
public IImage GetRoundedImage(Size size, NGraphics.Color color, float radius) { size *= _scale; UIGraphics.BeginImageContextWithOptions(new CGSize(size.Width, size.Height), false, 0); IImage image = null; using (var context = UIGraphics.GetCurrentContext()) { context.SetAllowsAntialiasing(true); context.SetShouldAntialias(true); context.SetFillColor(UIColor.FromRGBA(color.R, color.G, color.B, color.A).CGColor); context.AddPath(CGPath.FromRoundedRect(new CGRect(0, 0, size.Width, size.Height), radius, radius)); context.FillPath(); } var nativeImage = UIGraphics.GetImageFromCurrentImageContext(); image = new CGImageImage(nativeImage.CGImage, _scale); return(image); }
UIImage GetUIImage(Stream stream, double width, double height, Color color, float scale) { Graphic g = null; using (var sr = new StreamReader(stream)) { g = Graphic.LoadSvg(sr); } var newSize = SvgUtility.CalcAspect(g.Size, width, height); if (width > 0 || height > 0) { g = SvgUtility.Resize(g, newSize); } if (scale <= 1) { scale = ScreenScale; } var canvas = Platforms.Current.CreateImageCanvas(newSize, scale); if (color != Xamarin.Forms.Color.Default) { var nColor = new NGraphics.Color(color.R, color.G, color.B, color.A); foreach (var element in g.Children) { SvgUtility.ApplyColor(element, nColor); element.Draw(canvas); } } else { g.Draw(canvas); } return(canvas.GetImage().GetUIImage()); }
private static void ApplyColor(NGraphics.Element element, NGraphics.Color color) { var children = (element as Group)?.Children; if (children != null) { foreach (var child in children) { ApplyColor(child, color); } } if (element?.Pen != null) { element.Pen = new Pen(color, element.Pen.Width); } if (element?.Brush != null) { element.Brush = new SolidBrush(color); } }
/// <summary> /// Initializes a new instance of the /// <see cref="NControlDemo.Forms.Xamarin.Plugins.FormsApp.Controls.CircularButtonControl"/> class. /// </summary> public CircularButtonControl() { HeightRequest = 44; WidthRequest = 44; _label = new Label { Text = "+", TextColor = Xamarin.Forms.Color.White, FontSize = 17, BackgroundColor = Xamarin.Forms.Color.Transparent, HorizontalTextAlignment = Xamarin.Forms.TextAlignment.Center, VerticalTextAlignment = Xamarin.Forms.TextAlignment.Center, }; _circles = new NControlView { DrawingFunction = (canvas1, rect) => { var fillColor = new NGraphics.Color(FillColor.R, FillColor.G, FillColor.B, FillColor.A); canvas1.FillEllipse(rect, fillColor); rect.Inflate(new NGraphics.Size(-2, -2)); canvas1.FillEllipse(rect, Colors.White); rect.Inflate(new NGraphics.Size(-4, -4)); canvas1.FillEllipse(rect, fillColor); } }; Content = new Grid { Children = { _circles, _label, } }; }
/// <summary> /// Initializes a new instance of the /// <see cref="NControlDemo.Forms.Xamarin.Plugins.FormsApp.Controls.CircularButtonControl"/> class. /// </summary> public CircularButtonControl() { HeightRequest = 44; WidthRequest = 44; _label = new FontAwesomeLabel { Text = FontAwesomeLabel.FAAdjust, TextColor = Xamarin.Forms.Color.White, FontSize = 17, BackgroundColor = Xamarin.Forms.Color.Transparent, XAlign = Xamarin.Forms.TextAlignment.Center, YAlign = Xamarin.Forms.TextAlignment.Center, }; _circles = new NControlView { DrawingFunction = (canvas1, rect) => { var fillColor = new NGraphics.Color(FillColor.R, FillColor.G, FillColor.B, FillColor.A); canvas1.FillEllipse(rect, fillColor); rect.Inflate(new NGraphics.Size(-2, -2)); canvas1.FillEllipse(rect, Colors.White); rect.Inflate(new NGraphics.Size(-4, -4)); canvas1.FillEllipse(rect, fillColor); } }; Content = new Grid{ Children = { _circles, _label, } }; }
private void DrawIcon(ICanvas canvas, NGraphics.Rect rect) { if(Graphic == null) return; var padding = ((rect.Size * IconScale) - rect.Size) / 2; var transform = Transform.AspectFillRect(Graphic.ViewBox, rect.GetInflated(padding)); var transformed = Graphic.TransformGeometry(transform); var color = new NGraphics.Color( FillColor.R, FillColor.G, FillColor.B, FillColor.A); foreach(var element in transformed.Children) { if(OverpaintEnabled) { ApplyColor(element, color); } element.Draw(canvas); } }
private void DrawCircle(ICanvas canvas, NGraphics.Rect rect) { var color = new NGraphics.Color(BackColor.R, BackColor.G, BackColor.B, BackColor.A); canvas.FillEllipse(rect, color); }
public double DrawSegment(NGraphics.ICanvas canvas, NGraphics.Point center, double radius, double startRadian, double pullLength, NGraphics.Font font, NGraphics.Color textColor, bool isNameVisible, NGraphics.Color percentColor, bool isPercentVisible, NGraphics.Color valueColor, bool isValueVisible) { double middleRadian = startRadian + Radian / 2.0; if (IsPull) { center = new NGraphics.Point( center.X + pullLength * Math.Cos(middleRadian), center.Y + pullLength * Math.Sin(middleRadian)); } var brush = new SolidBrush((Color ?? NGraphics.Colors.Black)); var pen = new Pen(PenColor, PenWidth); canvas.DrawPath((path) => { const double shardRadian = 0.02; path.MoveTo(center); path.LineTo(center.X + Math.Cos(startRadian) * radius, center.Y + Math.Sin(startRadian) * radius); for (var addRadian = 0.0; addRadian < Radian; addRadian += shardRadian) { path.LineTo(center.X + Math.Cos(startRadian + addRadian) * radius, center.Y + Math.Sin(startRadian + addRadian) * radius); } path.LineTo(center.X + Math.Cos(startRadian + Radian) * radius, center.Y + Math.Sin(startRadian + Radian) * radius); path.Close(); }, pen, brush); if (isNameVisible) { DrawName(canvas, center, radius, font, TitleColor ?? textColor, middleRadian); } if (isValueVisible) { DrawValue(canvas, center, radius, font, ValueColor ?? valueColor, middleRadian); } if (isPercentVisible) { DrawPercent(canvas, center, radius, font, PercentColor ?? percentColor, middleRadian); } return(startRadian + Radian); }
void DrawText(string text, ICanvas canvas, NGraphics.Point center, double radius, NGraphics.Font font, NGraphics.Color textColor, double middleRadian) { if (!string.IsNullOrWhiteSpace(text)) { double textRadius = radius * 0.67; NGraphics.Point centerText = new NGraphics.Point(center.X + Math.Cos(middleRadian) * textRadius, center.Y + Math.Sin(middleRadian) * textRadius); const double width = 40; const double height = 5; Rect rect = new Rect(centerText.X - width / 2.0, centerText.Y, width, height); canvas.DrawText(text, rect, font, NGraphics.TextAlignment.Center, new Pen(textColor), new SolidBrush(textColor)); } }
void DrawValue(ICanvas canvas, NGraphics.Point center, double radius, NGraphics.Font font, NGraphics.Color textColor, double middleRadian) { var valueCenter = new NGraphics.Point(center.X, center.Y); DrawText(Value.ToString("N"), canvas, valueCenter, radius, font, textColor, middleRadian); }
void DrawPercent(ICanvas canvas, NGraphics.Point center, double radius, NGraphics.Font font, NGraphics.Color textColor, double middleRadian) { var percentCenter = new NGraphics.Point(center.X, center.Y + 20); DrawText(Percent.ToString("P1"), canvas, percentCenter, radius, font, textColor, middleRadian); }
void DrawName(ICanvas canvas, NGraphics.Point center, double radius, NGraphics.Font font, NGraphics.Color textColor, double middleRadian) { var percentCenter = new NGraphics.Point(center.X, center.Y - 20); DrawText(Title ?? "", canvas, percentCenter, radius, font, textColor, middleRadian); }
public override void Draw(ICanvas canvas, Rect rect) { #region variables _middleX = rect.Width / 2d; _middleY = rect.Height / 2d; _sectorRad = (float)(2 * Math.PI / Colors.Count); _radius = Math.Min(rect.Width, rect.Height) * 40d / 100d; _innerD = Math.Min(rect.Width, rect.Height) * 20d / 100d; #endregion #region back circle var outerD = _radius * 2; canvas.DrawEllipse( (rect.Width - outerD) / 2d - 15, (rect.Height - outerD) / 2d - 15, outerD + 30, outerD + 30, null, new SolidBrush(new Color(76d / 255d, 84d / 255d, 95d / 255d))); canvas.DrawEllipse( (rect.Width - outerD) / 2d - 5, (rect.Height - outerD) / 2d - 5, outerD + 10, outerD + 10, null, new SolidBrush(new Color(54d / 255d, 61d / 255d, 71d / 255d))); #endregion #region sectors var px0 = _middleX + (float)(_radius * Math.Cos(-_sectorRad)); var py0 = _middleY + (float)(_radius * Math.Sin(-_sectorRad)); paths = new List <List <PathOp> >(); for (var i = 0; i < Colors.Count; i++) { var angle = _sectorRad * i; var px = _middleX + (float)(_radius * Math.Cos(angle)); var py = _middleY + (float)(_radius * Math.Sin(angle)); var path = new List <PathOp>(); if (Xamarin.Forms.Device.OS == TargetPlatform.Android) { path = new List <PathOp> { new MoveTo(_middleX, _middleY), new LineTo(px0, py0), new ArcTo(new Size(_radius), false, true, new Point(px, py)), new LineTo(_middleX, _middleY) }; } else { path = new List <PathOp> { new MoveTo(_middleX, _middleY), new LineTo(px, py), new ArcTo(new Size(_radius), true, true, new Point(px0, py0)), new LineTo(_middleX, _middleY) }; } paths.Add(path); px0 = px; py0 = py; } for (var i = 0; i < Colors.Count; i++) { canvas.DrawPath(paths[i], new Pen(Colors[i]), new SolidBrush(Colors[i])); } #endregion #region inner circle var rdelta = 1.05d; canvas.DrawEllipse( _middleX - _radius * rdelta, _middleY - _radius * rdelta, _radius * 2 * rdelta, _radius * 2 * rdelta, null, new SolidBrush(new Color(0d, 0d, 0d, 52 / 256d))); var middleColor = Color.FromWhite(1); canvas.DrawEllipse( _middleX - _innerD / 2d, _middleY - _innerD / 2d, _innerD, _innerD, null, new SolidBrush(middleColor)); #endregion #region draw selected color if (SelectedColor != -1) { var angle = _sectorRad * (SelectedColor - 1); var px1 = _middleX + (float)(_radius * rdelta * Math.Cos(angle)); var py1 = _middleY + (float)(_radius * rdelta * Math.Sin(angle)); angle = _sectorRad * SelectedColor; var px2 = _middleX + (float)(_radius * rdelta * Math.Cos(angle)); var py2 = _middleY + (float)(_radius * rdelta * Math.Sin(angle)); var path = new List <PathOp>(); if (Xamarin.Forms.Device.OS == TargetPlatform.Android) { path = new List <PathOp> { new MoveTo(_middleX, _middleY), new LineTo(px1, py1), new ArcTo(new Size(_radius), false, true, new Point(px2, py2)), new LineTo(_middleX, _middleY) } } ; else { path = new List <PathOp> { new MoveTo(_middleX, _middleY), new LineTo(px2, py2), new ArcTo(new Size(_radius), true, true, new Point(px1, py1)), new LineTo(_middleX, _middleY) } }; canvas.DrawPath(path, new Pen(middleColor, 2), new SolidBrush(Colors[SelectedColor].WithBrightness(0.8d))); canvas.DrawEllipse( _middleX - _innerD / 2d + 2d, _middleY - _innerD / 2d + 2d, _innerD - 4d, _innerD - 4d, null, new SolidBrush(Colors[SelectedColor].WithBrightness(0.8d))); } #endregion }