androidandroid-imageviewlayerlayerdrawable

Display images layer by layer in ImageView not works


In my application I have a set of drawbles. I have to show it as layer by layer. For that I am using LayerDrawble. I have used it in this way

ImageView

     image = (ImageView)findViewById(R.id.image);                

        Resources r = getResources();
        Drawable[] capas = new Drawable[3];

        capas[0] = r.getDrawable(R.drawable.icon);
        capas[1] = r.getDrawable(R.drawable.icon2);
        capas[2] = r.getDrawable(R.drawable.icon3);           

        LayerDrawable capasDrawable = new LayerDrawable(capas);
        image.setImageDrawable(capasDrawable);

But it displays the top most image only. That means it not shows the whole 3 images layer by layer.

How do i display it as layet by layer

Update I need a view like this, the first and last images aligned in layers.

in this image it shows multiples layers

I have done aa jason posted in answer. The image shows layer by layer. But its bottom part has some weird look.. See the screen shot. And how do i make it correct enter image description here


Solution

  • I believe you want to use setLayerInset.

    setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset)

    I'm not sure about the positioning, you'll have to tweak that.

        ImageView image = (ImageView)findViewById(R.id.image);                
    
        Resources r = getResources();
        Drawable[] capas = new Drawable[3];
    
        capas[0] = r.getDrawable(R.drawable.icon);
        capas[1] = r.getDrawable(R.drawable.icon2);
        capas[2] = r.getDrawable(R.drawable.icon3);           
    
        LayerDrawable capasDrawable = new LayerDrawable(capas);
    
        // Set bottom layer inset
        capasDrawable.setLayerInset(0, 5, 0, 0, 5);
    
        // Set middle layer inset
        capasDrawable.setLayerInset(1, 10, 0, 0, 10);
    
        // Set top layer inset
        capasDrawable.setLayerInset(2, 15, 0, 0, 15);
    
        image.setImageDrawable(capasDrawable);