如何向我在 Android 中用作背景的可绘制对象添加圆角?

发布于 2024-09-07 07:11:17 字数 137 浏览 6 评论 0原文

我有一个特定的可绘制对象,在我的应用程序中用作背景。它不是纯色。
现在我想给这个可绘制对象添加圆角。

我只发现圆角可用于具有渐变或纯色作为背景的形状,但没有其他可绘制的形状。

是否有另一种简单的方法向可绘制对象添加圆角?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗忘曾经 2024-09-14 07:11:17

使用AQuery使可绘制或下载的图像变成圆角。

http://code.google.com/p/android-query/wiki/ImageLoading

注意: 这对于执行图像异步任务非常有效,并提供了很多功能。

从此处下载 API: http://code.google .com/p/android-query/downloads/list

代码来实现图像的圆角:

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);

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);
浅唱ヾ落雨殇 2024-09-14 07:11:17

你能使用“图层列表”绘图来完成你想要的事情吗?它允许您将形状和可绘制图形组合起来,如下所示:

<?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>

在本例中, shape_overlay 是:

<?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>

我使用它来在图像上获得“选定”效果。不过,它对我有用,因为相关图像是一个图标,并且背景与包含视图的背景颜色相同(两者都是白色)。

我看到的另一个选项是修改可绘制对象,使其具有圆角。

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.

耳钉梦 2024-09-14 07:11:17

对于我需要它的情况,我让它使用填充渐变且具有圆角的形状。这看起来有点像我想要达到的效果。

如果您有纹理或其他无法使用形状构建的复杂可绘制对象,则似乎您需要向项目中添加带有圆角的可绘制对象。或者自己构建圆角,就像这个 问题<中所解释的那样/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.

妖妓 2024-09-14 07:11:17

您可以使用您的九个补丁文件创建可绘制的。中心与您当前的背景相匹配,边缘和角落根据需要进行圆化。 Android 文档显示如何创建九个补丁文件< /a> 并且您可以使用绘制九个补丁工具 来构建它。

这就是 SDK 为弹出框等制作圆边、渐变背景样式的方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文