Skip to content

Commit

Permalink
fix: ImageBrush RenderTransform does not throw
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Aug 23, 2021
1 parent 00a3180 commit a633fa9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@
</Border.BorderBrush>
</Border>
</StackPanel>
<TextBlock FontSize="15">Linear Gradient as Border with relative transform</TextBlock>
<TextBlock FontSize="15">Linear Gradient as Border with relative transform / with single stop</TextBlock>
<StackPanel Orientation="Horizontal" Spacing="10">
<Border Width="100" Height="100" BorderThickness="40" CornerRadius="8">
<Border.BorderBrush>
Expand All @@ -296,6 +296,15 @@
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
<Border Width="100" Height="100" BorderThickness="40" CornerRadius="8">
<Border.BorderBrush>
<LinearGradientBrush MappingMode="Absolute" StartPoint="0,0" EndPoint="0,40">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Red" Offset="0.5" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
</StackPanel>
<TextBlock FontSize="15">Text with Radial/Linear gradient as foreground brush (fallback=green)</TextBlock>
<StackPanel Orientation="Horizontal" Spacing="10">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<DataTemplate>
<Border Width="150" Height="200">
<Border.Background>
<ImageBrush ImageSource="http://homepages.cae.wisc.edu/~ece533/images/fruits.png"
<ImageBrush ImageSource="ms-appx:///Assets/ingredient2.png"
Stretch="UniformToFill">
<ImageBrush.RelativeTransform>
<RotateTransform Angle="45" CenterX=".5" CenterY=".5"/>
Expand All @@ -31,4 +31,4 @@
</DataTemplate>
</controls:SampleControl.SampleContent>
</controls:SampleControl>
</UserControl>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<Grid>
<Border Width="150" Height="200">
<Border.Background>
<ImageBrush ImageSource="http://homepages.cae.wisc.edu/~ece533/images/fruits.png"
<ImageBrush ImageSource="ms-appx:///Assets/ingredient2.png"
Stretch="UniformToFill">
<ImageBrush.RelativeTransform>
<ScaleTransform ScaleX="1.5" ScaleY="1.5" CenterX=".5" CenterY=".5"/>
Expand All @@ -33,4 +33,4 @@
</DataTemplate>
</controls:SampleControl.SampleContent>
</controls:SampleControl>
</UserControl>
</UserControl>
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Media/ImageBrush.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private Android.Graphics.Matrix GenerateMatrix(Windows.Foundation.Rect drawRect,

matrix.SetRectToRect(sourceRect.ToRectF(), destinationRect.ToRectF(), Android.Graphics.Matrix.ScaleToFit.Fill);

RelativeTransform?.ToNativeMatrix(matrix, size: new Size(drawRect.Width, drawRect.Height));
RelativeTransform?.ToNativeMatrix(matrix, size: new Size(sourceRect.Width, sourceRect.Height));
return matrix;
}

Expand Down
64 changes: 35 additions & 29 deletions src/Uno.UI/UI/Xaml/Media/LinearGradientBrush.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,54 @@ public partial class LinearGradientBrush
{
protected internal override Shader GetShader(Rect destinationRect)
{
// Android LinearGradient requires two ore more stop points.
//TODO: If there is only one - we should use solid color brush here
if (GradientStops.Count >= 2)
if (GradientStops.Count == 0)
{
var colors = GradientStops.SelectToArray(s => ((Android.Graphics.Color)s.Color).ToArgb());
var locations = GradientStops.SelectToArray(s => (float)s.Offset);
return null;
}

var width = destinationRect.Width;
var height = destinationRect.Height;
var colors = GradientStops.SelectToList(s => ((Android.Graphics.Color)s.Color).ToArgb());
var locations = GradientStops.SelectToList(s => (float)s.Offset);

Android.Graphics.Matrix nativeTransformMatrix = null;
if (RelativeTransform != null)
{
var matrix = RelativeTransform.ToMatrix(Foundation.Point.Zero, new Windows.Foundation.Size(width, height));
matrix.M31 *= (float)width;
matrix.M32 *= (float)height;
nativeTransformMatrix = matrix.ToNative();
}
if (GradientStops.Count == 1)
{
// Android LinearGradient requires two ore more stop points.
// We work around this by duplicating the first gradient stop.
colors.Add(colors[0]);
locations.Add(locations[0]);
}

var width = destinationRect.Width;
var height = destinationRect.Height;

Android.Graphics.Matrix nativeTransformMatrix = null;
if (RelativeTransform != null)
{
var matrix = RelativeTransform.ToMatrix(Foundation.Point.Zero, new Windows.Foundation.Size(width, height));
matrix.M31 *= (float)width;
matrix.M32 *= (float)height;
nativeTransformMatrix = matrix.ToNative();
}

//Matrix .MapPoints takes an array of floats
var pts = MappingMode == BrushMappingMode.RelativeToBoundingBox
? new[]
{
//Matrix .MapPoints takes an array of floats
var pts = MappingMode == BrushMappingMode.RelativeToBoundingBox
? new[]
{
(float) (StartPoint.X * width),
(float) (StartPoint.Y * height),
(float) (EndPoint.X * width),
(float) (EndPoint.Y * height)
}
: new[]
{
}
: new[]
{
(float) ViewHelper.LogicalToPhysicalPixels(StartPoint.X),
(float) ViewHelper.LogicalToPhysicalPixels(StartPoint.Y),
(float) ViewHelper.LogicalToPhysicalPixels(EndPoint.X),
(float) ViewHelper.LogicalToPhysicalPixels(EndPoint.Y)
};

nativeTransformMatrix?.MapPoints(pts);
nativeTransformMatrix?.Dispose();
return new LinearGradient(pts[0], pts[1], pts[2], pts[3], colors, locations, Shader.TileMode.Clamp);
}
};

return null;
nativeTransformMatrix?.MapPoints(pts);
nativeTransformMatrix?.Dispose();
return new LinearGradient(pts[0], pts[1], pts[2], pts[3], colors.ToArray(), locations.ToArray(), Shader.TileMode.Clamp);
}
}
}
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/Media/Transform.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public partial class Transform

targetMatrix.SetValues(new[]
{
matrix.M11, matrix.M21, matrix.M31,
matrix.M12, matrix.M22, matrix.M32,
matrix.M11, matrix.M21, matrix.M31 * (float)size.Width,
matrix.M12, matrix.M22, matrix.M32 * (float)size.Height,
0, 0, 1
});

Expand Down

0 comments on commit a633fa9

Please sign in to comment.