Exemplo n.º 1
0
        public void DrawImage(IImage image, float x, float y, float width, float height, Xamarin.Forms.Aspect aspect)
        {
            if (image != null)
            {
                CGImage cgImage = ((Image)image).NativeImage;
                if (cgImage != null)
                {
                    if (aspect == Xamarin.Forms.Aspect.Fill)
                    {
                        context.DrawImage(new CGRect(x, y, width, height), cgImage);
                    }
                    else
                    {
                        Xamarin.Forms.Size bounding = new Xamarin.Forms.Size(width, height);

                        if (aspect == Xamarin.Forms.Aspect.AspectFill)
                        {
                            Xamarin.Forms.Rectangle imageBox = MathHelper.Fill(new Xamarin.Forms.Rectangle(Xamarin.Forms.Point.Zero, image.Size), bounding);

                            using (CGImage newImage = cgImage.WithImageInRect(new CGRect(imageBox.X, imageBox.Y, imageBox.Width, imageBox.Height)))
                                context.DrawImage(new CGRect(x, y, width, height), newImage);
                        }
                        else
                        {
                            Xamarin.Forms.Rectangle boundingBox = MathHelper.Fit(image.Size, new Xamarin.Forms.Rectangle(x, y, width, height));

                            context.DrawImage(new CGRect(boundingBox.X, boundingBox.Y, boundingBox.Width, boundingBox.Height), cgImage);
                        }
                    }
                }
                context.DrawImage(new CGRect(x, y, width, height), cgImage);
            }
        }
