Example usage for android.graphics.drawable LayerDrawable getNumberOfLayers

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

Introduction

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

Prototype

public int getNumberOfLayers() 

Source Link

Document

Returns the number of layers contained within this layer drawable.

Usage

From source file:Main.java

public static Drawable getLastDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }/* w ww .j  av a 2  s. c  o m*/

    if (!(drawable instanceof LayerDrawable)) {
        return drawable;
    }

    LayerDrawable layerDrawable = (LayerDrawable) drawable;
    for (int i = layerDrawable.getNumberOfLayers() - 1; i >= 0; i--) {
        Drawable childDrawable = getLastDrawable(layerDrawable.getDrawable(i));
        if (childDrawable != null) {
            return childDrawable;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the View's {@link ColorDrawable} background in case it has one.
 * The {@link ColorDrawable} may be set directly as the View's background,
 * or nested within a {@link LayerDrawable}. In case of a
 * {@link LayerDrawable}, the method will return the first color-drawable it
 * finds./*  ww  w  .  j a v a 2s. c o  m*/
 * 
 * @param view
 * @return A {@link ColorDrawable}, or <code>null</code> in case not found.
 */
private static ColorDrawable getColorDrawableBackground(View view) {
    if (view != null) {
        Drawable background = view.getBackground();
        if (background instanceof ColorDrawable) {
            return (ColorDrawable) background;
        }
        if (background instanceof LayerDrawable) {
            LayerDrawable layeredBG = (LayerDrawable) background;
            int numberOfLayers = layeredBG.getNumberOfLayers();
            for (int i = 0; i < numberOfLayers; i++) {
                if (layeredBG.getDrawable(i) instanceof ColorDrawable) {
                    return (ColorDrawable) layeredBG.getDrawable(i);
                }
            }
        }
    }
    return null;
}

From source file:com.android.tv.guide.ProgramItemView.java

private static void setProgress(Drawable drawable, int id, int progress) {
    if (drawable instanceof StateListDrawable) {
        StateListDrawable stateDrawable = (StateListDrawable) drawable;
        for (int i = 0; i < getStateCount(stateDrawable); ++i) {
            setProgress(getStateDrawable(stateDrawable, i), id, progress);
        }/*w w  w.  ja v a  2s  .  c o m*/
    } else if (drawable instanceof LayerDrawable) {
        LayerDrawable layerDrawable = (LayerDrawable) drawable;
        for (int i = 0; i < layerDrawable.getNumberOfLayers(); ++i) {
            setProgress(layerDrawable.getDrawable(i), id, progress);
            if (layerDrawable.getId(i) == id) {
                layerDrawable.getDrawable(i).setLevel(progress);
            }
        }
    }
}

From source file:com.quran.labs.androidquran.ui.preference.SeekBarPreference.java

private void styleSeekBar() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        final Drawable progressDrawable = mSeekBar.getProgressDrawable();
        if (progressDrawable != null) {
            if (progressDrawable instanceof LayerDrawable) {
                LayerDrawable ld = (LayerDrawable) progressDrawable;
                int layers = ld.getNumberOfLayers();
                for (int i = 0; i < layers; i++) {
                    ld.getDrawable(i).mutate().setColorFilter(mTintColor, PorterDuff.Mode.SRC_ATOP);
                }/*from ww w . ja  v a 2s . c  o  m*/
            } else {
                progressDrawable.mutate().setColorFilter(mTintColor, PorterDuff.Mode.SRC_ATOP);
            }
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            final Drawable thumb = mSeekBar.getThumb();
            if (thumb != null) {
                thumb.mutate().setColorFilter(mTintColor, PorterDuff.Mode.SRC_ATOP);
            }
        }
    }
}

From source file:android.support.v7.widget.AppCompatProgressBarHelper.java

/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 *///  w ww.  ja  v  a 2  s  .c o  m
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof DrawableWrapper) {
        Drawable inner = ((DrawableWrapper) drawable).getWrappedDrawable();
        if (inner != null) {
            inner = tileify(inner, clip);
            ((DrawableWrapper) drawable).setWrappedDrawable(inner);
        }
    } else if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

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

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    } else if (drawable instanceof BitmapDrawable) {
        final BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        final Bitmap tileBitmap = bitmapDrawable.getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT,
                Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        shapeDrawable.getPaint().setColorFilter(bitmapDrawable.getPaint().getColorFilter());
        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}

From source file:android.support.v7.internal.widget.TintRatingBar.java

/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */// ww w.ja v a 2s.  com
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof DrawableWrapper) {
        Drawable inner = ((DrawableWrapper) drawable).getWrappedDrawable();
        if (inner != null) {
            inner = tileify(inner, clip);
            ((DrawableWrapper) drawable).setWrappedDrawable(inner);
        }
    } else if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

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

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    } else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT,
                Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}

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 2s  .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;
}

From source file:com.lamcreations.scaffold.common.views.RoundedImageView.java

private void updateAttrs(Drawable drawable) {
    if (drawable == null) {
        return;//  ww w .j  a va 2s  . c om
    }

    if (drawable instanceof RoundedDrawable) {
        ((RoundedDrawable) drawable).setScaleType(mScaleType).setBorderWidth(mBorderWidth)
                .setBorderColor(mBorderColor).setOval(mIsOval).setTileModeX(mTileModeX)
                .setTileModeY(mTileModeY);

        if (mCornerRadii != null) {
            ((RoundedDrawable) drawable).setCornerRadius(mCornerRadii[Corner.TOP_LEFT],
                    mCornerRadii[Corner.TOP_RIGHT], mCornerRadii[Corner.BOTTOM_RIGHT],
                    mCornerRadii[Corner.BOTTOM_LEFT]);
        }

        applyColorMod();
    } else if (drawable instanceof LayerDrawable) {
        // loop through layers to and set drawable attrs
        LayerDrawable ld = ((LayerDrawable) drawable);
        for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
            updateAttrs(ld.getDrawable(i));
        }
    }
}

