Android GradientDrawable - 角半径问题
我正在尝试调试 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 技术交流群。
发布评论
评论(2)
编辑:刚刚意识到这并不能解决OP的问题。
除了 Mathias 的评论之外,解决此问题的方法是首先将半径设置为 1,然后设置各个角半径:
background.setCornerRadius(1.0f);
background.setCornerRadii(new float [] { topRadius, topRadius,
topRadius, topRadius,
bottomRadius, bottomRadius,
bottomRadius, bottomRadius});
如果这可以解决问题,请告诉我。至于获得角半径,我不知道有什么方法可以做到这一点......
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
不要调用 setCornerRadii(),而是调用 mutate().setCornerRadii(),这应该可以解决您的问题(完整说明位于 http://www.curious-creature.org/2009/05/02/drawable-mutations/)
编辑(Wonko)< br>
这几乎是正确的(并且提供的链接基本上解释了原因)。唯一的变化是 mutate() 返回一个 Drawable,它没有 setCornerRadii() 方法。相反,您需要将变异对象转换回 GradientDrawable,它的工作方式就像一个魅力。
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.