Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Apply offsets in API Level 29 or higher in GradientDrawable (#13414)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz authored Dec 23, 2021
1 parent 0eb3194 commit be6a45b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test 13100" xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue13100">
<local:TestContentPage.Resources>
<ResourceDictionary>

<LinearGradientBrush x:Key="IssueLinearGradientBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Green" Offset="0.0" />
<GradientStop Color="Blue" Offset="0.25" />
<GradientStop Color="Gold" Offset="1.0" />
</LinearGradientBrush>

</ResourceDictionary>
</local:TestContentPage.Resources>
<StackLayout>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="If both gradients render exactly the same, the test passed."/>
<StackLayout
Padding="12">
<Label
Text="Frame"/>
<Frame
BorderColor="Black"
HeightRequest="120"
WidthRequest="120"
Background="{StaticResource IssueLinearGradientBrush}"/>
<Label
Text="Rectangle"/>
<Rectangle
HeightRequest="120"
WidthRequest="120"
Background="{StaticResource IssueLinearGradientBrush}"/>
</StackLayout>
</StackLayout>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Xamarin.Forms.CustomAttributes;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.Brush)]
#endif
[Issue(IssueTracker.Github, 13100,
"[Bug] LinearGradientBrush GradientStop offset (Android)",
PlatformAffected.Android)]
public partial class Issue13100 : TestContentPage
{
public Issue13100()
{
#if APP
InitializeComponent();
#endif
}

protected override void Init()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutBackground.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentOffest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentWithZeroMargin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13100.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13337.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13232.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13378.xaml.cs" />
Expand Down Expand Up @@ -2248,6 +2249,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13136.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13100.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13337.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
19 changes: 17 additions & 2 deletions Xamarin.Forms.Platform.Android/Extensions/BrushExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,15 @@ public static void UpdateBackground(this GradientDrawable gradientDrawable, Brus
return;

gradientDrawable.SetGradientType(GradientType.LinearGradient);
gradientDrawable.SetColors(colors);

if (Forms.Is29OrNewer)
{
var offsets = gradientBrushData.Item2;
gradientDrawable.SetColors(colors, offsets);
}
else
gradientDrawable.SetColors(colors);

SetGradientOrientation(gradientDrawable, angle);
}

Expand All @@ -166,7 +174,14 @@ public static void UpdateBackground(this GradientDrawable gradientDrawable, Brus
gradientDrawable.SetGradientType(GradientType.RadialGradient);
gradientDrawable.SetGradientCenter(centerX, centerY);
gradientDrawable.SetGradientRadius(Math.Max(height, width) * radius);
gradientDrawable.SetColors(colors);

if (Forms.Is29OrNewer)
{
var offsets = gradientBrushData.Item2;
gradientDrawable.SetColors(colors, offsets);
}
else
gradientDrawable.SetColors(colors);
}
}

Expand Down

0 comments on commit be6a45b

Please sign in to comment.