protected override void OnDraw(Android.Graphics.Canvas canvas) { base.OnDraw(canvas); if (Borders != null) { Android.Graphics.Paint paint = new Android.Graphics.Paint(); paint.Color = Color.Black; paint.SetStyle(Android.Graphics.Paint.Style.Stroke); int Height = this.Height; int Width = this.Width; if (Borders.Top > 0) { paint.StrokeWidth = Borders.Top * 2; canvas.DrawLine(0, 0, Width, 0, paint); } if (Borders.Bottom > 0) { paint.StrokeWidth = Borders.Bottom * 2; canvas.DrawLine(0, Height, Width, Height, paint); } if (Borders.Left > 0) { paint.StrokeWidth = Borders.Left * 2; canvas.DrawLine(0, 0, 0, Height, paint); } if (Borders.Right > 0) { paint.StrokeWidth = Borders.Right * 2; canvas.DrawLine(Width, 0, Width, Height, paint); } paint.Dispose(); } }
public void Dispose() { if (_paint != null) { _paint.Dispose(); } _paint = null; }
public override void Draw(Canvas canvas) { var bounds = Bounds; using (var paint = new Paint()) { paint.Color = Color; canvas.DrawRect(new ARect(0, 0, bounds.Right, TopSize), paint); canvas.DrawRect(new ARect(0, bounds.Bottom - BottomSize, bounds.Right, bounds.Bottom), paint); paint.Dispose(); } }
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; } }
private void InitializeImages() { if(_pressedImage != null) { _pressedImage.Recycle(); _pressedImage.Dispose(); } if(_unpressedImage != null) { _unpressedImage.Recycle(); _unpressedImage.Dispose(); } _unpressedImage = Bitmap.CreateBitmap(Width, Settings.IsSquared ? Width : Height, Bitmap.Config.Argb8888); _pressedImage = Bitmap.CreateBitmap(Width, Settings.IsSquared ? Width : Height, Bitmap.Config.Argb8888); Canvas unpressedCanvas = new Canvas(_unpressedImage); Canvas pressedCanvas = new Canvas(_pressedImage); Settings.SetTextSize(ComplexUnitType.Px, Settings.TextSize == 0f ? Height * Settings.TextSizeRatio : Settings.TextSize); Settings.StrokeBorderWidth = Height * Settings.StrokeBorderWidthRatio; Settings.StrokeTextWidth = Height * Settings.StrokeTextWidthRatio; // Background fill paint Paint fillBackPaint = new Paint(); fillBackPaint.Color = Settings.FillColor; fillBackPaint.AntiAlias = true; // Background stroke paint Paint strokeBackPaint = new Paint(); strokeBackPaint.Color = Settings.StrokeColor; strokeBackPaint.SetStyle(Paint.Style.Stroke); strokeBackPaint.StrokeWidth = Settings.StrokeBorderWidth; strokeBackPaint.AntiAlias = true; // Text paint Paint textPaint = new Paint(); textPaint.Color = Settings.IsTextStroked ? Settings.FillColor : Settings.StrokeColor; textPaint.TextAlign = Paint.Align.Center; textPaint.TextSize = Settings.TextSize; textPaint.SetTypeface(Settings.Typeface); textPaint.AntiAlias = true; // Text stroke paint Paint strokePaint = new Paint(); strokePaint.Color = Settings.StrokeColor; strokePaint.TextAlign = Paint.Align.Center; strokePaint.TextSize = Settings.TextSize; strokePaint.SetTypeface(Settings.Typeface); strokePaint.SetStyle(Paint.Style.Stroke); strokePaint.StrokeWidth = Settings.StrokeTextWidth; strokePaint.AntiAlias = true; // Background bounds Rect local = new Rect(); this.GetLocalVisibleRect(local); RectF bounds = new RectF(local); bounds.Top += Settings.StrokeBorderWidth/2; bounds.Left += Settings.StrokeBorderWidth/2; bounds.Right -= Settings.StrokeBorderWidth/2; bounds.Bottom -= Settings.StrokeBorderWidth/2; while(bounds.Top > Height) { bounds.Top -= Height; } while(bounds.Bottom > Height) { bounds.Bottom -= Height; } while(bounds.Left > Width) { bounds.Left -= Width; } while(bounds.Right > Width) { bounds.Right -= Width; } // Text location Rect r = new Rect(); strokePaint.GetTextBounds(Text, 0, Text.Length, r); while(r.Width() + Settings.StrokeTextWidth >= bounds.Width()) { Settings.SetTextSize(ComplexUnitType.Px, Settings.TextSize-1); textPaint.TextSize = Settings.TextSize; strokePaint.TextSize = Settings.TextSize; strokePaint.GetTextBounds(Text, 0, Text.Length, r); } float x=0, y=0; switch (Settings.Gravity) { case GravityFlags.Top: y = PaddingTop + r.Height()/2; break; case GravityFlags.Bottom: y = Height - r.Height()/2 - PaddingBottom; break; default: y = Height / 2f + r.Height() / 2f - r.Bottom; break; } switch (Settings.Gravity) { case GravityFlags.Left: x = PaddingLeft + r.Width()/2; break; case GravityFlags.Right: x = Width - r.Width()/2 - PaddingRight; break; default: x = Width/2; break; } // Draw unpressed DrawBackground(unpressedCanvas, bounds, fillBackPaint, strokeBackPaint); if(Settings.IsTextStroked) unpressedCanvas.DrawText(Text, x, y, strokePaint); unpressedCanvas.DrawText(Text, x, y, textPaint); // Change colors fillBackPaint.Color = Settings.StrokeColor; strokeBackPaint.Color = Settings.FillColor; strokePaint.Color = Settings.FillColor; textPaint.Color = Settings.IsTextStroked ? Settings.StrokeColor : Settings.FillColor; // Draw pressed DrawBackground(pressedCanvas, bounds, fillBackPaint, strokeBackPaint); if(Settings.IsTextStroked) pressedCanvas.DrawText(Text, x, y, strokePaint); pressedCanvas.DrawText(Text, x, y, textPaint); // Set images for states StateListDrawable states = new StateListDrawable(); states.AddState(new int[] {Android.Resource.Attribute.StatePressed}, new BitmapDrawable(_pressedImage)); states.AddState(new int[] {Android.Resource.Attribute.StateFocused}, new BitmapDrawable(_pressedImage)); states.AddState(new int[] {Android.Resource.Attribute.StateSelected}, new BitmapDrawable(_pressedImage)); states.AddState(new int[] { }, new BitmapDrawable(_unpressedImage)); SetBackgroundDrawable(states); strokePaint.Dispose(); textPaint.Dispose(); strokeBackPaint.Dispose(); fillBackPaint.Dispose(); unpressedCanvas.Dispose(); pressedCanvas.Dispose(); }