How to make a Material You-like widget with image whose corners are rounded?
It is already made in Google Photo, but how to get the same effect?
Many ways (like ShapeableImageView) are forbidden and impossible to bypass due to restrictions of RemoteView.
These are some additional details for the first suggested answer:
My widget.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:theme="@style/widget_theme">
<ImageView
android:id="@+id/widget_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/MY_IMAGE" />
</LinearLayout>
attrs.xml in res/values folder looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AppWidgetAttrs">
<attr name="appWidgetPadding" format="dimension" />
<attr name="appWidgetInnerRadius" format="dimension" />
<attr name="appWidgetRadius" format="dimension" />
</declare-styleable>
</resources>
styles.xml in res/values folder looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Widget theme -->
<style name="widget_theme" parent="Theme.Material3.DayNight">
<item name="appWidgetRadius">@dimen/rounded_corners</item>
<item name="appWidgetInnerRadius">@dimen/rounded_corners_inner</item>
<item name="appWidgetPadding">0dp</item>
</style>
</resources>
dimens.xml in res/values folder looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="rounded_corners">16dp</dimen>
<dimen name="rounded_corners_inner">8dp</dimen>
</resources>
I test my app on a virtual Pixel 4a (API 30).
Minumum API is API 24.
Compile and Target SDKs are both API 31.
Actual solution
What the op wants is a widget containing only an image, and this image should be rounded, example:All you need is:
That's all, if you want the image to be circular just set a huge border radius.
Old and misunderstood solution
I'm probably late, but i had the same problem. A simple workaround is to use a foreground to "mask" tha image form, when the image is a normal ImageView inside the widget's layout. So what i did was:
EDIT: the op request was different, but this remains a valid solution for images inside the layout of the widget, so i won't delete it, just in case.