How to fit (or scale) a vector image to the phone/emulator screen width (and height, if needed)?

Viewed 195

I'm trying to get the effect of the image on the left (shows well in android preview) but i'm getting the effect of the image on the right (emulator).

Here is my code:

@Composable
fun Login(
    darkTheme: Boolean = isSystemInDarkTheme(),
    navController: NavHostController
) {
    Box {
        Image(
            painter = painterResource(id = R.drawable.ic_login_bg),
            contentDescription = "background"
        )
        Text(
            text = stringResource(id = R.string.welcomeback),
            modifier = Modifier
                .paddingFromBaseline(top = 152.dp)
                .fillMaxWidth(),
            style = MaterialTheme.typography.h2,
            color = MaterialTheme.colors.onBackground,
            textAlign = TextAlign.Center
        )
    }
}

@Preview("Login Light Theme", widthDp = 360, heightDp = 640)
@Composable
fun LoginLightPreview() {
    WeTradeTheme {
        Surface(color = Color.White) {
            val navController = rememberNavController()
            Login(navController = navController)
        }
    }
}

The vector image is:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    android:width="360dp"
    android:height="319dp"
    android:viewportWidth="360"
    android:viewportHeight="319">
  <group>
    <clip-path
        android:pathData="M0,0h360v319h-360z"/>
    <path
        android:pathData="M485.322,293.22C426.908,276.133 412,319 300.942,319C189.885,319 154,259 16.273,294.895C-20.04,309.74 -56.548,331.258 -84.365,359.728C-106.048,381.92 -136.223,413.966 -149.027,442.237C-158.266,462.636 -199.615,488.882 -170.539,505.122C-172.334,512.031 -206.906,522.916 -208.062,530.146L-208.049,530.933C-208.101,530.676 -208.104,530.414 -208.062,530.146L-209.238,454.91L-396.461,-526.739L300.942,-673.524L485.322,293.22Z">
      <aapt:attr name="android:fillColor">
        <gradient 
            android:startY="423.834"
            android:startX="-26.5365"
            android:endY="-531.219"
            android:endX="400.917"
            android:type="linear">
          <item android:offset="0" android:color="#FF322049"/>
          <item android:offset="0.938994" android:color="#FF374092"/>
        </gradient>
      </aapt:attr>
    </path>
  </group>
</vector>

Here is a few things i tried already:

  • Use Modifier.scale(1.10f) in the image. Of course this works but its not scalable since different screens have different sizes and so the scaling factor is also different.
  • Use BoxWithConstraints: same effect as if use Box.

Anyone knows how this can be done using compose?

Thanks!

1 Answers

Your vector has android:width="360dp" and your Preview has the same width: widthDp = 360. It is the reason because in the first image you don't have the white space on the right edge.

You can apply Modifier.fillMaxWidth() to your Image to fill the Box width, and then you have to set the appropriate ContentScale (for example Crop, but it depends by what you want to achieve):

 Box(){
    Image(
        modifier = Modifier.fillMaxWidth(),
        painter = painterResource(id = R.drawable.vector),
        contentDescription = "background",
        contentScale = ContentScale.Crop
    )
    //...
  }

enter image description here

Related