android开发之方形圆角listview代码分享
先看效果图:
首先,你得写一个类我们命名为cornerlistview
[java]
/**
* 圆角listview示例
* @description: 圆角listview示例
* @filename: cornerlistview.java
*/
public class cornerlistview extends listview {
public cornerlistview(context context) {
super(context);
}
public cornerlistview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
}
public cornerlistview(context context, attributeset attrs) {
super(context, attrs);
}
@override
public boolean onintercepttouchevent(motionevent ev) {
switch (ev.getaction()) {
case motionevent.action_down:
int x = (int) ev.getx();
int y = (int) ev.gety();
int itemnum = pointtoposition(x, y);
if (itemnum == adapterview.invalid_position)
break;
else{
if(itemnum==0){
if(itemnum==(getadapter().getcount()-1)){
setselector(r.drawable.<span style="color: #ff0000">app_list_corner_round</span>);
}else{
setselector(r.drawable.<span style="color: #ff0000">app_list_corner_round_top</span>);
}
}else if(itemnum==(getadapter().getcount()-1))
setselector(r.drawable.<span style="color: #ff0000">app_list_corner_round_bottom</span>);
else{
setselector(r.drawable.<span style="color: #ff0000">app_list_corner_shape</span>);
}
}
break;
case motionevent.action_up:
break;
}
return super.onintercepttouchevent(ev);
}
}
/**
* 圆角listview示例
* @description: 圆角listview示例
* @filename: cornerlistview.java
*/
public class cornerlistview extends listview {
public cornerlistview(context context) {
super(context);
}
public cornerlistview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
}
public cornerlistview(context context, attributeset attrs) {
super(context, attrs);
}
@override
public boolean onintercepttouchevent(motionevent ev) {
switch (ev.getaction()) {
case motionevent.action_down:
int x = (int) ev.getx();
int y = (int) ev.gety();
int itemnum = pointtoposition(x, y);
if (itemnum == adapterview.invalid_position)
break;
else{
if(itemnum==0){
if(itemnum==(getadapter().getcount()-1)){
setselector(r.drawable.app_list_corner_round);
}else{
setselector(r.drawable.app_list_corner_round_top);
}
}else if(itemnum==(getadapter().getcount()-1))
setselector(r.drawable.app_list_corner_round_bottom);
else{
setselector(r.drawable.app_list_corner_shape);
}
}
break;
case motionevent.action_up:
break;
}
return super.onintercepttouchevent(ev);
}
}
其中,app_list_corner_round
[html]
<span style="color: #333333"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startcolor="#bfeeff"
android:endcolor="#40b9ff"
android:angle="270"/>
<corners android:topleftradius="6dip"
android:toprightradius="6dip"
android:bottomleftradius="6dip"
android:bottomrightradius="6dip"/>
</shape> </span>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startcolor="#bfeeff"
android:endcolor="#40b9ff"
android:angle="270"/>
<corners android:topleftradius="6dip"
android:toprightradius="6dip"
android:bottomleftradius="6dip"
android:bottomrightradius="6dip"/>
</shape>
app_list_corner_round_top
[html]
<span style="color: #333333"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startcolor="#bfeeff"
android:endcolor="#40b9ff"
android:angle="270"/>
<corners android:topleftradius="6dip"
android:toprightradius="6dip"/>
</shape> </span>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startcolor="#bfeeff"
android:endcolor="#40b9ff"
android:angle="270"/>
<corners android:topleftradius="6dip"
android:toprightradius="6dip"/>
</shape>
app_list_corner_round_bottom
[html]
<span style="color: #333333"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startcolor="#bfeeff"
android:endcolor="#40b9ff"
android:angle="270"/>
<corners android:bottomleftradius="6dip"
android:bottomrightradius="6dip" />
</shape> </span>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startcolor="#bfeeff"
android:endcolor="#40b9ff"
android:angle="270"/>
<corners android:bottomleftradius="6dip"
android:bottomrightradius="6dip" />
</shape>
app_list_corner_shape
[html]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startcolor="#bfeeff"
android:endcolor="#40b9ff"
android:angle="270"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startcolor="#bfeeff"
android:endcolor="#40b9ff"
android:angle="270"/>
</shape>
写好了之后,就可以在你的代码中直接像listview一样调用。