Skip to content

romainguy/v9

Repository files navigation

v9

v9 Android build status

v9 is an Android library that provides the ability to dynamically resize Path objects as you would 9-patch bitmaps. In the example below, the chat bubble is dynamically resized while preserving the corners:

v9-demo.mov

Each slice of the original path (indicated by the shaded areas in the example above) defines a horizontal or vertical region of the path that needs to be stretched. A path can be resized using multiple slices:

Screen.Recording.2022-08-26.at.2.42.35.PM.mov

When multiple slices are used, the amount of stretch applied is proportional to the size of each slice. This behavior is used in the example above to keep the dark bars centered vertically and to spaced them equally horizontally, thus properly preserving details inside the path.

v9 is compatible with API 21+.

Maven

repositories {
    // ...
    mavenCentral()
}

dependencies {
    implementation 'dev.romainguy:v9:0.3.0'
}

How to use

Slicing a Path gives you a PathResizer. The easiest way to slice is to use a single vertical and a single horizontal slice:

val pathResizer = path.slice(Slices(9.0f, 7.0f, 15.0f, 13.0f))

This syntax follows the rectangle convention on Android of passing the top, left, right, and bottom coordinates: there's a vertical slice from 9.0 to 15.0 and a horizontal slice from 7.0 to 13.0.

You can also explicitly declare a list of slices:

val pathResizer = path.slice(
    Slices(
        listOf(Slice(9.0f, 10.0f), Slice(14.0f, 15.0f)),
        listOf(Slice(5.0f, 6.0f),  Slice(18.0f, 19.0f))
    )
)

Once you have a PathResizer, you can call the resize() method to create a new path derived from the original, at the desired size:

val resizedPath = pathResizer.resize(width, height)

For performance considerations, you can pass an existing path to resize(). This path will be rewound and returned:

val resizedPath = pathResizer.resize(width, height, destinationpath)
// resizedPath === destinationpath

License

Please see LICENSE.