Skip to content
Merged
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
1 change: 1 addition & 0 deletions eng/BannedSymbols.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddSingleton`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to create the service instead
M:Android.Content.Res.ColorStateList.#ctor(System.Int32[][],System.Int32[]);Use Microsoft.Maui.PlatformInterop.Get*ColorStateList() Java methods instead
M:Android.Widget.ImageView.GetScaleType();Use PlatformInterop.IsImageViewCenterCrop instead (or add a new method)
P:Microsoft.Maui.MauiWinUIApplication.Services;Use the IPlatformApplication.Current.Services instead
P:Microsoft.Maui.MauiWinUIApplication.Application;Use the IPlatformApplication.Current.Application instead
P:Microsoft.UI.Xaml.Window.AppWindow;This API doesn't have null safety. Use GetAppWindow() and make sure to account for the possibility that GetAppWindow() might be null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void OnLayoutChange(object sender, global::Android.Views.View.LayoutChang
{
if (sender is IVisualElementRenderer renderer && renderer.View is ImageView imageView)
#pragma warning disable CS0618 // Obsolete
AViewCompat.SetClipBounds(imageView, imageView.GetScaleType() == AScaleType.CenterCrop ? new ARect(0, 0, e.Right - e.Left, e.Bottom - e.Top) : null);
AViewCompat.SetClipBounds(imageView, PlatformInterop.IsImageViewCenterCrop(imageView) ? new ARect(0, 0, e.Right - e.Left, e.Bottom - e.Top) : null);
#pragma warning restore CS0618 // Obsolete

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<RootNamespace>Microsoft.Maui.DeviceTests</RootNamespace>
<AssemblyName>Microsoft.Maui.Controls.DeviceTests</AssemblyName>
<NoWarn>$(NoWarn),CA1416</NoWarn>
<IsTestProject>true</IsTestProject>
<!-- Disable multi-RID builds to workaround a parallel build issue -->
<RuntimeIdentifier Condition="$(TargetFramework.Contains('-maccatalyst'))">maccatalyst-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="$(TargetFramework.Contains('-maccatalyst')) and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'arm64'">maccatalyst-arm64</RuntimeIdentifier>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,19 @@ public static Animatable getAnimatable(Drawable drawable) {
}
return null;
}

/*
* Checks if the ScaleType of the ImageView is CENTER_CROP.
* Avoids returning an object to C#.
*/
public static boolean isImageViewCenterCrop(@NonNull ImageView imageView) {
return imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP;
}

/*
* Sets View.ClipBounds without creating a Rect object in C#
*/
public static void setClipBounds(@NonNull View view, int left, int top, int right, int bottom) {
view.setClipBounds(new Rect(left, top, right, bottom));
}
}
7 changes: 3 additions & 4 deletions src/Core/src/Handlers/Image/ImageHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,13 @@ await handler

public override void PlatformArrange(Graphics.Rect frame)
{
if (PlatformView.GetScaleType() == ImageView.ScaleType.CenterCrop)
if (PlatformInterop.IsImageViewCenterCrop(PlatformView))
{
// If the image is center cropped (AspectFill), then the size of the image likely exceeds
// the view size in some dimension. So we need to clip to the view's bounds.

var (left, top, right, bottom) = PlatformView.Context!.ToPixels(frame);
var clipRect = new Android.Graphics.Rect(0, 0, right - left, bottom - top);
PlatformView.ClipBounds = clipRect;
var (left, top, right, bottom) = PlatformView.ToPixels(frame);
PlatformInterop.SetClipBounds(PlatformView, 0, 0, right - left, bottom - top);
}
else
{
Expand Down
11 changes: 11 additions & 0 deletions src/Core/src/Platform/Android/ContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ static float ToPixelsUsingMetrics(double dp)
return (float)Math.Ceiling((dp * s_displayDensity) - GeometryUtil.Epsilon);
}

Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

The new ToPixels extension method for View lacks XML documentation. Consider adding documentation explaining its purpose and how it differs from the Context.ToPixels method.

Suggested change
/// <summary>
/// Converts a <see cref="Graphics.Rect"/> from device-independent pixels (DP) to physical pixels using the display metrics associated with the specified <see cref="View"/>.
/// This differs from <see cref="Context.ToPixels(Graphics.Rect)"/> in that it uses the metrics of the <see cref="View"/>, which may be different from those of the <see cref="Context"/>.
/// </summary>
/// <param name="view">The <see cref="View"/> whose display metrics are used for conversion.</param>
/// <param name="rectangle">The rectangle in device-independent pixels (DP) to convert.</param>
/// <returns>
/// A tuple containing the left, top, right, and bottom coordinates of the rectangle in physical pixels.
/// </returns>

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's internal, so I didn't document it yet. Its purpose is to avoid calling View.Context in the first place.

internal static (int left, int top, int right, int bottom) ToPixels(this View view, Graphics.Rect rectangle)
{
return
(
(int)view.ToPixels(rectangle.Left),
(int)view.ToPixels(rectangle.Top),
(int)view.ToPixels(rectangle.Right),
(int)view.ToPixels(rectangle.Bottom)
);
}

public static (int left, int top, int right, int bottom) ToPixels(this Context context, Graphics.Rect rectangle)
{
return
Expand Down
1 change: 1 addition & 0 deletions src/Core/tests/DeviceTests/Core.DeviceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<RootNamespace>Microsoft.Maui.DeviceTests</RootNamespace>
<AssemblyName>Microsoft.Maui.Core.DeviceTests</AssemblyName>
<NoWarn>$(NoWarn),CA1416</NoWarn>
<IsTestProject>true</IsTestProject>
<!-- Disable multi-RID builds to workaround a parallel build issue -->
<RuntimeIdentifier Condition="$(TargetFramework.Contains('-maccatalyst'))">maccatalyst-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="$(TargetFramework.Contains('-maccatalyst')) and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'arm64'">maccatalyst-arm64</RuntimeIdentifier>
Expand Down
Loading