Repository navigation
[Android] Fix ImageButton not rendering when CornerRadius is larger than half its size - #39344
Conversation
…han half its size The corner radius was copied into the ShapeAppearanceModel without a limit, and ShapeableImageView erased the whole view when the radius was much larger than the view. The radius given to the model is now limited to half of the shorter side and is applied again when the view is laid out.
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 39344Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 39344" |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
Hey there @@KirkPatrickJunsay! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
Hey there @KirkPatrickJunsay! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
@dotnet-policy-service agree |
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
|
Note 🔍
|
|
Note 🔍
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Note 🔍
|
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Description of Change
On Android, an
ImageButtonwhoseCornerRadiusis much larger than half of its size does not draw its background. It still takes up space and still receives taps.Root cause
#30074 made
UpdateMauiRippleDrawableStrokecopy the corner radius into theShapeAppearanceModelof theShapeableImageView, so that the image is clipped to the rounded corners.ShapeableImageViewerases everything outside that shape, background included. The model takes the radius as an absolute size and does not limit it to the size of the view, while theGradientDrawablelayers that draw the background do. With a radius of 100 on a 50x50 view, the whole view is erased.Fix
UpdateMauiRippleDrawableStrokelimits the radius it gives to theShapeAppearanceModelto half of the shorter side of the view. The drawables keep the radius they had.ImageButtonHandler.OnPlatformViewLayoutChangenow also runs theCornerRadiusmapping withUpdateValue(nameof(IImageButton.CornerRadius))when the view's size changes, so custom mappings are applied there too. A change of position only does not re-run it. It already callsUpdateBackgroundthere.A radius of half the size or less gives the same model as before. No public API changes.
I tried Material's
ClampedCornerSizefirst. The binding marks it as internal API (XAOBS001), so the limit is applied in MAUI.Tests
ImageButtonHandlerTests.BackgroundRendersWhenCornerRadiusIsLargerThanHalfTheSize(Core.DeviceTests, Android) creates anImageButtonwith a red background andCornerRadius = 100, and checks that red is drawn.Color [A=255, R=255, G=0, B=0] not foundCategory=ImageButtonCategory=ButtonRun on
main(55c1377) on a Lenovo TB323FU tablet, Android 16 (API 36). The new test also fails when only theOnPlatformViewLayoutChangechange is removed.ImageButtonHandlerTests.CustomCornerRadiusMappingIsKeptAfterLayoutoverrides theCornerRadiusmapping to square the corners and checks they are still square after layout. It fails when the layout callback callsUpdateCornerRadiusdirectly (corner size 69 instead of 0) and passes withUpdateValue;Category=ImageButtonis then 152 passed, 0 failed.ImageButtonHandlerTests.CornerRadiusMappingIsReappliedOnlyWhenTheSizeChangesmoves the laid-out button without resizing it and checks theCornerRadiusmapping did not run again, then resizes it and checks it did. It fails without the size check (the mapping runs on the move) and passes with it;Category=ImageButtonis 153 passed, 0 failed.The Android snapshot for
CornerRadiusShouldBeApplied(Issue23854) is updated. Its 100x20ImageButtonwithCornerRadius="20"was clipped to a distorted shape and is now a pill. The new image is the one this PR's CI produced.I ran the XAML from the issue in the Sandbox app on the same tablet (videos below). I did not run the UI tests locally, and I did not test with Material 3 enabled.
Before
https://github.com/user-attachments/assets/c02d1e2a-4569-4aee-8b40-b2c115d3f1f9
After
https://github.com/user-attachments/assets/56d0967f-26c6-4bf0-8cd3-5121f05d1bb5
Issues Fixed
Fixes #39307