From source file:com.landenlabs.all_devtool.IconBaseFragment.java

/**
 * Display icon (drawable) information//from   w  w w . j a  v a  2  s .  com
 *
 * @param iconInfo
 */
private void showIconDialog(IconInfo iconInfo) {
    Drawable iconD = iconInfo.getDrawable();
    String iconType = iconD.getClass().getSimpleName();

    LayoutInflater inflater = m_context.getLayoutInflater();
    final View dialogLayout = inflater.inflate(R.layout.icon_dlg, null);

    View shareBtn = dialogLayout.findViewById(R.id.icon_dlg_share);
    shareBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Utils.shareScreen(dialogLayout, "iconDetail", null);
        }
    });

    final TextView imageName = Ui.viewById(dialogLayout, R.id.icon_dlg_name);
    final TextView imageSize = Ui.viewById(dialogLayout, R.id.icon_dlg_size);
    final TextView imageType = Ui.viewById(dialogLayout, R.id.icon_dlg_type);
    final TextView imageExtra = Ui.viewById(dialogLayout, R.id.icon_dlg_extra);

    imageName.setText(iconInfo.fieldStr());
    imageSize.setText(String.format("Size: %d x %d", iconD.getIntrinsicWidth(), iconD.getIntrinsicHeight()));
    imageType.setText(iconType);

    final ImageView imageView = Ui.viewById(dialogLayout, R.id.icon_dlg_image);
    // imageView.setImageDrawable(iconD);
    boolean hasStates = iconD.isStateful();

    final View stateTitle = dialogLayout.findViewById(R.id.icon_dlg_state_title);
    stateTitle.setVisibility(hasStates ? View.VISIBLE : View.GONE);

    final TableRow row1 = Ui.viewById(dialogLayout, R.id.icon_dlg_state_row1);
    row1.removeAllViews();

    final TableRow row2 = Ui.viewById(dialogLayout, R.id.icon_dlg_state_row2);
    row2.removeAllViews();

    boolean showRows = false;
    String extraInfo = "";

    if (hasStates) {
        extraInfo = "StateFul";
        showRows = true;

        StateListDrawable stateListDrawable = (StateListDrawable) iconD;
        Set<Drawable> stateIcons = new HashSet<Drawable>();
        showStateIcon(imageView, row1, row2, stateListDrawable, android.R.attr.state_enabled, "Enabled",
                stateIcons);
        showStateIcon(imageView, row1, row2, stateListDrawable, android.R.attr.state_pressed, "Pressed",
                stateIcons);
        showStateIcon(imageView, row1, row2, stateListDrawable, android.R.attr.state_checked, "Checked",
                stateIcons);
        showStateIcon(imageView, row1, row2, stateListDrawable, android.R.attr.state_selected, "Selected",
                stateIcons);
    }

    if (iconType.equals(LayerDrawable.class.getSimpleName())) {
        showRows = true;
        LayerDrawable layerDrawable = (LayerDrawable) iconD;
        int layerCnt = layerDrawable.getNumberOfLayers();
        extraInfo = String.format(Locale.getDefault(), "Layers:%d", layerCnt);
        for (int layerIdx = 0; layerIdx < Math.min(layerCnt, 3); layerIdx++) {
            showLayerIcon(imageView, row1, row2, layerDrawable.getDrawable(layerIdx), layerIdx);
        }
    } else if (iconType.equals(AnimationDrawable.class.getSimpleName())) {
        final AnimationDrawable animationDrawable = (AnimationDrawable) iconD;
        extraInfo = String.format(Locale.getDefault(), "Frames:%d", animationDrawable.getNumberOfFrames());
        showRows = true;
        showAnimationBtns(imageView, animationDrawable, row1, row2);

        // Can't control animation at this time, drawable not rendered yet.
        // animationDrawable.stop();
    }

    row1.setVisibility(showRows ? View.VISIBLE : View.GONE);
    row2.setVisibility(showRows ? View.VISIBLE : View.GONE);

    imageExtra.setText(extraInfo);
    imageView.setImageDrawable(iconD);

    dialogLayout.findViewById(R.id.icon_dlg_whiteBtn).setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            imageView.setBackgroundDrawable(v.getBackground());
        }
    });

    dialogLayout.findViewById(R.id.icon_dlg_grayBtn).setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            imageView.setBackgroundDrawable(v.getBackground());
        }
    });

    dialogLayout.findViewById(R.id.icon_dlg_blackBtn).setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            imageView.setBackgroundDrawable(v.getBackground());
        }
    });

    dialogLayout.findViewById(R.id.icon_dlg_squaresBtn).setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            imageView.setBackgroundDrawable(v.getBackground());
        }
    });

    AlertDialog.Builder builder = new AlertDialog.Builder(m_context);
    builder.setView(dialogLayout);

    builder.setMessage("Icon").setCancelable(false).setPositiveButton("Close",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, close
                    dialog.cancel();
                }
            });

    builder.show();
}

From source file:it.ndorigatti.android.view.MulticolorProgressBar.java

/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 *///  w w w. ja  va2s. com
private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

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

        LayerDrawable newBg = new LayerDrawable(outDrawables);
        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }
        return newBg;
    }
    return drawable;
}