private protected void Render( Android.Graphics.Path path, Windows.Foundation.Size?size = null, double scaleX = 1d, double scaleY = 1d, double renderOriginX = 0d, double renderOriginY = 0d) { _path = path; if (_path == null) { return; } var matrix = new Android.Graphics.Matrix(); matrix.SetScale((float)scaleX * (float)ViewHelper.Scale, (float)scaleY * (float)ViewHelper.Scale); matrix.PostTranslate(ViewHelper.LogicalToPhysicalPixels(renderOriginX), ViewHelper.LogicalToPhysicalPixels(renderOriginY)); _path.Transform(matrix); _drawArea = GetPathBoundingBox(_path); _drawArea.Width = size?.Width ?? _drawArea.Width; _drawArea.Height = size?.Height ?? _drawArea.Height; Invalidate(); }
protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); if (_path == null) { return; } AMatrix transformMatrix = CreateMatrix(); _path.Transform(transformMatrix); transformMatrix.MapRect(_pathFillBounds); transformMatrix.MapRect(_pathStrokeBounds); if (_fill != null) { _drawable.Paint.SetStyle(Paint.Style.Fill); _drawable.Paint.Color = _fill; _drawable.Draw(canvas); _drawable.Paint.SetShader(null); } if (_stroke != null) { _drawable.Paint.SetStyle(Paint.Style.Stroke); _drawable.Paint.Color = _stroke; _drawable.Draw(canvas); _drawable.Paint.SetShader(null); } AMatrix inverseTransformMatrix = new AMatrix(); transformMatrix.Invert(inverseTransformMatrix); _path.Transform(inverseTransformMatrix); inverseTransformMatrix.MapRect(_pathFillBounds); inverseTransformMatrix.MapRect(_pathStrokeBounds); }
private void TouchUp() { var left = (_canvasSize.Width - _mutableForeground.Width) / 2; var top = (_canvasSize.Height - _mutableForeground.Height) / 2; if (_currentStroke.IsEmpty) { _strokeBrush.SetStyle(Paint.Style.Fill); ForegroundMutator.DrawCircle((float)(_currentTouch.X - left), (float)(_currentTouch.Y - top), (float)StrokeThickness / 2, _strokeBrush); } else { _currentStroke.LineTo((float)_currentTouch.X, (float)_currentTouch.Y); // Commit the path to our offscreen canvas var m = new Matrix(); m.SetTranslate((float)-left, (float)-top); _currentStroke.Transform(m); ForegroundMutator.DrawPath(_currentStroke, _strokeBrush); } // Reset the stroke so that it is not redrawn in OnDraw. _currentStroke.Reset(); }
private void DrawStrokesOnCanvas (Canvas canvas, Color strokeColor, Color fillColor, bool shouldCrop, RectF croppedRectangle = null) { canvas.DrawColor (fillColor); paint.Color = strokeColor; foreach (var path in paths) { var tempPath = path; if (shouldCrop) { tempPath = new Path (path); var translate = new Matrix (); translate.SetTranslate (-croppedRectangle.Left, -croppedRectangle.Top); tempPath.Transform (translate); } canvas.DrawPath (tempPath, paint); tempPath = null; } paint.Color = this.strokeColor; }
protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); if (_path == null) { return; } AMatrix transformMatrix = CreateMatrix(); _path.Transform(transformMatrix); transformMatrix.MapRect(_pathFillBounds); transformMatrix.MapRect(_pathStrokeBounds); if (_fill != null) { _drawable.Paint.SetStyle(Paint.Style.Fill); if (_fill is GradientBrush fillGradientBrush) { if (fillGradientBrush is LinearGradientBrush linearGradientBrush) { _fillShader = CreateLinearGradient(linearGradientBrush, _pathFillBounds); } if (fillGradientBrush is RadialGradientBrush radialGradientBrush) { _fillShader = CreateRadialGradient(radialGradientBrush, _pathFillBounds); } _drawable.Paint.SetShader(_fillShader); } else { AColor fillColor = Colors.Transparent.ToAndroid(); if (_fill is SolidColorBrush solidColorBrush && solidColorBrush.Color != null) { fillColor = solidColorBrush.Color.ToAndroid(); } _drawable.Paint.Color = fillColor; } _drawable.Draw(canvas); _drawable.Paint.SetShader(null); } if (_stroke != null) { _drawable.Paint.SetStyle(Paint.Style.Stroke); if (_stroke is GradientBrush strokeGradientBrush) { UpdatePathStrokeBounds(); if (strokeGradientBrush is LinearGradientBrush linearGradientBrush) { _strokeShader = CreateLinearGradient(linearGradientBrush, _pathStrokeBounds); } if (strokeGradientBrush is RadialGradientBrush radialGradientBrush) { _strokeShader = CreateRadialGradient(radialGradientBrush, _pathStrokeBounds); } _drawable.Paint.SetShader(_strokeShader); } else { AColor strokeColor = Graphics.Colors.Transparent.ToAndroid(); if (_stroke is SolidColorBrush solidColorBrush && solidColorBrush.Color != null) { strokeColor = solidColorBrush.Color.ToAndroid(); } _drawable.Paint.Color = strokeColor; } _drawable.Draw(canvas); _drawable.Paint.SetShader(null); } AMatrix inverseTransformMatrix = new AMatrix(); transformMatrix.Invert(inverseTransformMatrix); _path.Transform(inverseTransformMatrix); inverseTransformMatrix.MapRect(_pathFillBounds); inverseTransformMatrix.MapRect(_pathStrokeBounds); }
private static void scalePath(Path path, float density) { if (density == PATH_GEN_DENSITY) return; Matrix scaleMatrix = new Matrix(); scaleMatrix.SetScale(density / PATH_GEN_DENSITY, density / PATH_GEN_DENSITY, 0, 0); path.Transform(scaleMatrix); }
public override void DrawPath (Path path, Paint paint) { Path dst = new Path (); GetMatrix (matrix); path.Transform (matrix, dst); helper.Paths.Add (new SvgPath (dst, new Paint (helper.SourcePaint))); }
public object ConvertToNative(Geometry geometry) { // TODO: Can we keep this object around and reset it? droidGraphics.Path path = new droidGraphics.Path(); // TODO: Might we also save a non-transformed path and a transformed path? -- No, that fails for GeometryGroup // Determine what type of Geometry we're dealing with if (geometry is LineGeometry) { LineGeometry lineGeometry = geometry as LineGeometry; path.MoveTo(PixelsPerDip * (float)lineGeometry.StartPoint.X, PixelsPerDip * (float)lineGeometry.StartPoint.Y); path.LineTo(PixelsPerDip * (float)lineGeometry.EndPoint.X, PixelsPerDip * (float)lineGeometry.EndPoint.Y); } else if (geometry is RectangleGeometry) { Rect rect = (geometry as RectangleGeometry).Rect; path.AddRect(PixelsPerDip * (float)rect.Left, PixelsPerDip * (float)rect.Top, PixelsPerDip * (float)rect.Right, PixelsPerDip * (float)rect.Bottom, droidGraphics.Path.Direction.Cw); // TODO: Check for compatibility! } else if (geometry is EllipseGeometry) { EllipseGeometry ellipseGeometry = geometry as EllipseGeometry; path.AddOval(new droidGraphics.RectF( PixelsPerDip * (float)(ellipseGeometry.Center.X - ellipseGeometry.RadiusX), PixelsPerDip * (float)(ellipseGeometry.Center.Y - ellipseGeometry.RadiusY), PixelsPerDip * (float)(ellipseGeometry.Center.X + ellipseGeometry.RadiusX), PixelsPerDip * (float)(ellipseGeometry.Center.Y + ellipseGeometry.RadiusY)), droidGraphics.Path.Direction.Cw); // TODO: Check for compatibility! } else if (geometry is GeometryGroup) { GeometryGroup geometryGroup = geometry as GeometryGroup; SetFillRule(path, geometryGroup.FillRule); foreach (Geometry child in geometryGroup.Children) { droidGraphics.Path childPath = ConvertToNative(child) as droidGraphics.Path; path.AddPath(childPath); } } else if (geometry is PathGeometry) { PathGeometry pathGeometry = geometry as PathGeometry; SetFillRule(path, pathGeometry.FillRule); foreach (PathFigure pathFigure in pathGeometry.Figures) { path.MoveTo(PixelsPerDip * (float)pathFigure.StartPoint.X, PixelsPerDip * (float)pathFigure.StartPoint.Y); Point lastPoint = pathFigure.StartPoint; foreach (PathSegment pathSegment in pathFigure.Segments) { // LineSegment if (pathSegment is LineSegment) { LineSegment lineSegment = pathSegment as LineSegment; path.LineTo(PixelsPerDip * (float)lineSegment.Point.X, PixelsPerDip * (float)lineSegment.Point.Y); lastPoint = lineSegment.Point; } // PolylineSegment else if (pathSegment is PolyLineSegment) { PolyLineSegment polylineSegment = pathSegment as PolyLineSegment; PointCollection points = polylineSegment.Points; for (int i = 0; i < points.Count; i++) { path.LineTo(PixelsPerDip * (float)points[i].X, PixelsPerDip * (float)points[i].Y); } lastPoint = points[points.Count - 1]; } // BezierSegment else if (pathSegment is BezierSegment) { BezierSegment bezierSegment = pathSegment as BezierSegment; path.CubicTo(PixelsPerDip * (float)bezierSegment.Point1.X, PixelsPerDip * (float)bezierSegment.Point1.Y, PixelsPerDip * (float)bezierSegment.Point2.X, PixelsPerDip * (float)bezierSegment.Point2.Y, PixelsPerDip * (float)bezierSegment.Point3.X, PixelsPerDip * (float)bezierSegment.Point3.Y); lastPoint = bezierSegment.Point3; } // PolyBezierSegment else if (pathSegment is PolyBezierSegment) { PolyBezierSegment polyBezierSegment = pathSegment as PolyBezierSegment; PointCollection points = polyBezierSegment.Points; for (int i = 0; i < points.Count; i += 3) { path.CubicTo(PixelsPerDip * (float)points[i + 0].X, PixelsPerDip * (float)points[i + 0].Y, PixelsPerDip * (float)points[i + 1].X, PixelsPerDip * (float)points[i + 1].Y, PixelsPerDip * (float)points[i + 2].X, PixelsPerDip * (float)points[i + 2].Y); } lastPoint = points[points.Count - 1]; } // QuadraticBezierSegment else if (pathSegment is QuadraticBezierSegment) { QuadraticBezierSegment bezierSegment = pathSegment as QuadraticBezierSegment; path.QuadTo(PixelsPerDip * (float)bezierSegment.Point1.X, PixelsPerDip * (float)bezierSegment.Point1.Y, PixelsPerDip * (float)bezierSegment.Point2.X, PixelsPerDip * (float)bezierSegment.Point2.Y); lastPoint = bezierSegment.Point2; } // PolyQuadraticBezierSegment else if (pathSegment is PolyQuadraticBezierSegment) { PolyQuadraticBezierSegment polyBezierSegment = pathSegment as PolyQuadraticBezierSegment; PointCollection points = polyBezierSegment.Points; for (int i = 0; i < points.Count; i += 2) { path.QuadTo(PixelsPerDip * (float)points[i + 0].X, PixelsPerDip * (float)points[i + 0].Y, PixelsPerDip * (float)points[i + 1].X, PixelsPerDip * (float)points[i + 1].Y); } lastPoint = points[points.Count - 1]; } // ArcSegment else if (pathSegment is ArcSegment) { ArcSegment arcSegment = pathSegment as ArcSegment; List <Point> points = new List <Point>(); Xamarin.Forms.Shapes.Shapes.FlattenArc(points, lastPoint, arcSegment.Point, arcSegment.Size.Width, arcSegment.Size.Height, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection == SweepDirection.Counterclockwise, 1); for (int i = 0; i < points.Count; i++) { path.LineTo(PixelsPerDip * (float)points[i].X, PixelsPerDip * (float)points[i].Y); } lastPoint = points[points.Count - 1]; } } if (pathFigure.IsClosed) { path.Close(); } } } // Apply transform if (geometry.Transform != null) { path.Transform((droidGraphics.Matrix)geometry.Transform.GetNativeObject()); } return(path); }
public override void Draw(Canvas canvas) { if (_path == null) { return; } AMatrix transformMatrix = CreateMatrix(); _path.Transform(transformMatrix); transformMatrix.MapRect(_pathFillBounds); transformMatrix.MapRect(_pathStrokeBounds); if (_fill != null) { this.Paint.SetStyle(_strokeWidth > 0 ? Paint.Style.Fill : Paint.Style.FillAndStroke); if (_fill is GradientBrush fillGradientBrush) { if (fillGradientBrush is LinearGradientBrush linearGradientBrush) { _fillShader = CreateLinearGradient(linearGradientBrush, _pathFillBounds); } if (fillGradientBrush is RadialGradientBrush radialGradientBrush) { _fillShader = CreateRadialGradient(radialGradientBrush, _pathFillBounds); } this.Paint.SetShader(_fillShader); } else { AColor fillColor = Color.Default.ToAndroid(); if (_fill is SolidColorBrush solidColorBrush && solidColorBrush.Color != Color.Default) { fillColor = solidColorBrush.Color.ToAndroid(); } this.Paint.Color = fillColor; } base.Draw(canvas); this.Paint.SetShader(null); } if (_stroke != null && _strokeWidth > 0) { this.Paint.SetStyle(Paint.Style.Stroke); if (_stroke is GradientBrush strokeGradientBrush) { if (strokeGradientBrush is LinearGradientBrush linearGradientBrush) { _strokeShader = CreateLinearGradient(linearGradientBrush, _pathStrokeBounds); } if (strokeGradientBrush is RadialGradientBrush radialGradientBrush) { _strokeShader = CreateRadialGradient(radialGradientBrush, _pathStrokeBounds); } this.Paint.SetShader(_strokeShader); } else { AColor strokeColor = Color.Default.ToAndroid(); if (_stroke is SolidColorBrush solidColorBrush && solidColorBrush.Color != Color.Default) { strokeColor = solidColorBrush.Color.ToAndroid(); } this.Paint.Color = strokeColor; } base.Draw(canvas); this.Paint.SetShader(null); } AMatrix inverseTransformMatrix = new AMatrix(); transformMatrix.Invert(inverseTransformMatrix); _path.Transform(inverseTransformMatrix); inverseTransformMatrix.MapRect(_pathFillBounds); inverseTransformMatrix.MapRect(_pathStrokeBounds); }