欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android开发之方形圆角listview代码分享

程序员文章站 2023-11-12 10:22:46
先看效果图: 首先,你得写一个类我们命名为cornerlistview [java] 复制代码 代码如下:/** * 圆角listview示例 ...

先看效果图:

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一样调用。