Example usage for android.graphics.drawable LayerDrawable setLayerGravity

List of usage examples for android.graphics.drawable LayerDrawable setLayerGravity

Introduction

In this page you can find the example usage for android.graphics.drawable LayerDrawable setLayerGravity.

Prototype

public void setLayerGravity(int index, int gravity) 

Source Link

Document

Sets the gravity used to position or stretch the specified layer within its container.

Usage

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 ava2 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;
}