Skip to content

[Android] - Fix Shadow Rendering For Transparent Fill, Stroke (Lines), and Text on Shapes #29528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29394.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29394, "On Android Shadows should not be rendered over fully transparent areas of drawn shapes", PlatformAffected.Android)]
public class Issue29394 : TestContentPage
{
protected override void Init()
{
var verticalStackLayout = new VerticalStackLayout()
{
Padding = new Thickness(30, 0),
Spacing = 25,
VerticalOptions = LayoutOptions.Center
};

var graphicsView = new GraphicsView()
{
HeightRequest = 500,
WidthRequest = 400,
Drawable = new Issue29394_Drawable()
};
var label = new Label()
{
Text = "This is a test for Shadows should not be rendered over fully transparent areas of drawn shapes.",
AutomationId = "label",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};

verticalStackLayout.Children.Add(graphicsView);
verticalStackLayout.Children.Add(label);
Content = verticalStackLayout;
}
}

public class Issue29394_Drawable : IDrawable
{
Rect fillRect = new Rect(0, 0, 300, 300);

public void Draw(ICanvas canvas, RectF dirtyRect)
{
canvas.SaveState();

float centerX = (dirtyRect.Width - (float)fillRect.Width) / 2;
float centerY = (dirtyRect.Height - (float)fillRect.Height) / 2;
var outerRect = new RectF(centerX, centerY, (float)fillRect.Width, (float)fillRect.Height);

canvas.SetFillPaint(Colors.Transparent.AsPaint(), outerRect);
canvas.SetShadow(new SizeF(0, 15), 4, Color.FromArgb("#59000000"));
canvas.FillEllipse(outerRect.X, outerRect.Y, outerRect.Width, outerRect.Height);

float arcSize = 250f;
float arcX = outerRect.X + (outerRect.Width - arcSize) / 2;
float arcY = outerRect.Y + (outerRect.Height - arcSize) / 2;

canvas.FillColor = Colors.Blue;
canvas.FillCircle(arcX + arcSize / 2, arcY + arcSize / 2, 15);
canvas.DrawArc(arcX, arcY, arcSize, arcSize, 45, 90, true, false);

canvas.StrokeColor = Colors.Transparent;
canvas.StrokeSize = 2;
canvas.DrawLine(0, 25, dirtyRect.Width, 25);

canvas.FontColor = Colors.Transparent;
canvas.DrawString("Shadow should not be Rendered", dirtyRect.Width / 2, 10, HorizontalAlignment.Center);

canvas.RestoreState();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;
public class Issue29394 : _IssuesUITest
{
public Issue29394(TestDevice device) : base(device) { }

public override string Issue => "On Android Shadows should not be rendered over fully transparent areas of drawn shapes";

[Test]
[Category(UITestCategories.GraphicsView)]
public void TransparentShapeShouldNotDisplayShadow()
{
App.WaitForElement("label");
VerifyScreenshot();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,40 @@ public PlatformCanvasState(PlatformCanvasState prototype) : base(prototype)
public Color StrokeColor
{
get => _strokeColor;
set => _strokeColor = value;
set
{
if (_strokeColor != value)
{
_strokeColor = value;
UpdateShadowState();
}
}
}

public Color FillColor
{
get => _fillColor;
set => _fillColor = value;
set
{
if (_fillColor != value)
{
_fillColor = value;
UpdateShadowState();
}
}
}

public Color FontColor
{
get => _fontColor;
set
{
_fontColor = value;
FontPaint.Color = value != null ? _fontColor.AsColor() : global::Android.Graphics.Color.Black;
if (_fontColor != value)
{
_fontColor = value;
FontPaint.Color = value != null ? _fontColor.AsColor() : global::Android.Graphics.Color.Black;
UpdateShadowState();
}
}
}

Expand Down Expand Up @@ -352,15 +370,15 @@ public override void Dispose()

public void SetShadow(float blur, float sx, float sy, global::Android.Graphics.Color color)
{
FillPaint.SetShadowLayer(blur, sx, sy, color);
StrokePaint.SetShadowLayer(blur, sx, sy, color);
FontPaint.SetShadowLayer(blur, sx, sy, color);

_shadowed = true;
_shadowBlur = blur;
_shadowX = sx;
_shadowY = sy;
_shadowColor = color;

ApplyShadow(FillPaint, FillColor.Alpha);
ApplyShadow(StrokePaint, StrokeColor.Alpha);
ApplyShadow(FontPaint, FontColor.Alpha);
}

public global::Android.Graphics.Paint GetShadowPaint(float sx, float sy)
Expand Down Expand Up @@ -420,5 +438,25 @@ public void Reset(global::Android.Graphics.Paint aFontPaint, global::Android.Gra
_scaleX = 1;
_scaleY = 1;
}

void ApplyShadow(global::Android.Graphics.Paint paint, float alpha)
{
if (alpha > 0)
{
paint.SetShadowLayer(_shadowBlur, _shadowX, _shadowY, _shadowColor);
}
else
{
paint.ClearShadowLayer();
}
}

void UpdateShadowState()
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider refactoring UpdateShadowState to batch multiple color updates to avoid redundant calls to SetShadow if several color properties change consecutively. This could improve performance if shadow re-application becomes expensive.

Copilot uses AI. Check for mistakes.

{
if (_shadowed)
{
SetShadow(_shadowBlur, _shadowX, _shadowY, _shadowColor);
}
}
}
}
Loading