Exemplo n.º 2
0
        public void DrawImage(IImage image, float x, float y, float width, float height, Xamarin.Forms.Aspect aspect)
        {
            if (image != null)
            {
                Bitmap bitmap = ((Image)image).NativeImage;
                if (bitmap != null)
                {
                    if (aspect == Xamarin.Forms.Aspect.Fill)
                    {
                        using (RectF rect = new RectF(x, y, x + width - 1, y + height - 1))
                            canvas.DrawBitmap(bitmap, null, rect, null);
                    }
                    else
                    {
                        Xamarin.Forms.Size bounding = new Xamarin.Forms.Size(width, height);

                        if (aspect == Xamarin.Forms.Aspect.AspectFill)
                        {
                            Xamarin.Forms.Rectangle imageBox = MathHelper.Fill(new Xamarin.Forms.Rectangle(Xamarin.Forms.Point.Zero, image.Size), bounding);
                            using (RectF rectB = new RectF(x, y, x + width - 1, y + height - 1))
                                using (Rect rectI = new Rect((int)imageBox.X, (int)imageBox.Y, (int)imageBox.X + (int)imageBox.Width - 1, (int)imageBox.Y + (int)imageBox.Height - 1))
                                    canvas.DrawBitmap(bitmap, rectI, rectB, null);
                        }
                        else
                        {
                            Xamarin.Forms.Rectangle boundingBox = MathHelper.Fit(image.Size, new Xamarin.Forms.Rectangle(x, y, width, height));
                            using (RectF rect = new RectF((float)boundingBox.X, (float)boundingBox.Y, (float)boundingBox.X + (float)boundingBox.Width - 1, (float)boundingBox.Y + (float)boundingBox.Height - 1))
                                canvas.DrawBitmap(bitmap, null, rect, null);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 public void SetRectangle(Xamarin.Forms.Rectangle Rect)
 {
     _x      = Rect.X;
     _y      = Rect.Y;
     _width  = Rect.Width;
     _height = Rect.Height;
 }
Exemplo n.º 4
0
        public void DrawImage(IImage image, float x, float y, float width, float height, Xamarin.Forms.Aspect aspect)
        {
            if (image != null)
            {
                CanvasBitmap bitmap = ((Image)image).NativeBitmap;
                if (bitmap != null)
                {
                    if (aspect == Xamarin.Forms.Aspect.Fill)
                    {
                        ds.DrawImage(bitmap, new Rect(x, y, width, height));
                    }
                    else
                    {
                        Xamarin.Forms.Size bounding = new Xamarin.Forms.Size(width, height);

                        if (aspect == Xamarin.Forms.Aspect.AspectFill)
                        {
                            Xamarin.Forms.Rectangle imageBox = MathHelper.Fill(new Xamarin.Forms.Rectangle(Xamarin.Forms.Point.Zero, image.Size), bounding);

                            ds.DrawImage(bitmap, new Rect(x, y, width, height), new Rect(imageBox.X, imageBox.Y, imageBox.Width, imageBox.Height));
                        }
                        else
                        {
                            Xamarin.Forms.Rectangle boundingBox = MathHelper.Fit(image.Size, new Xamarin.Forms.Rectangle(x, y, width, height));

                            ds.DrawImage(bitmap, new Rect(boundingBox.X, boundingBox.Y, boundingBox.Width, boundingBox.Height));
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        internal static void TransformRect(ref Xamarin.Forms.Rectangle rect, ref Matrix matrix)
        {
            if (rect.IsEmpty)
            {
                return;
            }

            MatrixTypes matrixType = matrix._type;

            if (matrixType == MatrixTypes.Identity)
            {
                return;
            }

            // Scaling
            if (0 != (matrixType & MatrixTypes.Scaling))
            {
                rect.X      *= matrix._m11;
                rect.Y      *= matrix._m22;
                rect.Width  *= matrix._m11;
                rect.Height *= matrix._m22;

                if (rect.Width < 0.0)
                {
                    rect.X    += rect.Width;
                    rect.Width = -rect.Width;
                }

                if (rect.Height < 0.0)
                {
                    rect.Y     += rect.Height;
                    rect.Height = -rect.Height;
                }
            }

            // Translation
            if (0 != (matrixType & MatrixTypes.Translation))
            {
                // X
                rect.X += matrix._offsetX;

                // Y
                rect.X += matrix._offsetY;
            }

            if (matrixType == MatrixTypes.Unknown)
            {
                Point point0 = matrix.Transform(new Point(rect.Right, rect.Top));
                Point point1 = matrix.Transform(new Point(rect.Right, rect.Top));
                Point point2 = matrix.Transform(new Point(rect.Right, rect.Bottom));
                Point point3 = matrix.Transform(new Point(rect.Left, rect.Bottom));

                rect.X = Math.Min(Math.Min(point0.X, point1.X), Math.Min(point2.X, point3.X));
                rect.Y = Math.Min(Math.Min(point0.Y, point1.Y), Math.Min(point2.Y, point3.Y));

                rect.Width  = Math.Max(Math.Max(point0.X, point1.X), Math.Max(point2.X, point3.X)) - rect.X;
                rect.Height = Math.Max(Math.Max(point0.Y, point1.Y), Math.Max(point2.Y, point3.Y)) - rect.Y;
            }
        }
Exemplo n.º 6
0
        public void Render(IGraphicProvider gfx)
        {
            Xamarin.Forms.Point     ul     = m_Mapper.GetGraphicsPoint(m_Top.Start);
            Xamarin.Forms.Point     lr     = m_Mapper.GetGraphicsPoint(m_Right.End);
            Xamarin.Forms.Rectangle bounds = new Xamarin.Forms.Rectangle(ul, new Xamarin.Forms.Size(lr.X - ul.X, lr.Y - ul.Y));

            //gfx.DrawString(ul, new Font(FontFamily.GenericSansSerif, 10), m_Player.Color, m_Player.Initials);
        }
Exemplo n.º 7
0
 public static RectF ToRectF(this Xamarin.Forms.Rectangle rectangle)
 {
     return(new RectF(
                (float)rectangle.Left,
                (float)rectangle.Top,
                (float)rectangle.Right,
                (float)rectangle.Bottom
                ));
 }
Exemplo n.º 8
0
 public static CGRect ToRect(this Xamarin.Forms.Rectangle rectangle)
 {
     return(CGRect.FromLTRB(
                (nfloat)rectangle.Left,
                (nfloat)rectangle.Top,
                (nfloat)rectangle.Right,
                (nfloat)rectangle.Bottom
                ));
 }
Exemplo n.º 9
0
        private bool IsViewInBounds(View view, int x, int y)
        {
            view.GetDrawingRect(_rect);
            view.GetLocationOnScreen(_location);

            var  rect = new Xamarin.Forms.Rectangle(_location[0], _location[1], _rect.Width(), _rect.Height());
            bool res  = rect.Contains(x, y);

            return(res);
        }
Exemplo n.º 10
0
        // Code taken from
        // http://www.michaelridland.com/xamarin/creating-native-view-xamarin-forms-viewpage/
        public static ViewGroup ConvertFormsToNative(Xamarin.Forms.View view, Xamarin.Forms.Rectangle size)
        {
            var renderer  = Platform.CreateRenderer(view);
            var viewGroup = renderer.ViewGroup;

            renderer.Tracker.UpdateLayout();
            var layoutParams = new ViewGroup.LayoutParams((int)size.Width, (int)size.Height);

            viewGroup.LayoutParameters = layoutParams;
            view.Layout(size);
            viewGroup.Layout(0, 0, (int)view.WidthRequest, (int)view.HeightRequest);
            return(viewGroup);
        }
Exemplo n.º 11
0
        static SKPath MakePath(RectangleGeometry rectangleGeometry)
        {
            var            path = new SKPath();
            FormsRectangle rect = rectangleGeometry.Rect;

            path.AddRect(new SKRect(
                             Forms.ConvertToScaledPixel(rect.Left),
                             Forms.ConvertToScaledPixel(rect.Top),
                             Forms.ConvertToScaledPixel(rect.Right),
                             Forms.ConvertToScaledPixel(rect.Bottom)),
                         SKPathDirection.Clockwise);

            return(path);
        }
Exemplo n.º 12
0
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            if (CrossPlatformArrange == null || Context == null)
            {
                return;
            }

            var deviceIndependentLeft   = Context.FromPixels(l);
            var deviceIndependentTop    = Context.FromPixels(t);
            var deviceIndependentRight  = Context.FromPixels(r);
            var deviceIndependentBottom = Context.FromPixels(b);

            var destination = Rectangle.FromLTRB(deviceIndependentLeft, deviceIndependentTop,
                                                 deviceIndependentRight, deviceIndependentBottom);

            CrossPlatformArrange(destination);
        }
Exemplo n.º 13
0
        private View ConvertFormsToNative(Xamarin.Forms.View view, Xamarin.Forms.Rectangle size)
        {
            viewRenderer = Platform.CreateRendererWithContext(view, Context);
            var viewGroup = viewRenderer.View;

            viewRenderer.Tracker.UpdateLayout();

            if (view.HeightRequest > 0)
            {
                size.Height = view.HeightRequest;
            }

            var layoutParams = new ViewGroup.LayoutParams((int)(size.Width * Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Density), (int)(size.Height * Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Density));

            viewGroup.LayoutParameters = layoutParams;
            view.Layout(size);
            viewGroup.Layout(0, 0, (int)view.Width, (int)view.Height);
            return(viewGroup);
        }
Exemplo n.º 14
0
 public WebViewToPngSize(int width, Xamarin.Forms.Rectangle bounds = default)
 {
     Bounds = bounds;
 }
Exemplo n.º 15
0
        public static PathData ToCGPath(this Geometry geometry)
        {
            PathData pathData = new PathData
            {
                Data = new CGPath()
            };

            CGAffineTransform transform = CGAffineTransform.MakeIdentity();

            if (geometry is LineGeometry)
            {
                LineGeometry lineGeometry = geometry as LineGeometry;
                pathData.Data.MoveToPoint(transform, lineGeometry.StartPoint.ToPointF());
                pathData.Data.AddLineToPoint(transform, lineGeometry.EndPoint.ToPointF());
            }
            else if (geometry is RectangleGeometry)
            {
                Rect rect = (geometry as RectangleGeometry).Rect;
                pathData.Data.AddRect(transform, new CGRect(rect.X, rect.Y, rect.Width, rect.Height));
            }
            else if (geometry is EllipseGeometry)
            {
                EllipseGeometry ellipseGeometry = geometry as EllipseGeometry;

                CGRect rect = new CGRect(
                    ellipseGeometry.Center.X - ellipseGeometry.RadiusX,
                    ellipseGeometry.Center.Y - ellipseGeometry.RadiusY,
                    ellipseGeometry.RadiusX * 2,
                    ellipseGeometry.RadiusY * 2);

                pathData.Data.AddEllipseInRect(transform, rect);
            }
            else if (geometry is GeometryGroup)
            {
                GeometryGroup geometryGroup = geometry as GeometryGroup;

                pathData.IsNonzeroFillRule = geometryGroup.FillRule == FillRule.Nonzero;

                foreach (Geometry child in geometryGroup.Children)
                {
                    PathData pathChild = child.ToCGPath();
                    pathData.Data.AddPath(pathChild.Data);
                }
            }
            else if (geometry is PathGeometry)
            {
                PathGeometry pathGeometry = geometry as PathGeometry;

                pathData.IsNonzeroFillRule = pathGeometry.FillRule == FillRule.Nonzero;

                foreach (PathFigure pathFigure in pathGeometry.Figures)
                {
                    pathData.Data.MoveToPoint(transform, pathFigure.StartPoint.ToPointF());
                    Point lastPoint = pathFigure.StartPoint;

                    foreach (PathSegment pathSegment in pathFigure.Segments)
                    {
                        // LineSegment
                        if (pathSegment is LineSegment)
                        {
                            LineSegment lineSegment = pathSegment as LineSegment;

                            pathData.Data.AddLineToPoint(transform, lineSegment.Point.ToPointF());
                            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++)
                            {
                                pathData.Data.AddLineToPoint(transform, points[i].ToPointF());
                            }

                            lastPoint = points[points.Count - 1];
                        }

                        // BezierSegment
                        if (pathSegment is BezierSegment)
                        {
                            BezierSegment bezierSegment = pathSegment as BezierSegment;

                            pathData.Data.AddCurveToPoint(
                                transform,
                                bezierSegment.Point1.ToPointF(),
                                bezierSegment.Point2.ToPointF(),
                                bezierSegment.Point3.ToPointF());

                            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)
                            {
                                pathData.Data.AddCurveToPoint(
                                    transform,
                                    points[i].ToPointF(),
                                    points[i + 1].ToPointF(),
                                    points[i + 2].ToPointF());
                            }
                            lastPoint = points[points.Count - 1];
                        }

                        // QuadraticBezierSegment
                        if (pathSegment is QuadraticBezierSegment)
                        {
                            QuadraticBezierSegment bezierSegment = pathSegment as QuadraticBezierSegment;

                            pathData.Data.AddQuadCurveToPoint(
                                transform,
                                new nfloat(bezierSegment.Point1.X),
                                new nfloat(bezierSegment.Point1.Y),
                                new nfloat(bezierSegment.Point2.X),
                                new nfloat(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)
                            {
                                pathData.Data.AddQuadCurveToPoint(
                                    transform,
                                    new nfloat(points[i + 0].X),
                                    new nfloat(points[i + 0].Y),
                                    new nfloat(points[i + 1].X),
                                    new nfloat(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);

                            CGPoint[] cgpoints = new CGPoint[points.Count];

                            for (int i = 0; i < points.Count; i++)
                            {
                                cgpoints[i] = transform.TransformPoint(points[i].ToPointF());
                            }

                            pathData.Data.AddLines(cgpoints);

                            lastPoint = points[points.Count - 1];
                        }
                    }

                    if (pathFigure.IsClosed)
                    {
                        pathData.Data.CloseSubpath();
                    }
                }
            }

            return(pathData);
        }
Exemplo n.º 16
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);
        }
Exemplo n.º 17
0
 public static Rectangle ToLiteRectangle(this Xamarin.Forms.Rectangle rectangle)
 {
     return(new Rectangle((float)rectangle.X, (float)rectangle.Y, (float)rectangle.Width, (float)rectangle.Height));
 }
Exemplo n.º 18
0
 public Rect(Xamarin.Forms.Rectangle rect)
 {
     SetRectangle(rect);
 }
Exemplo n.º 19
0
 public static RectangleGeometry Rect(this RectangleGeometry rect, Xamarin.Forms.Rectangle rectangle)
 {
     rect.Rect = rectangle;
     return(rect);
 }
Exemplo n.º 20
0
 public static Windows.Foundation.Rect ToWinRect(this Xamarin.Forms.Rectangle rectangle) => new Windows.Foundation.Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
Exemplo n.º 21
0
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);

            if (mViewPager == null)
            {
                return;
            }
            int count = mViewPager.Adapter.Count;

            if (count == 0)
            {
                return;
            }

            if (mCurrentPage >= count)
            {
                SetCurrentItem(count - 1);
                return;
            }

            int longSize;
            int longPaddingBefore;
            int longPaddingAfter;
            int shortPaddingBefore;

            if (mOrientation == HORIZONTAL)
            {
                longSize           = Width;
                longPaddingBefore  = PaddingLeft;
                longPaddingAfter   = PaddingRight;
                shortPaddingBefore = PaddingTop;
            }
            else
            {
                longSize           = Height;
                longPaddingBefore  = PaddingTop;
                longPaddingAfter   = PaddingBottom;
                shortPaddingBefore = PaddingLeft;
            }

            float threeRadius = mRadius * 4;             // dots separation
            float shortOffset = shortPaddingBefore + mRadius;
            float longOffset  = longPaddingBefore + mRadius;

            if (mCentered)
            {
                longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f);
            }

            float dX;
            float dY;

            float pageFillRadius = mRadius;

            if (mPaintStroke.StrokeWidth > 0)
            {
                pageFillRadius -= mPaintStroke.StrokeWidth / 2.0f;
            }

            //Draw stroked circles
            for (int iLoop = 0; iLoop < count; iLoop++)
            {
                float drawLong = longOffset + (iLoop * threeRadius);
                if (mOrientation == HORIZONTAL)
                {
                    dX = drawLong;
                    dY = shortOffset;
                }
                else
                {
                    dX = shortOffset;
                    dY = drawLong;
                }

                // Only paint fill if not completely transparent
                if (mPaintPageFill.Alpha > 0)
                {
                    switch (indicatorsStyle)
                    {
                    case IndicatorsShape.Square:
                        var rect1        = new Xamarin.Forms.Rectangle((int)dX, (int)dY, (int)pageFillRadius * 2, (int)pageFillRadius * 2);
                        var rect1_native = new Rect((int)rect1.Left, (int)rect1.Top, (int)rect1.Right, (int)rect1.Bottom);
                        canvas.DrawRect(rect1_native, mPaintPageFill);
                        break;

                    default:
                        canvas.DrawCircle(dX, dY, pageFillRadius, mPaintPageFill);
                        break;
                    }
                }

                // Only paint stroke if a stroke width was non-zero
                if (pageFillRadius != mRadius)
                {
                    switch (indicatorsStyle)
                    {
                    case IndicatorsShape.Square:
                        var rect2        = new Xamarin.Forms.Rectangle((int)dX, (int)dY, (int)mRadius * 2, (int)mRadius * 2);
                        var rect2_native = new Rect((int)rect2.Left, (int)rect2.Top, (int)rect2.Right, (int)rect2.Bottom);
                        canvas.DrawRect(rect2_native, mPaintPageFill);
                        break;

                    default:
                        canvas.DrawCircle(dX, dY, mRadius, mPaintStroke);
                        break;
                    }
                }
            }

            //Draw the filled circle according to the current scroll
            float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;

            if (!mSnap && (mPageSize != 0))
            {
                cx += (mCurrentOffset * 1.0f / mPageSize) * threeRadius;
            }
            if (mOrientation == HORIZONTAL)
            {
                dX = longOffset + cx;
                dY = shortOffset;
            }
            else
            {
                dX = shortOffset;
                dY = longOffset + cx;
            }

            switch (indicatorsStyle)
            {
            case IndicatorsShape.Square:
                var rect3        = new Xamarin.Forms.Rectangle((int)dX, (int)dY, (int)mRadius * 2, (int)mRadius * 2);
                var rect3_native = new Rect((int)rect3.Left, (int)rect3.Top, (int)rect3.Right, (int)rect3.Bottom);
                canvas.DrawRect(rect3_native, mPaintFill);
                break;

            default:
                canvas.DrawCircle(dX, dY, mRadius, mPaintFill);
                break;
            }
        }