views:

91

answers:

3

I have a specific drawable that I use as background in my app. It is not a solid color.
Now I want to add rounded corners to this drawable.

I only found the rounded corner available in a shape with a gradient or a solid color as a background but not another drawable.

Is there another easy way of adding rounded corners to a drawable?

A: 

Could you accomplish what you want using a "layer-list" drawable? It allows you to combine a shape and a graphic drawable like so:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt;
        <item android:drawable="@drawable/icon_home_button_img"/>
        <item android:drawable="@drawable/icon_home_shape_overlay"/>
</layer-list>

In this case, shape_overlay is:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <solid android:color="#60000000"/>
    <stroke android:width="3dp" color="#ff000000"/>
    <corners android:radius="10dp" />
</shape>

I'm using this to get a "selected" effect on an image. It works for me, though, because the image in question is an icon and the background is the same color as the containing view's background (both are white).

The other option I see is to modify the drawable so it has rounded corners.

ydant
It is a nice idea but it is not working for me. I would need the corners be a solid color and the inner color of the drawable must be perfectly transparent, I haven't figured out how to do that.
Janusz
A: 

For the case where I needed it I got it working with a shape that was filled with a gradient and had round corners. This looks a bit like the effect I wanted to achieve.

If you have a Texture or some other complex drawable that you can't build with shapes it seems you need to add a drawable with the rounded corners to your project. Or build the round corners yourself like it is explained in this question.

Janusz
A: 

You could create a Nine-patch file with your drawable. With the center matching your current background and the edges and corners rounded, as needed. The Android documentation shows how to create Nine-patch files and you may be able to use the Draw nine-patch tool to build it.

This is how the SDK makes rounded-edge, gradient background styles for popup boxes and such.

jwadsack
this is possible but I have to create the round corners as graphics and if the resolution changes the corners will look smaller or bigger because they won't change their size. With a shape I could specify the angle of the corner in DIP.
Janusz