示例#1
0
        /// <summary>
        /// Paint swich
        /// </summary>
        protected override void OnPaintSelectionElement(SKPaintSurfaceEventArgs e)
        {
            float skTrackCornerRadius    = (float)(_visualSettings.TrackCornerRadius * DeviceScale);
            float skTrackBorderThickness = (float)(_visualSettings.TrackBorderThickness * DeviceScale);
            float skActualTrackHeight    = (float)(_visualSettings.TrackHeightRequest * DeviceScale);
            float skActualTrackWidth     = (float)(_visualSettings.TrackWidthRequest * DeviceScale);

            float skThumbCornerRadius    = (float)(_visualSettings.ThumbCornerRadius * DeviceScale);
            float skThumbBorderThickness = (float)(_visualSettings.ThumbBorderThickness * DeviceScale);
            float skActualThumbHeight    = (float)(_visualSettings.ThumbHeightRequest * DeviceScale);
            float skActualThumbWidth     = (float)(_visualSettings.ThumbWidthRequest * DeviceScale);

            float skHorizontalTextMargin = string.IsNullOrEmpty(Text) == false ? (float)(TextMargin.HorizontalThickness * DeviceScale) : 0;

            float skNegativePadding = (float)NegativePadding * DeviceScale;
            float skWidth           = (float)e.Info.Width;
            float skHeight          = (float)e.Info.Height;

            //
            // Track location
            //

            float skActualTrackCornerRadius = Math.Min(skTrackCornerRadius, Math.Min(skActualTrackHeight, skActualTrackWidth));

            float skStartX = 0;

            if (SwitchLocation == HorizontalLocations.Right)
            {
                if (HorizontalOptions.Alignment == LayoutAlignment.Fill)
                {
                    skStartX = skWidth - skNegativePadding - skActualTrackWidth - (float)((Padding.Right + _visualSettings.ThumbHorizontalNegativeRightPadding) * DeviceScale);
                }
                else
                {
                    skStartX = (float)((_textSize.Width + _visualSettings.ThumbHorizontalNegativeLeftPadding) * DeviceScale) + skHorizontalTextMargin;
                }
            }
            else
            {
                skStartX = skNegativePadding + (float)(Padding.Right + _visualSettings.ThumbHorizontalNegativeRightPadding) * DeviceScale;
            }

            SKRect trackLocation = new SKRect();

            trackLocation.Left   = skStartX;
            trackLocation.Top    = (skHeight - skActualTrackHeight) / 2;
            trackLocation.Right  = trackLocation.Left + skActualTrackWidth;
            trackLocation.Bottom = trackLocation.Top + skActualTrackHeight;

            float skActualThumbCornerRadius = Math.Min(skThumbCornerRadius, Math.Min(skActualThumbHeight / 2, skActualThumbWidth / 2));

            //
            // Thumb location
            //

            SKRect thumbLocation = new SKRect();

            thumbLocation.Left   = trackLocation.Left + (float)(_visualSettings.ThumbPadding.Left * DeviceScale) + (float)_toggledAnimationProcess * (skActualTrackWidth - skActualThumbWidth - (float)(_visualSettings.ThumbPadding.HorizontalThickness * DeviceScale));
            thumbLocation.Top    = (skHeight - skActualThumbHeight + (float)(_visualSettings.ThumbPadding.VerticalThickness * DeviceScale)) / 2;
            thumbLocation.Right  = thumbLocation.Left + skActualThumbWidth;
            thumbLocation.Bottom = thumbLocation.Top + skActualThumbHeight;

            //
            // Draw Thumb ellipse
            //

            if (EllipseDiameter > 0 && _toggledAnimationProcess > 0 && _toggledAnimationProcess < 1 && IsEllipseAnimationEnabled)
            {
                SKPaint ellipsePaint = new SKPaint()
                {
                    IsAntialias = true,
                    Style       = SKPaintStyle.Fill,
                };

                if (_toggledAnimationProcess <= 0.5)
                {
                    ellipsePaint.Color = EllipseColor.MultiplyAlpha(_toggledAnimationProcessWithoutEasing * 2).ToSKColor();
                }
                else
                {
                    ellipsePaint.Color = EllipseColor.MultiplyAlpha(1 - (_toggledAnimationProcessWithoutEasing - 0.5) * 2).ToSKColor();
                }

                e.Surface.Canvas.DrawCircle(new SKPoint(thumbLocation.MidX, thumbLocation.MidY), (float)(EllipseDiameter / 2) * DeviceScale, ellipsePaint);
            }

            //
            // Draw track
            //

            SKPaint trackPaint = new SKPaint()
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = AnimationUtils.ColorTransform(_toggledAnimationProcess, TrackUnToggledColor, TrackToggledColor).ToSKColor(),
            };

            e.Surface.Canvas.DrawRoundRect(trackLocation, skActualTrackCornerRadius, skActualTrackCornerRadius, trackPaint);

            //
            // Draw track border
            //

            if (skTrackBorderThickness > 0)
            {
                SKRect trackBorderLocation = new SKRect();
                trackBorderLocation.Left   = trackLocation.Left + skTrackBorderThickness / 2;
                trackBorderLocation.Top    = trackLocation.Top + (skTrackBorderThickness / 2);
                trackBorderLocation.Right  = trackLocation.Right - (skTrackBorderThickness / 2);
                trackBorderLocation.Bottom = trackLocation.Bottom - (skTrackBorderThickness / 2);

                SKPaint trackBorderPaint = new SKPaint()
                {
                    IsAntialias = true,
                    Style       = SKPaintStyle.Stroke,
                    Color       = AnimationUtils.ColorTransform(_toggledAnimationProcess, TrackBorderUnToggledColor, TrackBorderToggledColor).ToSKColor(),
                    StrokeWidth = skTrackBorderThickness,
                };

                e.Surface.Canvas.DrawRoundRect(trackBorderLocation, skActualTrackCornerRadius, skActualTrackCornerRadius, trackBorderPaint);
            }

            //
            // Draw thumb shadow
            //

            if (IsThumbShadowEnabled)
            {
                SKRect shadowLocation = new SKRect();
                shadowLocation.Left   = thumbLocation.Left;
                shadowLocation.Right  = thumbLocation.Right;
                shadowLocation.Top    = thumbLocation.Top + _shadowLenght;
                shadowLocation.Bottom = thumbLocation.Bottom + _shadowLenght;
                SkiaUtils.DrawShadow(e, shadowLocation, skActualThumbCornerRadius, (float)(_shadowLenght * DeviceScale), Color.Black, 0.2, true);
            }

            //
            // Draw thumb
            //

            SKPaint thumbPaint = new SKPaint()
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = AnimationUtils.ColorTransform(_toggledAnimationProcess, ThumbUnToggledColor, ThumbToggledColor).ToSKColor(),
            };

            SKRect thumbBackgroundLocation = thumbLocation;

            thumbBackgroundLocation.Inflate(-skThumbBorderThickness / 2, -skThumbBorderThickness / 2);

            e.Surface.Canvas.DrawRoundRect(thumbBackgroundLocation, skActualThumbCornerRadius, skActualThumbCornerRadius, thumbPaint);

            //
            // Draw thumb border
            //

            if (skThumbBorderThickness > 0)
            {
                SKRect thumbBorderLocation = new SKRect();
                thumbBorderLocation.Left   = thumbLocation.Left + (skThumbBorderThickness / 2);
                thumbBorderLocation.Top    = thumbLocation.Top + (skThumbBorderThickness / 2);
                thumbBorderLocation.Right  = thumbLocation.Right - (skThumbBorderThickness / 2);
                thumbBorderLocation.Bottom = thumbLocation.Bottom - (skThumbBorderThickness / 2);

                SKPaint thumbBorderPaint = new SKPaint()
                {
                    IsAntialias = true,
                    Style       = SKPaintStyle.Stroke,
                    Color       = AnimationUtils.ColorTransform(_toggledAnimationProcess, ThumbBorderUnToggledColor, ThumbBorderToggledColor).ToSKColor(),
                    StrokeWidth = skThumbBorderThickness,
                };

                e.Surface.Canvas.DrawRoundRect(thumbBorderLocation, skActualThumbCornerRadius, skActualThumbCornerRadius, thumbBorderPaint);
            }
        }
