DEV Community

myougaTheAxo
myougaTheAxo

Posted on

SVG & VectorDrawable in Android: ImageVector, AnimatedVector, Dynamic Colors

SVG & VectorDrawable in Android: ImageVector, AnimatedVector, Dynamic Colors

Vector drawables provide scalable, efficient graphics. Jetpack Compose makes working with vectors intuitive.

Creating ImageVector

Icon(
    imageVector = Icons.Default.Home,
    contentDescription = "Home",
    modifier = Modifier.size(24.dp),
    tint = Color.Blue
)
Enter fullscreen mode Exit fullscreen mode

Custom SVG Icons

Convert SVG files to VectorDrawable XML, then use in Compose:

val customIcon = painterResource(id = R.drawable.ic_custom)
Icon(
    painter = customIcon,
    contentDescription = "Custom",
    modifier = Modifier.size(32.dp)
)
Enter fullscreen mode Exit fullscreen mode

Animated Vectors

@Composable
fun AnimatedPlayPause() {
    var isPlaying by remember { mutableStateOf(false) }
    val animatedProgress by animateFloatAsState(
        targetValue = if (isPlaying) 1f else 0f
    )

    Icon(
        imageVector = Icons.Default.PlayArrow,
        contentDescription = "Play/Pause",
        modifier = Modifier.graphicsLayer {
            rotationZ = animatedProgress * 360f
        }
    )
}
Enter fullscreen mode Exit fullscreen mode

Dynamic Colors

val dynamicColor = if (isDarkTheme) Color.White else Color.Black
Icon(
    imageVector = Icons.Default.Favorite,
    contentDescription = "Like",
    tint = dynamicColor
)
Enter fullscreen mode Exit fullscreen mode

Vector graphics are essential for creating adaptive, responsive UIs that look crisp on any screen size.


8 Android app templates available on Gumroad

Top comments (0)