Beispiel #1
0
        internal static Android.Graphics.Path ToPath(this Windows.Foundation.Rect rect)
        {
            var path = new Android.Graphics.Path();

            path.AddRect(rect.ToRectF(), Path.Direction.Cw);

            return(path);
        }
Beispiel #2
0
        public static APath ToAPath(this Geometry geometry, Context context)
        {
            APath path = new APath();

            float density = context.Resources.DisplayMetrics.Density;

            if (geometry is LineGeometry)
            {
                LineGeometry lineGeometry = geometry as LineGeometry;

                path.MoveTo(
                    density * (float)lineGeometry.StartPoint.X,
                    density * (float)lineGeometry.StartPoint.Y);

                path.LineTo(
                    density * (float)lineGeometry.EndPoint.X,
                    density * (float)lineGeometry.EndPoint.Y);
            }
            else if (geometry is RectangleGeometry)
            {
                FormsRectangle rect = (geometry as RectangleGeometry).Rect;

                path.AddRect(
                    density * (float)rect.Left,
                    density * (float)rect.Top,
                    density * (float)rect.Right,
                    density * (float)rect.Bottom,
                    APath.Direction.Cw);
            }
            else if (geometry is EllipseGeometry)
            {
                EllipseGeometry ellipseGeometry = geometry as EllipseGeometry;

                path.AddOval(new RectF(
                                 density * (float)(ellipseGeometry.Center.X - ellipseGeometry.RadiusX),
                                 density * (float)(ellipseGeometry.Center.Y - ellipseGeometry.RadiusY),
                                 density * (float)(ellipseGeometry.Center.X + ellipseGeometry.RadiusX),
                                 density * (float)(ellipseGeometry.Center.Y + ellipseGeometry.RadiusY)),
                             APath.Direction.Cw);
            }
            else if (geometry is GeometryGroup)
            {
                GeometryGroup geometryGroup = geometry as GeometryGroup;

                path.SetFillType(geometryGroup.FillRule == FillRule.Nonzero ? APath.FillType.Winding : APath.FillType.EvenOdd);

                foreach (Geometry child in geometryGroup.Children)
                {
                    APath childPath = child.ToAPath(context);
                    path.AddPath(childPath);
                }
            }
            else if (geometry is PathGeometry)
            {
                PathGeometry pathGeometry = geometry as PathGeometry;

                path.SetFillType(pathGeometry.FillRule == FillRule.Nonzero ? APath.FillType.Winding : APath.FillType.EvenOdd);

                foreach (PathFigure pathFigure in pathGeometry.Figures)
                {
                    path.MoveTo(
                        density * (float)pathFigure.StartPoint.X,
                        density * (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(
                                density * (float)lineSegment.Point.X,
                                density * (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(
                                    density * (float)points[i].X,
                                    density * (float)points[i].Y);
                            }
                            lastPoint = points[points.Count - 1];
                        }
                        // BezierSegment
                        else if (pathSegment is BezierSegment)
                        {
                            BezierSegment bezierSegment = pathSegment as BezierSegment;

                            path.CubicTo(
                                density * (float)bezierSegment.Point1.X, density * (float)bezierSegment.Point1.Y,
                                density * (float)bezierSegment.Point2.X, density * (float)bezierSegment.Point2.Y,
                                density * (float)bezierSegment.Point3.X, density * (float)bezierSegment.Point3.Y);

                            lastPoint = bezierSegment.Point3;
                        }
                        // PolyBezierSegment
                        else if (pathSegment is PolyBezierSegment)
                        {
                            PolyBezierSegment polyBezierSegment = pathSegment as PolyBezierSegment;
                            PointCollection   points            = polyBezierSegment.Points;

                            if (points.Count >= 3)
                            {
                                for (int i = 0; i < points.Count; i += 3)
                                {
                                    path.CubicTo(
                                        density * (float)points[i + 0].X, density * (float)points[i + 0].Y,
                                        density * (float)points[i + 1].X, density * (float)points[i + 1].Y,
                                        density * (float)points[i + 2].X, density * (float)points[i + 2].Y);
                                }
                            }

                            lastPoint = points[points.Count - 1];
                        }
                        // QuadraticBezierSegment
                        else if (pathSegment is QuadraticBezierSegment)
                        {
                            QuadraticBezierSegment bezierSegment = pathSegment as QuadraticBezierSegment;

                            path.QuadTo(
                                density * (float)bezierSegment.Point1.X, density * (float)bezierSegment.Point1.Y,
                                density * (float)bezierSegment.Point2.X, density * (float)bezierSegment.Point2.Y);

                            lastPoint = bezierSegment.Point2;
                        }
                        // PolyQuadraticBezierSegment
                        else if (pathSegment is PolyQuadraticBezierSegment)
                        {
                            PolyQuadraticBezierSegment polyBezierSegment = pathSegment as PolyQuadraticBezierSegment;
                            PointCollection            points            = polyBezierSegment.Points;

                            if (points.Count >= 2)
                            {
                                for (int i = 0; i < points.Count; i += 2)
                                {
                                    path.QuadTo(
                                        density * (float)points[i + 0].X, density * (float)points[i + 0].Y,
                                        density * (float)points[i + 1].X, density * (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>();

                            GeometryHelper.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(
                                    density * (float)points[i].X,
                                    density * (float)points[i].Y);
                            }

                            if (points.Count > 0)
                            {
                                lastPoint = points[points.Count - 1];
                            }
                        }
                    }

                    if (pathFigure.IsClosed)
                    {
                        path.Close();
                    }
                }
            }

            return(path);
        }
        public void Draw(Canvas canvas)
        {
            if (Hidden)
            {
                return;
            }

            canvas.Save();

            if (!Focused)
            {
                outlinePaint.Color = Color.White;
                canvas.DrawRect(DrawRect, outlinePaint);
            }
            else
            {
                Rect viewDrawingRect = new Rect();
                context.GetDrawingRect(viewDrawingRect);

                outlinePaint.Color = Color.White;// new Color(0XFF, 0xFF, 0x8A, 0x00);
                focusPaint.Color = new Color(50, 50, 50, 125);

                Path path = new Path();
                path.AddRect(new RectF(DrawRect), Path.Direction.Cw);

                canvas.ClipPath(path, Region.Op.Difference);
                canvas.DrawRect(viewDrawingRect, focusPaint);

                canvas.Restore();
                canvas.DrawPath(path, outlinePaint);

                if (mode == ModifyMode.Grow)
                {
                    int left = DrawRect.Left + 1;
                    int right = DrawRect.Right + 1;
                    int top = DrawRect.Top + 4;
                    int bottom = DrawRect.Bottom + 3;

                    int widthWidth = resizeDrawableWidth.IntrinsicWidth / 2;
                    int widthHeight = resizeDrawableWidth.IntrinsicHeight / 2;
                    int heightHeight = resizeDrawableHeight.IntrinsicHeight / 2;
                    int heightWidth = resizeDrawableHeight.IntrinsicWidth / 2;

                    int xMiddle = DrawRect.Left + ((DrawRect.Right - DrawRect.Left) / 2);
                    int yMiddle = DrawRect.Top + ((DrawRect.Bottom - DrawRect.Top) / 2);

                    resizeDrawableWidth.SetBounds(left - widthWidth,
                                                   yMiddle - widthHeight,
                                                   left + widthWidth,
                                                   yMiddle + widthHeight);
                    resizeDrawableWidth.Draw(canvas);

                    resizeDrawableWidth.SetBounds(right - widthWidth,
                                                   yMiddle - widthHeight,
                                                   right + widthWidth,
                                                   yMiddle + widthHeight);
                    resizeDrawableWidth.Draw(canvas);

                    resizeDrawableHeight.SetBounds(xMiddle - heightWidth,
                                                    top - heightHeight,
                                                    xMiddle + heightWidth,
                                                    top + heightHeight);
                    resizeDrawableHeight.Draw(canvas);

                    resizeDrawableHeight.SetBounds(xMiddle - heightWidth,
                                                    bottom - heightHeight,
                                                    xMiddle + heightWidth,
                                                    bottom + heightHeight);
                    resizeDrawableHeight.Draw(canvas);
                }
            }
        }
 void CreatePath()
 {
     droidGraphics.Path path = new droidGraphics.Path();
     path.AddRect(new droidGraphics.RectF(0, 0, 1, 1), droidGraphics.Path.Direction.Cw);           // TODO: Is direction correct?
     Path = path;
 }
Beispiel #5
0
        internal void Draw(Canvas canvas, bool isRound)
        {
            if (Hidden)
            {
                return;
            }

            canvas.Save();
            var resizerPaint = new Paint { Color = new Color(255, 255, 255, 200) };

            //if (!Focused)
            //{
            //    outlinePaint.Color = Color.White;
            //    canvas.DrawRect(DrawRect, outlinePaint);
            //    //canvas.DrawCircle(DrawRect.CenterX(), DrawRect.CenterY(), DrawRect.Width(), resizerPaint);
            //}
            //else
            //{
            Rect viewDrawingRect = new Rect();
            context.GetDrawingRect(viewDrawingRect);

            outlinePaint.Color = Color.White;// new Color(0XFF, 0xFF, 0x8A, 0x00);
            focusPaint.Color = new Color(50, 50, 50, 125);

            Path path = new Path();

            if (isRound)
            {
                path.AddCircle(DrawRect.CenterX(), DrawRect.CenterY(), DrawRect.Width() / 2, Path.Direction.Cw);
                canvas.ClipPath(path, Region.Op.Difference);
                canvas.DrawCircle(viewDrawingRect.CenterX(), viewDrawingRect.CenterY(), viewDrawingRect.Width(), focusPaint);
            }
            else
            {
                path.AddRect(new RectF(DrawRect), Path.Direction.Cw);
                canvas.ClipPath(path, Region.Op.Difference);
                canvas.DrawRect(viewDrawingRect, focusPaint);

            }

            //canvas.ClipPath(path, Region.Op.Difference);

            //if (isRound)
            //    canvas.DrawCircle(viewDrawingRect.CenterX(), viewDrawingRect.CenterY(), viewDrawingRect.Width(), focusPaint);
            //else
            //    canvas.DrawRect(viewDrawingRect, focusPaint);

            canvas.Restore();
            canvas.DrawPath(path, outlinePaint);

            var resizerTriangle = new Path();
            var resizerOffset = 4;
            var triangleSize = resizerSize + resizerOffset;
            resizerTriangle.MoveTo(DrawRect.Right - resizerOffset, DrawRect.Bottom - triangleSize);
            resizerTriangle.LineTo(DrawRect.Right - resizerOffset, DrawRect.Bottom - resizerOffset);
            resizerTriangle.LineTo(DrawRect.Right - triangleSize, DrawRect.Bottom - resizerOffset);
            resizerPaint.SetStyle(Paint.Style.Fill);
            canvas.DrawPath(resizerTriangle, resizerPaint);

            if (isRound)
            {
                // Draw the rectangle around the round cropper                
                var roundCropperRectangle = new Path();
                roundCropperRectangle.MoveTo(DrawRect.Left, DrawRect.Top);
                roundCropperRectangle.LineTo(DrawRect.Right, DrawRect.Top);
                roundCropperRectangle.LineTo(DrawRect.Right, DrawRect.Bottom);
                roundCropperRectangle.LineTo(DrawRect.Left, DrawRect.Bottom);
                roundCropperRectangle.LineTo(DrawRect.Left, DrawRect.Top);

                resizerPaint.SetStyle(Paint.Style.Stroke);
                canvas.DrawPath(roundCropperRectangle, resizerPaint);
            }
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
 public void AddRect(float x, float y, float width, float height)
 {
     path.AddRect(x, y, width, height, Android.Graphics.Path.Direction.Cw);
 }