我们如何平铺vector图像?

随着支持库现在完全支持vector图像,我试图切换到我的应用程序中尽可能多的vector图像。 我遇到的一个问题是似乎不可能重复它们。

使用位图图像可以使用以下xml:

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

这是行不通的,因为vector图像不能在位图中使用: https : //code.google.com/p/android/issues/detail? id = 187566

有没有其他的方式来平铺/重复vector图像?

Solutions Collecting From Web of "我们如何平铺vector图像?"

感谢@pskink我做了一个可绘制的另一个drawable: https ://gist.github.com/9ffbdf01478e36194f8f

这必须在代码中设置,它不能从XML中使用:

 public class TilingDrawable extends android.support.v7.graphics.drawable.DrawableWrapper { private boolean callbackEnabled = true; public TilingDrawable(Drawable drawable) { super(drawable); } @Override public void draw(Canvas canvas) { callbackEnabled = false; Rect bounds = getBounds(); Drawable wrappedDrawable = getWrappedDrawable(); int width = wrappedDrawable.getIntrinsicWidth(); int height = wrappedDrawable.getIntrinsicHeight(); for (int x = bounds.left; x < bounds.right + width - 1; x+= width) { for (int y = bounds.top; y < bounds.bottom + height - 1; y += height) { wrappedDrawable.setBounds(x, y, x + width, y + height); wrappedDrawable.draw(canvas); } } callbackEnabled = true; } @Override protected void onBoundsChange(Rect bounds) { } /** * {@inheritDoc} */ public void invalidateDrawable(Drawable who) { if (callbackEnabled) { super.invalidateDrawable(who); } } /** * {@inheritDoc} */ public void scheduleDrawable(Drawable who, Runnable what, long when) { if (callbackEnabled) { super.scheduleDrawable(who, what, when); } } /** * {@inheritDoc} */ public void unscheduleDrawable(Drawable who, Runnable what) { if (callbackEnabled) { super.unscheduleDrawable(who, what); } } }