
方法一:定义
1 | <?xml version ="1.0" encoding ="UTF-8" ?> |
2 | <shape xmlns:android ="http://schemas.android.com/apk/res/android" android:shape ="rectangle" > |
3 | <gradient android:startColor ="#B0BCCD" android:endColor ="#6D84A2" android:angle ="270" /> |
4 | <corners android:bottomRightRadius ="8dp" android:bottomLeftRadius ="8dp" android:topLeftRadius ="8dp" |
5 | android:topRightRadius ="8dp" /> |
布局文件中的ListView属性android:listSelector="@drawable/shape"
存在问题是,ListView最上面或者最下面的item选中时,圆角背景变成直角的
方法二:修改选中item
01 | public class RoundedRectListView extends ListView { |
02 | private Context mContext; |
03 | public RoundedRectListView( Context context) { |
05 | this .mContext = context; |
08 | public RoundedRectListView( Context context, AttributeSet attrs) { |
09 | super ( context, attrs) ; |
10 | this .mContext = context; |
13 | public RoundedRectListView( Context context, AttributeSet attrs, int defStyle) { |
14 | super ( context, attrs, defStyle) ; |
15 | this .mContext = context; |
18 | protected void init( ) { |
19 | setBackgroundDrawable( mContext.getResources ( ) .getDrawable ( R.drawable .background_rounded_white ) ) ; |
20 | setCacheColorHint( Color .WHITE ) ; |
21 | setFooterDividersEnabled( false ) ; |
24 | public boolean onInterceptTouchEvent( MotionEvent ev) { |
26 | switch ( ev.getAction ( ) ) { |
27 | case MotionEvent.ACTION_DOWN : |
28 | int x = ( int ) ev.getX ( ) ; |
29 | int y = ( int ) ev.getY ( ) ; |
30 | int itemnum = pointToPosition( x, y) ; |
31 | if ( itemnum == AdapterView.INVALID_POSITION ) |
36 | if ( itemnum== ( getAdapter( ) .getCount ( ) - 1) ) |
37 | setSelector( R.drawable .listview_selection_shade_rounded ) ; |
39 | setSelector( R.drawable .listview_selection_shade_top_rounded ) ; |
41 | else if ( itemnum== ( getAdapter( ) .getCount ( ) - 1) ) |
42 | setSelector( R.drawable .listview_selection_shade_bottom_rounded ) ; |
44 | setSelector( R.drawable .listview_selection_shade ) ; |
47 | case MotionEvent.ACTION_UP : |
方法三:定义selector
01 | public class RoundedRectListView extends ListView { |
02 | private Context mContext; |
04 | public RoundedRectListView( Context context) { |
06 | this .mContext = context; |
09 | public RoundedRectListView( Context context, AttributeSet attrs) { |
10 | super ( context, attrs) ; |
11 | this .mContext = context; |
14 | public RoundedRectListView( Context context, AttributeSet attrs, int defStyle) { |
15 | super ( context, attrs, defStyle) ; |
16 | this .mContext = context; |
19 | protected void init( ) { |
20 | setBackgroundDrawable( mContext.getResources ( ) .getDrawable ( R.drawable .background_rounded_white ) ) ; |
21 | setCacheColorHint( Color .WHITE ) ; |
22 | setFooterDividersEnabled( false ) ; |
25 | public boolean onInterceptTouchEvent( MotionEvent ev) { |
27 | switch ( ev.getAction ( ) ) { |
28 | case MotionEvent.ACTION_DOWN : |
29 | int x = ( int ) ev.getX ( ) ; |
30 | int y = ( int ) ev.getY ( ) ; |
31 | int itemnum = pointToPosition( x, y) ; |
33 | if ( itemnum == AdapterView.INVALID_POSITION ) |
38 | if ( itemnum== ( getAdapter( ) .getCount ( ) - 1) ) |
39 | setSelector( R.drawable .listview_selection_shade_rounded ) ; |
41 | setSelector( R.drawable .listview_selection_shade_top_rounded ) ; |
43 | else if ( itemnum== ( getAdapter( ) .getCount ( ) - 1) ) |
44 | setSelector( R.drawable .listview_selection_shade_bottom_rounded ) ; |
46 | setSelector( R.drawable .listview_selection_shade ) ; |
49 | case MotionEvent.ACTION_UP : |
方法四:方法二和方法三的结合
001 | public class RoundedRectListView extends ListView { |
003 | private Context mContext; |
005 | public RoundedRectListView( Context context) { |
007 | this .mContext = context; |
010 | public RoundedRectListView( Context context, AttributeSet attrs) { |
011 | super ( context, attrs) ; |
012 | this .mContext = context; |
015 | public RoundedRectListView( Context context, AttributeSet attrs, int defStyle) { |
016 | super ( context, attrs, defStyle) ; |
017 | this .mContext = context; |
020 | protected void init( ) { |
021 | setBackgroundDrawable( mContext.getResources ( ) .getDrawable ( R.drawable .background_rounded_white ) ) ; |
022 | setCacheColorHint( Color .WHITE ) ; |
023 | setFooterDividersEnabled( false ) ; |
024 | setSelector( new Selector( RoundedRectListView.this ,- 1) ) ; |
027 | public boolean onInterceptTouchEvent( MotionEvent ev) { |
028 | switch ( ev.getAction ( ) ) { |
029 | case MotionEvent.ACTION_DOWN : |
030 | int x = ( int ) ev.getX ( ) ; |
031 | int y = ( int ) ev.getY ( ) ; |
032 | int itemnum = pointToPosition( x, y) ; |
033 | if ( itemnum == AdapterView.INVALID_POSITION ) |
037 | setSelector( new Selector( RoundedRectListView.this ,itemnum) ) ; |
040 | case MotionEvent.ACTION_UP : |
045 | class Selector extends Drawable { |
046 | private static final String TAG = "Selector" ; |
047 | private Paint mPaint; |
048 | private AdapterView mList; |
049 | private RectF mRectF; |
050 | private int position; |
052 | public Selector( AdapterView list,int position) { |
054 | mPaint = new Paint ( ) ; |
055 | mRectF = new RectF( ) ; |
056 | this .position = position |
058 | LinearGradient g= new LinearGradient( mRectF.top ,mRectF.left ,mRectF.right ,mRectF.bottom ,Color .parseColor ( "#058cf5" ) ,Color .parseColor ( "#015fe6" ) ,TileMode.REPEAT ) ; |
060 | mPaint.setShader ( g) ; |
063 | public void draw( Canvas canvas) { |
064 | Rect b = getBounds( ) ; |
065 | int mPosition = mList.getSelectedItemPosition ( ) ; |
066 | if ( mPosition==- 1) { |
069 | Log.d ( TAG, "Position :" + mPosition) ; |
071 | canvas.clipRect ( b.left , b.top , b.right , ( b.bottom + b.top ) / 2) ; |
072 | drawHalf( canvas, b, mPosition == 0) ; |
075 | canvas.clipRect ( b.left , ( b.bottom + b.top ) / 2, b.right , b.bottom ) ; |
076 | drawHalf( canvas, b, mPosition == mList.getAdapter ( ) .getCount ( ) - 1 && b.bottom == mList.getHeight ( ) ) ; |
078 | Log.d ( TAG, "draw " + b) ; |
080 | private void drawHalf( Canvas canvas, Rect b ,boolean round) { |
084 | canvas.drawRoundRect ( mRectF, 10, 10, mPaint) ; |
086 | canvas.drawRect ( b, mPaint) ; |
090 | public int getOpacity( ) { |
094 | public void setAlpha( int alpha) { |
097 | public void setColorFilter( ColorFilter cf) { |
方法五:
01 | public class RoundedRectListView extends ListView { |
02 | private static final float RADIUS = 16 ; |
04 | public RoundedRectListView( Context context, AttributeSet attrs) { |
05 | super ( context, attrs) ; |
08 | private void init( ) { |
09 | GradientDrawable gd = new GradientDrawable( ) ; |
10 | gd.setCornerRadius ( RADIUS) ; |
11 | gd.setColor ( 0xff208020) ; |
12 | setBackgroundDrawable( gd) ; |
13 | setCacheColorHint( 0) ; |
14 | setVerticalFadingEdgeEnabled( false ) ; |
15 | StateListDrawable sld = new StateListDrawable( ) ; |
17 | PRESSED_ENABLED_STATE_SET, |
18 | new GradientDrawable( Orientation.LEFT_RIGHT , new int [ ] { 0xffa58cf5, 0xffa13f99} ) ) ; |
21 | new GradientDrawable( Orientation.LEFT_RIGHT , new int [ ] { 0xff058cf5, 0xff013f99} ) ) ; |
25 | protected void onSizeChanged( int w, int h, int oldw, int oldh) { |
26 | super .onSizeChanged ( w, h, oldw, oldh) ; |
28 | RectF rect = new RectF( 0, 0, w, h) ; |
29 | mClip.addRoundRect ( rect, RADIUS, RADIUS, Direction.CW ) ; |
32 | protected void dispatchDraw( Canvas canvas) { |
34 | canvas.clipPath ( mClip) ; |
35 | super .dispatchDraw ( canvas) ; |
第五种方法最好