Search code examples
androidvector-graphics

How can we tile a vector image?


With the support library now fully supporting vector images, I'm trying to switch to vector images as much as I can in my app. An issue I'm running into is that it seems impossible to repeat them.

With bitmap images the following xml could be used:

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/repeat_me"
    android:tileMode="repeat"
    />

This does not work, as vector images can not be used in bitmaps: https://code.google.com/p/android/issues/detail?id=187566

Is there any other way to tile/repeat vector images?


Solution

  • Check Nick Butcher solution:
    https://gist.github.com/nickbutcher/4179642450db266f0a33837f2622ace3
    Add TileDrawable class to your project and then set tiled drawable to your image view:

    // after view created
    val d = ContextCompat.getDrawable(this, R.drawable.pattern)
    imageView.setImageDrawable(TileDrawable(d, Shader.TileMode.REPEAT))