Beispiel #1
0
        public static void SetColorFilter(this ADrawable drawable, Graphics.Color color, FilterMode mode)
        {
            if (drawable == null)
            {
                return;
            }

            drawable.SetColorFilter(color.ToNative(), mode);
        }
Beispiel #2
0
        public static void UpdateTextColor(this TextView textView, ITextStyle textStyle, Graphics.Color defaultColor)
        {
            var textColor = textStyle.TextColor?.ToNative() ?? defaultColor?.ToNative();

            if (textColor != null)
            {
                textView.SetTextColor(textColor.Value);
            }
        }
Beispiel #3
0
        public static void UpdateThumbColor(this UISwitch uiSwitch, ISwitch view, UIColor?defaultThumbColor)
        {
            if (view == null)
            {
                return;
            }

            Graphics.Color thumbColor = view.ThumbColor;
            uiSwitch.ThumbTintColor = thumbColor?.ToNative() ?? defaultThumbColor;
        }
Beispiel #4
0
 public static void UpdatePlaceholderColor(this EditText editText, Graphics.Color placeholderTextColor, ColorStateList?defaultColor)
 {
     if (placeholderTextColor == null)
     {
         editText.SetHintTextColor(defaultColor);
     }
     else
     {
         var androidColor = placeholderTextColor.ToNative();
         if (!editText.HintTextColors.IsOneColor(ColorStates.EditText, androidColor))
         {
             editText.SetHintTextColor(ColorStateListExtensions.CreateEditText(androidColor));
         }
     }
 }
Beispiel #5
0
        public static void SetColorFilter(this ADrawable drawable, Graphics.Color color, FilterMode mode, AColorFilter?defaultColorFilter)
        {
            if (drawable == null)
            {
                return;
            }

            if (color == null)
            {
                SetColorFilter(drawable, defaultColorFilter);
            }
            else
            {
                drawable.SetColorFilter(color.ToNative(), mode);
            }
        }
Beispiel #6
0
 public static void UpdateTextColor(this EditText editText, Graphics.Color textColor, ColorStateList?defaultColor)
 {
     if (textColor == null)
     {
         if (defaultColor != null)
         {
             editText.SetTextColor(defaultColor);
         }
     }
     else
     {
         var androidColor = textColor.ToNative();
         if (!editText.TextColors.IsOneColor(ColorStates.EditText, androidColor))
         {
             editText.SetTextColor(ColorStateListExtensions.CreateEditText(androidColor));
         }
     }
 }
Beispiel #7
0
        void DrawShadow(Canvas canvas)
        {
            if (_shadowCanvas == null)
            {
                _shadowCanvas = new Canvas();
            }

            if (_shadowPaint == null)
            {
                _shadowPaint = new Android.Graphics.Paint
                {
                    AntiAlias    = true,
                    Dither       = true,
                    FilterBitmap = true
                }
            }
            ;

            Graphics.Color solidColor = null;

            // If need to redraw shadow
            if (_invalidateShadow)
            {
                // If bounds is zero
                if (_viewBounds.Width() != 0 && _viewBounds.Height() != 0)
                {
                    var bitmapHeight = _viewBounds.Height() + MaximumRadius;
                    var bitmapWidth  = _viewBounds.Width() + MaximumRadius;

                    // Reset bitmap to bounds
                    _shadowBitmap = Bitmap.CreateBitmap(
                        bitmapWidth, bitmapHeight, Bitmap.Config.Argb8888
                        );

                    // Reset Canvas
                    _shadowCanvas.SetBitmap(_shadowBitmap);

                    _invalidateShadow = false;

                    // Create the local copy of all content to draw bitmap as a
                    // bottom layer of natural canvas.
                    base.DispatchDraw(_shadowCanvas);

                    // Get the alpha bounds of bitmap
                    Bitmap extractAlpha = _shadowBitmap.ExtractAlpha();

                    // Clear past content content to draw shadow
                    _shadowCanvas.DrawColor(Android.Graphics.Color.Black, PorterDuff.Mode.Clear);

                    var shadowOpacity = (float)Shadow.Opacity;

                    if (Shadow.Paint is LinearGradientPaint linearGradientPaint)
                    {
                        var linearGradientShaderFactory = PaintExtensions.GetLinearGradientShaderFactory(linearGradientPaint);
                        _shadowPaint.SetShader(linearGradientShaderFactory.Resize(bitmapWidth, bitmapHeight));
                    }
                    if (Shadow.Paint is RadialGradientPaint radialGradientPaint)
                    {
                        var radialGradientShaderFactory = PaintExtensions.GetRadialGradientShaderFactory(radialGradientPaint);
                        _shadowPaint.SetShader(radialGradientShaderFactory.Resize(bitmapWidth, bitmapHeight));
                    }
                    if (Shadow.Paint is SolidPaint solidPaint)
                    {
                        solidColor         = solidPaint.ToColor();
                        _shadowPaint.Color = solidColor.WithAlpha(shadowOpacity).ToNative();
                    }

                    // Apply the shadow radius
                    var radius = Shadow.Radius;

                    if (radius <= 0)
                    {
                        radius = 0.01f;
                    }

                    if (radius > 100)
                    {
                        radius = MaximumRadius;
                    }

                    _shadowPaint.SetMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.Normal));

                    float shadowOffsetX = (float)Shadow.Offset.X;
                    float shadowOffsetY = (float)Shadow.Offset.Y;

                    if (Clip == null)
                    {
                        _shadowCanvas.DrawBitmap(extractAlpha, shadowOffsetX, shadowOffsetY, _shadowPaint);
                    }
                    else
                    {
                        var bounds = new RectangleF(0, 0, canvas.Width, canvas.Height);
                        var path   = Clip.PathForBounds(bounds)?.AsAndroidPath();

                        path.Offset(shadowOffsetX, shadowOffsetY);

                        _shadowCanvas.DrawPath(path, _shadowPaint);
                    }

                    // Recycle and clear extracted alpha
                    extractAlpha.Recycle();
                }
                else
                {
                    // Create placeholder bitmap when size is zero and wait until new size coming up
                    _shadowBitmap = Bitmap.CreateBitmap(1, 1, Bitmap.Config.Rgb565 !);
                }
            }

            // Reset alpha to draw child with full alpha
            if (solidColor != null)
            {
                _shadowPaint.Color = solidColor.ToNative();
            }

            // Draw shadow bitmap
            if (_shadowCanvas != null && _shadowBitmap != null && !_shadowBitmap.IsRecycled)
            {
                canvas.DrawBitmap(_shadowBitmap, 0.0F, 0.0F, _shadowPaint);
            }
        }

        void ClearShadowResources()
        {
            _shadowCanvas?.Dispose();
            _shadowPaint?.Dispose();
            _shadowBitmap?.Dispose();
            _shadowCanvas = null;
            _shadowPaint  = null;
            _shadowBitmap = null;
        }
    }
Beispiel #8
0
 public static WBrush ToBrush(this Graphics.Color color) => color.ToNative();