How can we tile a vector image?

后端 未结 5 732
眼角桃花
眼角桃花 2021-02-12 21:02

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 impos

5条回答
  •  無奈伤痛
    2021-02-12 21:22

    Thanks to @pskink I made a drawable that tiles another drawable: https://gist.github.com/9ffbdf01478e36194f8f

    This has to be set in code, it can not be used from 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);
            }
        }
    }
    

提交回复
热议问题