public void Apply(Paint pPaint)
		{
			pPaint.SetMaskFilter (mBlurMaskFilter);
		}
Exemple #2
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)
            {
                var viewHeight = _viewBounds.Height();
                var viewWidth  = _viewBounds.Width();

                if (GetChildAt(0) is AView child)
                {
                    if (viewHeight == 0)
                    {
                        viewHeight = child.MeasuredHeight;
                    }

                    if (viewWidth == 0)
                    {
                        viewWidth = child.MeasuredWidth;
                    }
                }

                // If bounds is zero
                if (viewHeight != 0 && viewWidth != 0)
                {
                    var bitmapHeight = viewHeight + MaximumRadius;
                    var bitmapWidth  = viewWidth + 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, shadowOpacity);
                        _shadowPaint.SetShader(linearGradientShaderFactory.Resize(bitmapWidth, bitmapHeight));
                    }
                    if (Shadow.Paint is RadialGradientPaint radialGradientPaint)
                    {
                        var radialGradientShaderFactory = PaintExtensions.GetRadialGradientShaderFactory(radialGradientPaint, shadowOpacity);
                        _shadowPaint.SetShader(radialGradientShaderFactory.Resize(bitmapWidth, bitmapHeight));
                    }
                    if (Shadow.Paint is SolidPaint solidPaint)
                    {
                        solidColor = solidPaint.ToColor();
#pragma warning disable CA1416 // https://github.com/xamarin/xamarin-android/issues/6962
                        _shadowPaint.Color = solidColor.WithAlpha(shadowOpacity).ToPlatform();
#pragma warning restore CA1416
                    }

                    // 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 Graphics.RectF(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)
#pragma warning disable CA1416 // https://github.com/xamarin/xamarin-android/issues/6962
            {
                _shadowPaint.Color = solidColor.ToPlatform();
            }
#pragma warning restore CA1416

            // 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;
        }
    }
		void Initialize ()
		{
			window_manager = (Context.GetSystemService (Context.WindowService)).JavaCast<IWindowManager> ();

			//set background paint properties
			background_paint = new Paint ();
			background_paint.SetStyle (Paint.Style.Fill);
			background_paint.Dither = true;
			background_paint.AntiAlias = true;

			image_paint = new Paint ();

			//set properties for the other paints
			circle_paint = new Paint ();
			circle_paint.SetStyle (Paint.Style.Fill);
			circle_paint.Dither = true;
			circle_paint.AntiAlias = true;

			led_paint = new Paint ();
			led_paint.SetStyle (Paint.Style.Fill);
			led_paint.Dither = true;
			led_paint.AntiAlias = true;
			BlurMaskFilter blur_mask_filter = new BlurMaskFilter (20.0f, BlurMaskFilter.Blur.Outer);
			led_paint.SetMaskFilter (blur_mask_filter);

			directional_paint = new Paint ();
			directional_paint.Dither = true;
			directional_paint.AntiAlias = true;
			directional_paint.Alpha = 204;

			gradient_paint = new Paint ();
			gradient_paint.Dither = true;
			gradient_paint.AntiAlias = true;
			gradient_paint.Alpha = 204;
		}
		private void Init()
		{
			_buttonBackgroundPaint = new Paint(PaintFlags.AntiAlias);
			_buttonBackgroundPaint.SetMaskFilter(new BlurMaskFilter(2, BlurMaskFilter.Blur.Normal));
			_shadowPaint = new Paint();
			_shadowPaint.AntiAlias = true;
		}