示例#2
0
        /// <summary>
        /// Paint background, border and shadow
        /// </summary>
        private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            e.Surface.Canvas.Clear();

            /*
             * if (ShadowLenght.Equals(0))
             * {
             *  return;
             * }
             */
            // Get device pixel intencity scale
            DeviceScale = (float)(e.Info.Width / _skiaCanvas.Width);

            Rectangle contentLocationRelativeToCanvas = new Rectangle(
                ShadowLenght,
                ShadowLenght,
                Width - (ShadowLenght * 2),
                Height - (ShadowLenght * 2));

            float maxCornerRadius   = (float)Math.Min(Width / 2, Height / 2) * DeviceScale;
            float skCornerRadius    = Math.Min(maxCornerRadius, (float)(CornerRadius * DeviceScale));
            float skBorderThickness = (float)BorderThickness * DeviceScale;

            float skShadowLenght = (float)Math.Round(ShadowLenght * DeviceScale, MidpointRounding.AwayFromZero);

            SKRect skBackgroundDrawLocation = new SKRect(skShadowLenght, skShadowLenght, e.Info.Width - skShadowLenght, e.Info.Height - skShadowLenght);

            // Shadow

            if (ShadowLenght > 0 && ShadowColor != Color.Transparent && IsShadowEnabled)
            {
                SkiaUtils.DrawShadow(e, skBackgroundDrawLocation, skCornerRadius, skShadowLenght, ShadowColor, ShadowOpacity, false);
            }

            // Background

            if (BorderBackgroundColor != Color.Transparent)
            {
                if (_backgroundPaint == null)
                {
                    _backgroundPaint             = new SKPaint();
                    _backgroundPaint.IsAntialias = true;
                    _backgroundPaint.Style       = SKPaintStyle.Fill;
                    _backgroundPaint.Color       = BorderBackgroundColor.ToSKColor();
                }

                SKRect backgroundRect = skBackgroundDrawLocation;

                if (BorderThickness > 0)
                {
                    backgroundRect.Inflate(-skBorderThickness, -skBorderThickness);
                }

                SKRoundRect backgroundRoundedRect = new SKRoundRect(backgroundRect, skCornerRadius, skCornerRadius);
                e.Surface.Canvas.DrawRoundRect(backgroundRoundedRect, _backgroundPaint);
            }

            // Border

            if (BorderThickness > 0 && BorderColor != Color.Transparent)
            {
                if (_borderPaint == null)
                {
                    _borderPaint             = new SKPaint();
                    _borderPaint.Color       = BorderColor.ToSKColor();
                    _borderPaint.IsAntialias = true;
                    _borderPaint.Style       = SKPaintStyle.Stroke;
                    _borderPaint.StrokeWidth = skBorderThickness;
                }

                SKRect borderRect = skBackgroundDrawLocation;
                borderRect.Inflate(-skBorderThickness / 2, -skBorderThickness / 2);

                SKRoundRect borderRoundedRect = new SKRoundRect(borderRect, skCornerRadius, skCornerRadius);

                e.Surface.Canvas.DrawRoundRect(borderRoundedRect, _borderPaint);
            }
        }