Go through layers and dispatch them via fixDrawableRepeat - Android Graphics

Android examples for Graphics:Drawable

Description

Go through layers and dispatch them via fixDrawableRepeat

Demo Code


//package com.java2s;

import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;

public class Main {
    /** Go through layers and dispatch them via fixDrawableRepeat **/
    public static void fixLayerRepeat(LayerDrawable lyr) {
        for (int i = 0; i < lyr.getNumberOfLayers(); i++) {
            fixDrawableRepeat(lyr.getDrawable(i));
        }/*from   www  . j a v a2  s  . c  om*/
    }

    /** Dispatches Drawable repeat fixes based on the type of the Drawable **/
    public static void fixDrawableRepeat(Drawable d) {
        if (d != null) {
            if (d instanceof BitmapDrawable)
                fixBitmapRepeat((BitmapDrawable) d);
            else if (d instanceof LayerDrawable)
                fixLayerRepeat((LayerDrawable) d);
            else if (d instanceof StateListDrawable)
                fixStateListRepeat((StateListDrawable) d);
        }
    }

    /** Mutate the bitmap and reset tilemode **/
    public static void fixBitmapRepeat(BitmapDrawable bmp) {
        bmp.mutate(); // make sure that we aren't sharing state anymore
        bmp.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
    }

    /** There's a limitation where we can only get the current Drawable, so that's the only one that can be mutated **/
    private static void fixStateListRepeat(StateListDrawable d) {
        fixDrawableRepeat(d.getCurrent());
    }
}

Related Tutorials