add LayerDrawable - Android Graphics

Android examples for Graphics:Drawable

Description

add LayerDrawable

Demo Code


//package com.book2s;
import android.graphics.drawable.Drawable;

import android.graphics.drawable.LayerDrawable;

import android.view.View;

public class Main {
    static LayerDrawable addLayer(LayerDrawable layerDrawable,
            Drawable drawable) {/*from w ww. j  a va2  s.co  m*/
        final int n = layerDrawable.getNumberOfLayers() + 1;
        final Drawable[] drawables = new Drawable[n];
        for (int i = 0; i < n - 1; i++) {
            drawables[i] = layerDrawable.getDrawable(i);
        }
        drawables[n - 1] = drawable;
        final LayerDrawable newLayerDrawable = new LayerDrawable(drawables);
        for (int i = 0; i < n - 1; i++) {
            if (layerDrawable.getId(i) != View.NO_ID) {
                newLayerDrawable.setId(i, layerDrawable.getId(i));
            }
        }
        return newLayerDrawable;
    }

    static LayerDrawable addLayer(LayerDrawable layerDrawable,
            Drawable drawable, int id) {
        LayerDrawable newLayerDrawable = addLayer(layerDrawable, drawable);
        newLayerDrawable
                .setId(newLayerDrawable.getNumberOfLayers() - 1, id);
        return newLayerDrawable;
    }
}

Related Tutorials