Example usage for android.graphics.drawable BitmapDrawable getConstantState

List of usage examples for android.graphics.drawable BitmapDrawable getConstantState

Introduction

In this page you can find the example usage for android.graphics.drawable BitmapDrawable getConstantState.

Prototype

@Override
    public final ConstantState getConstantState() 

Source Link

Usage

From source file:Main.java

public static Drawable createBitmapDrawable(Context context, Bitmap bitmap) {
    BitmapDrawable d = new BitmapDrawable(context.getResources(), bitmap);
    return d.getConstantState().newDrawable(context.getResources()).mutate();
}

From source file:uk.co.brightec.ratetheapp.RateTheApp.java

/**
 * Converts a drawable to a tiled version of itself. It will recursively traverse layer and state list drawables.
 * This method was copied from android.widget.ProgressBar, however it was highlighted in their code that this may be sub optimal.
 *
 * @param drawable The drawable to tileify
 * @param clip     Whether to clip drawable
 * @return The tiled drawable/*from  ww  w. j a v a 2  s  . c  o m*/
 */
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof LayerDrawable) {
        final LayerDrawable orig = (LayerDrawable) drawable;
        final int N = orig.getNumberOfLayers();
        final Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            final int id = orig.getId(i);
            outDrawables[i] = tileify(orig.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }

        final LayerDrawable clone = new LayerDrawable(outDrawables);
        for (int i = 0; i < N; i++) {
            clone.setId(i, orig.getId(i));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                clone.setLayerGravity(i, orig.getLayerGravity(i));
                clone.setLayerWidth(i, orig.getLayerWidth(i));
                clone.setLayerHeight(i, orig.getLayerHeight(i));
                clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i));
                clone.setLayerInsetRight(i, orig.getLayerInsetRight(i));
                clone.setLayerInsetTop(i, orig.getLayerInsetTop(i));
                clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i));
                clone.setLayerInsetStart(i, orig.getLayerInsetStart(i));
                clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i));
            }
        }

        return clone;
    }

    if (drawable instanceof BitmapDrawable) {
        final BitmapDrawable bitmap = (BitmapDrawable) drawable;

        final BitmapDrawable clone = (BitmapDrawable) bitmap.getConstantState().newDrawable();
        clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);

        if (clip) {
            return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL);
        } else {
            return clone;
        }
    }

    return drawable;
}