问题
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?
回答1:
Use AQuery to make Drawable or Downloaded images rounded corners.
http://code.google.com/p/android-query/wiki/ImageLoading
Note : This is very effective in doing asynchronous task with images and provides lots of featres.
Download API from here : http://code.google.com/p/android-query/downloads/list
Code to do rounded Corners of an image :
AQuery aq = new AQuery(this);
// Image URL to download
String url = "http://www.vikispot.com/z/images/vikispot/android-w.png";
ImageOptions options = new ImageOptions();
options.round = 15;
aq.id(R.id.image).image(url, options);
回答2:
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">
<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">
<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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/3150514/how-to-add-rounded-corner-to-a-drawable-im-using-as-a-background-in-android