Exemple #1
0
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
            base.OnPaintSurface(e);

            var canvas = e.Surface.Canvas;

            canvas.Clear();

            int width  = e.Info.Width;
            int height = e.Info.Height;

            SKPaint backPaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = SKColors.WhiteSmoke,
            };

            canvas.DrawRect(new SKRect(0, 0, width, height), backPaint);

            canvas.Save();

            canvas.Translate(width / 2, height / 2);
            canvas.Scale(Math.Min(width / 210f, height / 520f));
            SKPoint center = new SKPoint(0, 0);

            var rect = new SKRect(-100, -100, 100, 100);

            // Add a buffer for the rectangle
            rect.Inflate(-10, -10);


            SKPaint GaugePointPaint = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = ValueColor.ToSKColor()
            };

            SKPaint HighlightRangePaint = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = RangeColor.ToSKColor()
            };


            // Draw the range of values

            var rangeStartAngle = AmountToAngle(HighlightRangeStartValue);
            var rangeEndAngle   = AmountToAngle(HighlightRangeEndValue);
            var angleDistance   = rangeEndAngle - rangeStartAngle;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, rangeStartAngle, angleDistance);
                path.LineTo(center);
                canvas.DrawPath(path, HighlightRangePaint);
            }

            // Draw the main gauge line/arc
            SKPaint GaugeMainLinePaintP1 = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Blue,
                StrokeWidth = 10
            };
            var startAngle = 135;
            var sweepAngle = 67.5f;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngle, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaintP1);
            }

            //Sector2
            SKPaint GaugeMainLinePaintP2 = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Green,
                StrokeWidth = 10
            };

            var startAngleP2 = 202.5f;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngleP2, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaintP2);
            }

            //Sector3
            SKPaint GaugeMainLinePaintP3 = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Orange,
                StrokeWidth = 10
            };

            var startAngleP3 = 270f;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngleP3, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaintP3);
            }

            //Sector 4

            SKPaint GaugeMainLinePaintP4 = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Red,
                StrokeWidth = 10
            };

            var startAngleP4 = 337.5f;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngleP4, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaintP4);
            }

            //Draw Needle
            DrawNeedle(canvas, Value);

            //Draw Screw
            SKPaint NeedleScrewPaint = new SKPaint()
            {
                IsAntialias = true,
                Shader      = SKShader.CreateRadialGradient(center, width / 60, new SKColor[]
                                                            { new SKColor(171, 171, 171), SKColors.White }, new float[] { 0.05f, 0.9f }, SKShaderTileMode.Mirror)
            };

            canvas.DrawCircle(center, width / 60, NeedleScrewPaint);

            SKPaint paint = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = new SKColor(81, 84, 89).WithAlpha(100),
                StrokeWidth = 1f
            };

            canvas.DrawCircle(center, width / 60, paint);

            // Draw the Units of Measurement Text on the display
            SKPaint textPaint = new SKPaint
            {
                IsAntialias = true,
                Color       = SKColors.Black
            };

            float textWidth = textPaint.MeasureText(UnitsText);

            textPaint.TextSize = 12f;

            SKRect textBounds = SKRect.Empty;

            textPaint.MeasureText(UnitsText, ref textBounds);

            float xText = -1 * textBounds.MidX;
            float yText = 95 - textBounds.Height;

            // And draw the text
            canvas.DrawText(UnitsText, xText, yText, textPaint);

            // Draw the Value on the display
            var   valueText      = Value.ToString("F0"); //You can set F1 or F2 if you need float values
            float valueTextWidth = textPaint.MeasureText(valueText);

            textPaint.TextSize = ValueFontSize;

            textPaint.MeasureText(valueText, ref textBounds);

            xText = -1 * textBounds.MidX;
            yText = 85 - textBounds.Height;

            // And draw the text
            canvas.DrawText(valueText, xText, yText, textPaint);
            canvas.Restore();
        }
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
            base.OnPaintSurface(e);

            var canvas = e.Surface.Canvas;

            SKPaint backPaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = Color.WhiteSmoke.ToSKColor(),
            };

            int width  = e.Info.Width;
            int height = e.Info.Height;

            canvas.DrawRect(new SKRect(0, 0, width, height), backPaint);

            canvas.Save();

            canvas.Translate(width / 2, height / 2);
            canvas.Scale(Math.Min(width / 210f, height / 520f));

            SKPaint GaugeMainLinePaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                Color       = GaugeLineColor.ToSKColor(),
                StrokeWidth = 10
            };

            SKPaint GaugePointPaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = ValueColor.ToSKColor()
            };

            SKPaint HighlightRangePaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = RangeColor.ToSKColor()
            };

            SKPoint center = new SKPoint(0, 0);

            var rect = new SKRect(-100, -100, 100, 100);

            // Add a buffer for the rectangle
            rect.Inflate(-10, -10);

            // Draw the range of values
            if (RangeIsVisible)
            {
                var rangeStartAngle = AmountToAngle(HighlightRangeStartValue);
                var rangeEndAngle   = AmountToAngle(HighlightRangeEndValue);
                var angleDistance   = rangeEndAngle - rangeStartAngle;

                using (SKPath path = new SKPath())
                {
                    path.AddArc(rect, rangeStartAngle, angleDistance);
                    path.LineTo(center);
                    canvas.DrawPath(path, HighlightRangePaint);
                }
            }

            // Draw the main gauge line/arc
            var startAngle = 135;
            var sweepAngle = 270;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngle, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaint);
            }

            // Draw the current value on the arc
            var valueAsAngle = AmountToAngle(Value);

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, valueAsAngle, 2);
                canvas.DrawCircle(path.Bounds.Left + path.Bounds.Width / 2,
                                  path.Bounds.Top + path.Bounds.Height / 2,
                                  10f,
                                  GaugePointPaint);
            }

            // Draw the Units of Measurement Text on the display
            SKPaint textPaint = new SKPaint
            {
                Color = SKColors.Black
            };

            float textWidth = textPaint.MeasureText(UnitsText);

            textPaint.TextSize = 12f;

            SKRect textBounds = SKRect.Empty;

            textPaint.MeasureText(UnitsText, ref textBounds);

            float xText = -1 * textBounds.MidX;
            float yText = 95 - textBounds.Height;

            // And draw the text
            canvas.DrawText(UnitsText, xText, yText, textPaint);

            // Draw the Value on the display
            var   valueText      = Value.ToString("F1");
            float valueTextWidth = textPaint.MeasureText(valueText);

            textPaint.TextSize = 35f;

            textPaint.MeasureText(valueText, ref textBounds);

            xText = -1 * textBounds.MidX;
            yText = 85 - textBounds.Height;

            // And draw the text
            canvas.DrawText(valueText, xText, yText, textPaint);

            canvas.Restore();
        }