Status Update
Comments
ma...@google.com <ma...@google.com>
nj...@google.com <nj...@google.com> #2
Assuming that the ic_android.xml asset is the following one:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>
It looks like the size given to the Image composable is larger than the intrinsic size of the vector asset (56dp vs 24dp). By default ContentScale.Inside is used on the Image composable which will shrink down the size of the VectorAsset if the destination size is smaller than that of the defaults of the VectorAsset itself. However, if the destination is larger than the intrinsic size, no scaling is applied which is why the size of the icon does not change. To verify I created the following composable:
Image(asset = vectorResource(id = R.drawable.ic_android),
contentScale = ContentScale.Fit,
modifier = Modifier
.drawBackground(Color.Gray)
.size(size.value)
.clickable(
onClick = {
if (size.value == 56.dp) {
size.value = 300.dp
} else {
size.value = 56.dp
}
}
)
)
By changing the ContentScale here to ContentScale.Fit, this will scale the VectorAsset to the bounds of the composable. Clicking the Image here will toggle the size between 56 and 300dp and verified that the VectorAsset is scaled accordingly.
TLDR use ContentScale.Fit instead of the default of ContentScale.Inside.
he...@gmail.com <he...@gmail.com> #3
I've tried ContentScale.Fit
, and it worked well. Thank you for explaining that
nj...@google.com <nj...@google.com> #4
Thanks for filing this ticket. After some additional thought we have decided to change the default ContentScale parameter to be ContentScale.Fit instead of inside. This would make the original code example work as expected without having to be explicit. Usually when a default size is provided, the intention is the scale the content accordingly either up or down. This does not change the default behavior if no explicit size is provided.
ap...@google.com <ap...@google.com> #5
Branch: androidx-master-dev
commit 1c3daea07de792f10e46dfd4ec0d171e1cb9b7e4
Author: Nader Jawad <njawad@google.com>
Date: Mon Jun 29 13:32:51 2020
Change Default ContentScale for Image
Relnote: "Changed the default ContentScale
parameter for the Image composable
from Inside to Fit. This was done in order
to get behavior to scale up the underlying
Painter if the layout size is larger than
the intrinsic size of the painter while
maintaining the aspect ratio. This
behavior better matches expectations
for providing fixed sizes to the Image
while not affecting the default behavior
if only the intrinsic size is used to
compute the size of the composable."
Fixes: 159838006
Test: Updated ImageTest to optionally provide
ContentScale.Inside as well as removing
explicit usages of ContentScale.Fit
Change-Id: I40ae3af9b6a2efc3d730ea0ba5f457a788eca1f7
M ui/ui-foundation/api/0.1.0-dev15.txt
M ui/ui-foundation/api/current.txt
M ui/ui-foundation/api/public_plus_experimental_0.1.0-dev15.txt
M ui/ui-foundation/api/public_plus_experimental_current.txt
M ui/ui-foundation/api/restricted_0.1.0-dev15.txt
M ui/ui-foundation/api/restricted_current.txt
M ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ImageTest.kt
M ui/ui-foundation/src/main/java/androidx/ui/foundation/Image.kt
Description
Android Studio Build: 4.2 Canary 2 Version of Gradle Plugin: 4.2.0-alpha02' Version of Gradle: 6.5-rc-1 Version of Java: 8 OS: Linux
Steps to Reproduce:
vectorResource()
to obtain a Drawable from resourcesImage()
composableModifier.size()
Example:
Image( asset = vectorResource(id = R.drawable.ic_android), modifier = Modifier.size(56.dp) )
But it does not change the size of my icon.