Android 圆角的 ListView

圆角ListView效果如下:


实现圆角的ListView需要重写ListView控件,代码如下:


 1 public class RoundedRectListView extends ListView {
2 // 设置圆角的值
3 private static final float RADIUS = 8;
4 private Path mClip;
5
6
7 public RoundedRectListView(Context context, AttributeSet attrs) {
8 super(context, attrs);
9 init();
10 }
11
12
13 private void init() {
14 // 设置背景颜色
15 // GradientDrawable gd = new GradientDrawable();
16 // gd.setCornerRadius(RADIUS);
17 // gd.setColor(0x7f05000e);
18 //
19 // setBackgroundDrawable(gd);
20 // setCacheColorHint( 0) ;
21 //
22 setVerticalFadingEdgeEnabled(false);
23 // 设置选中样式
24 StateListDrawable sld = new StateListDrawable();
25 //设置选中项的颜色渐变
26 sld.addState(PRESSED_ENABLED_STATE_SET, new GradientDrawable(
27 Orientation.LEFT_RIGHT, new int[] { 0xffa58cf5, 0xffa13f99 }));
28 sld.addState(EMPTY_STATE_SET, new GradientDrawable(
29 Orientation.LEFT_RIGHT, new int[] { 0xff058cf5, 0xff013f99 }));
30 setSelector(sld);
31 }
32
33
34 @Override
35 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
36 super.onSizeChanged(w, h, oldw, oldh);
37 mClip = new Path();
38 RectF rect = new RectF(0, 0, w, h);
39 mClip.addRoundRect(rect, RADIUS, RADIUS, Direction.CW);
40 }
41
42
43 @Override
44 protected void dispatchDraw(Canvas canvas) {
45 canvas.save();
46 canvas.clipPath(mClip);
47 super.dispatchDraw(canvas);
48 canvas.restore();
49 }
50 }



posted @ 2012-03-07 00:42  hejiyong  阅读(199)  评论(0)    收藏  举报