Android GradientDrawable - 角半径问题

发布于 10-06 02:05 字数 982 浏览 4 评论 0原文

我正在尝试调试 ExpandableListAdapter.getChildView 遇到的问题。

我定义了一个 Drawable,其形状包含渐变和半径为 1 的角作为列表项的背景 - 没有什么特别的。

然后,在我的适配器代码中,我在 getChildView 中有以下代码片段:

GradientDrawable background = (GradientDrawable) convertView.getBackground();
float topRadius = 0;
float bottomRadius = 0;

// Make the corner radius obvious for debugging             
if (childPosition == 0)
    topRadius = 14;
if (childPosition == (mValues.size() - 1))
    bottomRadius = 14;

background.setCornerRadii(new float [] { topRadius, topRadius, 
                                         topRadius, topRadius,  
                                         bottomRadius, bottomRadius, 
                                         bottomRadius, bottomRadius});
convertView.setBackgroundDrawable(background);

这里的尝试是舍入第一个列表项的顶部和最后一个列表项的底部。通过调试,我似乎正在为我想要的项目设置我想要的值。

但是,我遇到的问题是为所有列表项设置角半径,就好像它是底部项一样。

顺便说一句,有没有办法获得 GradientDrawable 的角半径,至少出于调试目的?

谢谢,
工作时间

I am trying to debug a problem I am having with ExpandableListAdapter.getChildView.

I have defined a Drawable with a shape containing a gradient and a corner with a radius of 1 for the background of the list item - nothing special there.

Then, in my adapter code, I have this snippet within getChildView:

GradientDrawable background = (GradientDrawable) convertView.getBackground();
float topRadius = 0;
float bottomRadius = 0;

// Make the corner radius obvious for debugging             
if (childPosition == 0)
    topRadius = 14;
if (childPosition == (mValues.size() - 1))
    bottomRadius = 14;

background.setCornerRadii(new float [] { topRadius, topRadius, 
                                         topRadius, topRadius,  
                                         bottomRadius, bottomRadius, 
                                         bottomRadius, bottomRadius});
convertView.setBackgroundDrawable(background);

The attempt here is to round the top of the first list item, and the bottom of the last list item. Via debugging, it would appear that I am setting the values I want for the items that I want.

However, the problem I'm having is that the corner radii are being set for all list items as if it were the bottom item.

As a slight aside, is there a way to get the corner radii of the GradientDrawable, at least for debugging purposes?

Thanks,
wTs

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

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

发布评论

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

评论(2

春庭雪2024-10-13 02:05:15

不要调用 setCornerRadii(),而是调用 mutate().setCornerRadii(),这应该可以解决您的问题(完整说明位于 http://www.curious-creature.org/2009/05/02/drawable-mutations/)

编辑(Wonko)< br>
这几乎是正确的(并且提供的链接基本上解释了原因)。唯一的变化是 mutate() 返回一个 Drawable,它没有 setCornerRadii() 方法。相反,您需要将变异对象转换回 GradientDrawable,它的工作方式就像一个魅力。

((GradientDrawable) background.mutate()).setCornerRadii(
    new float [] { topRadius, topRadius, topRadius, topRadius,  
                   bottomRadius, bottomRadius, bottomRadius, bottomRadius});

Instead of calling setCornerRadii(), call mutate().setCornerRadii(), that should fix your issue (full explanation available at http://www.curious-creature.org/2009/05/02/drawable-mutations/)

Edit (by Wonko)
This is almost correct (and the link provided does basically explain why). The only change is that mutate() returns a Drawable, which does not have a setCornerRadii() method. Instead, you need to cast the mutated object back to a GradientDrawable, and it works like a charm.

((GradientDrawable) background.mutate()).setCornerRadii(
    new float [] { topRadius, topRadius, topRadius, topRadius,  
                   bottomRadius, bottomRadius, bottomRadius, bottomRadius});
空城仅有旧梦在2024-10-13 02:05:15

编辑:刚刚意识到这并不能解决OP的问题。

除了 Mathias 的评论之外,解决此问题的方法是首先将半径设置为 1,然后设置各个角半径:

background.setCornerRadius(1.0f);
background.setCornerRadii(new float [] { topRadius, topRadius,  
                                         topRadius, topRadius,   
                                         bottomRadius, bottomRadius,  
                                         bottomRadius, bottomRadius}); 

如果这可以解决问题,请告诉我。至于获得角半径,我不知道有什么方法可以做到这一点......

EDIT: Just realized this isn't a fix for the OP's problem.

In addition to Mathias's comment, a fix for this is to first set the radius to 1, then set the individual corner radii:

background.setCornerRadius(1.0f);
background.setCornerRadii(new float [] { topRadius, topRadius,  
                                         topRadius, topRadius,   
                                         bottomRadius, bottomRadius,  
                                         bottomRadius, bottomRadius}); 

Let me know if this fixes the problem. As far as getting the corner radii, I don't know of any way to do so...

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