开发者

Android GradientDrawable - Corner Radii issue

开发者 https://www.devze.com 2023-01-28 14:06 出处:网络
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

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消