首页 > 代码库 > 类似QQ在线离线好友界面

类似QQ在线离线好友界面

把头像设置成圆形的代码如下:

  1 package com.example.lesson6_11_id19;
  2 
  3 import android.content.Context;
  4 import android.content.res.TypedArray;
  5 import android.graphics.Bitmap;
  6 import android.graphics.Canvas;
  7 import android.graphics.Color;
  8 import android.graphics.Paint;
  9 import android.graphics.PorterDuff;
 10 import android.graphics.PorterDuffXfermode;
 11 import android.graphics.RectF;
 12 import android.graphics.Xfermode;
 13 import android.graphics.drawable.Drawable;
 14 import android.graphics.drawable.NinePatchDrawable;
 15 import android.util.AttributeSet;
 16 import android.widget.ImageView;
 17 
 18 public class CircleImageView extends ImageView {
 19     private static final Xfermode MASK_XFERMODE;
 20     private Bitmap mask;
 21     private Paint paint; 
 22     private int mBorderWidth = 10;
 23     private int mBorderColor = Color.parseColor("#f2f2f2");
 24     private boolean useDefaultStyle = false;
 25     
 26     static {
 27         PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;
 28         MASK_XFERMODE = new PorterDuffXfermode(localMode);
 29     }
 30     
 31     public CircleImageView(Context context) {
 32         super(context);
 33     }
 34 
 35     public CircleImageView(Context context, AttributeSet attrs) {
 36         this(context, attrs, 0);
 37     }
 38     
 39     public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
 40         super(context, attrs, defStyle);
 41         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircularImage);
 42         mBorderColor = a.getColor(R.styleable.CircularImage_border_color, mBorderColor);
 43         final int def = (int) (2*context.getResources().getDisplayMetrics().density + 0.5f);
 44         mBorderWidth = a.getDimensionPixelOffset(R.styleable.CircularImage_border_width, def);
 45         a.recycle();        
 46     }
 47 
 48     private void useDefaultStyle(boolean useDefaultStyle) {
 49         this.useDefaultStyle = useDefaultStyle;
 50     }
 51     
 52     @Override
 53     protected void onDraw(Canvas canvas) {
 54         if(useDefaultStyle) {
 55             super.onDraw(canvas);
 56             return ;
 57         }
 58         final Drawable localDraw = getDrawable();
 59         if(localDraw == null) {
 60             return ;
 61         }
 62         if(localDraw instanceof NinePatchDrawable) {
 63             return ;
 64         }
 65         if (this.paint == null) {  
 66             final Paint localPaint = new Paint();  
 67             localPaint.setFilterBitmap(false);  
 68             localPaint.setAntiAlias(true);  
 69             localPaint.setXfermode(MASK_XFERMODE);  
 70             this.paint = localPaint;  
 71         }  
 72         final int width = getWidth();  
 73         final int height = getHeight();  
 74         /** ????layer */  
 75         int layer = canvas.saveLayer(0.0F, 0.0F, width, height, null, 31);  
 76         /** ????drawable???С */  
 77         localDraw.setBounds(0, 0, width, height);  
 78         /** ??drawable????bitmap(this.mask)???棨drawable??????bitmap????????? */  
 79         localDraw.draw(canvas);  
 80         if ((this.mask == null) || (this.mask.isRecycled())) {  
 81             this.mask = createOvalBitmap(width, height);  
 82         }  
 83         /** ??bitmap????canvas???? */  
 84         canvas.drawBitmap(this.mask, 0.0F, 0.0F, this.paint);  
 85         /** ???????????layer?? */  
 86         canvas.restoreToCount(layer);  
 87         drawBorder(canvas, width, height); 
 88     }
 89     
 90     /**
 91      * ??????α??
 92      */
 93     private void drawBorder(Canvas canvas, final int width, final int height) {
 94         if(mBorderWidth == 0) {
 95             return ;
 96         }
 97         final Paint mBorderPaint = new Paint();
 98         mBorderPaint.setStyle(Paint.Style.STROKE);
 99         mBorderPaint.setAntiAlias(true);
100         mBorderPaint.setColor(mBorderColor);
101         mBorderPaint.setStrokeWidth(mBorderWidth);
102         canvas.drawCircle(width/2, height/2, (width-mBorderWidth)/2, mBorderPaint);
103         canvas = null;
104     }
105     
106     public Bitmap createOvalBitmap(final int width, final int height) {
107         Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;  
108         Bitmap localBitmap = Bitmap.createBitmap(width, height, localConfig);  
109         Canvas localCanvas = new Canvas(localBitmap);  
110         Paint localPaint = new Paint();  
111         final int padding = (mBorderWidth - 3) > 0 ? mBorderWidth - 3 : 1;  
112         /** 
113          * ??????????С(???????????????border???????????????????????????????п?????????Ч?????????????????????? 
114          * ????????padding px) 
115          */  
116         RectF localRectF = new RectF(padding, padding, width - padding, height - padding);  
117         localCanvas.drawOval(localRectF, localPaint);  
118         
119         return localBitmap;
120     }
121     
122 }

把下面的xml创建到values下,再把child_xml的ImageView改成java上面java代码所对应的包名:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 
4     <declare-styleable name="CircularImage">
5         <attr name="border_width" format="dimension" />
6         <attr name="border_color" format="color" />
7     </declare-styleable>
8 
9 </resources>

下面的就是功能代码:

activity_xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="#c7edcc"
 6     tools:context="com.example.lesson6_11_id19.MainActivity" >
 7     
 8     <ExpandableListView
 9         android:id="@+id/elv"
10         android:layout_width="match_parent"
11         android:layout_height="match_parent" >
12     </ExpandableListView>
13 </RelativeLayout>

item_group.xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:padding="10dp"
 5     android:background="#c7edcc" >
 6     
 7     <TextView 
 8         android:id="@+id/tv_groupName"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="组名"
12         android:textSize="20sp"/>
13     
14     <TextView 
15         android:id="@+id/tv_numble"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_alignParentRight="true"
19         android:text="1/10"
20         android:textSize="20dp"/>
21 
22 </RelativeLayout>

item_child.xml:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="wrap_content"
 4     android:orientation="horizontal"
 5     android:background="#c7edcc">
 6     
 7     <com.example.lesson6_11_id19.CircleImageView
 8         android:id="@+id/iv_pic"
 9         android:layout_width="80dp"
10         android:layout_height="80dp"
11         android:padding="5dp"
12         />
13     
14     <LinearLayout 
15         android:layout_width="match_parent"
16         android:layout_height="match_parent"
17         android:orientation="vertical"
18         android:padding="5dp">
19         
20         <TextView 
21             android:id="@+id/tv_name"
22             android:layout_width="wrap_content"
23             android:layout_height="0dp"
24             android:layout_weight="1"
25             android:text="昵称"
26             android:gravity="center"/>
27         
28         <TextView 
29             android:id="@+id/tv_online"
30             android:layout_width="wrap_content"
31             android:layout_height="0dp"
32             android:layout_weight="1"
33             android:gravity="center"
34             android:text="在线"/>
35         
36     </LinearLayout>
37 
38 </LinearLayout>

java代码:

 1 package com.example.lesson6_11_id19;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import com.example.lesson6_11_id19.UserGroup.User;
 7 
 8 import android.app.Activity;
 9 import android.os.Bundle;
10 import android.widget.ExpandableListView;
11 
12 public class MainActivity extends Activity {
13     ExpandableListView elv;
14     List<UserGroup> mlist = new ArrayList<UserGroup>();
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         elv = (ExpandableListView) findViewById(R.id.elv);
20         initData();
21         elv.setAdapter(new MyAdapter(this, mlist));
22         elv.setGroupIndicator(null);
23     }
24     private void initData() {
25         List<User> list_1 = new ArrayList<UserGroup.User>();
26         list_1.add(new User(R.drawable.zhaoliying, "赵丽颖", true));
27         list_1.add(new User(R.drawable.gaoyuanyuan, "高圆圆", true));
28         list_1.add(new User(R.drawable.songhuiqiao, "宋慧乔", false));
29         list_1.add(new User(R.drawable.yangmi, "赵丽颖", true));
30         UserGroup user_1 = new UserGroup("女神", list_1);
31         List<User> list_2 = new ArrayList<UserGroup.User>();
32         list_2.add(new User(R.drawable.fanbingbign, "范冰冰", true));
33         list_2.add(new User(R.drawable.libingbing, "李冰冰", false));
34         list_2.add(new User(R.drawable.linxinru, "林心如", false));
35         list_2.add(new User(R.drawable.liutao, "刘涛", true));
36         UserGroup user_2 = new UserGroup("美女明星", list_2);
37         List<User> list_3 = new ArrayList<UserGroup.User>();
38         list_3.add(new User(R.drawable.liushishi, "刘诗诗", true));
39         list_3.add(new User(R.drawable.liuyifei, "刘亦菲", true));
40         list_3.add(new User(R.drawable.songhuiqiao, "宋慧乔", false));
41         list_3.add(new User(R.drawable.zhaoliying, "赵丽颖", true));
42         UserGroup user_3 = new UserGroup("女神", list_3);
43         List<User> list_4 = new ArrayList<UserGroup.User>();
44         list_4.add(new User(R.drawable.zhaoliying, "赵丽颖", false));
45         list_4.add(new User(R.drawable.gaoyuanyuan, "高圆圆", true));
46         list_4.add(new User(R.drawable.songhuiqiao, "宋慧乔", false));
47         list_4.add(new User(R.drawable.zhaoliying, "赵丽颖", true));
48         UserGroup user_4 = new UserGroup("女神", list_4);
49         mlist.add(user_1);
50         mlist.add(user_2);
51         mlist.add(user_3);
52         mlist.add(user_4);
53     }
54 }
  1 package com.example.lesson6_11_id19;
  2 
  3 import java.util.List;
  4 
  5 import com.example.lesson6_11_id19.UserGroup.User;
  6 
  7 import android.content.Context;
  8 import android.view.View;
  9 import android.view.ViewGroup;
 10 import android.widget.BaseExpandableListAdapter;
 11 import android.widget.ImageView;
 12 import android.widget.TextView;
 13 
 14 public class MyAdapter extends BaseExpandableListAdapter{
 15     private Context mContext;
 16     private List<UserGroup> mlist;
 17     
 18     
 19     
 20     public MyAdapter(Context context, List<UserGroup> list) {
 21         this.mContext = context;
 22         this.mlist = list;
 23     }
 24 
 25     @Override
 26     public int getGroupCount() {
 27         return mlist.size();
 28     }
 29 
 30     @Override
 31     public int getChildrenCount(int groupPosition) {
 32         return mlist.get(groupPosition).users.size();
 33     }
 34 
 35     @Override
 36     public UserGroup getGroup(int groupPosition) {
 37         return mlist.get(groupPosition);
 38     }
 39 
 40     @Override
 41     public User getChild(int groupPosition, int childPosition) {
 42         return mlist.get(groupPosition).users.get(childPosition);
 43     }
 44 
 45     @Override
 46     public long getGroupId(int groupPosition) {
 47         return groupPosition;
 48     }
 49 
 50     @Override
 51     public long getChildId(int groupPosition, int childPosition) {
 52         return childPosition;
 53     }
 54 
 55     @Override
 56     public boolean hasStableIds() {
 57         return true;
 58     }
 59 
 60     @Override
 61     public View getGroupView(int groupPosition, boolean isExpanded,
 62             View convertView, ViewGroup parent) {
 63         GroupHolder holder;
 64         if (convertView == null) {
 65             convertView = View.inflate(mContext, R.layout.item_group, null);
 66             holder = new GroupHolder(convertView);
 67             convertView.setTag(holder);
 68         }else {
 69             holder = (GroupHolder) convertView.getTag();
 70         }
 71         UserGroup userGroup = getGroup(groupPosition);
 72         holder.tv_groupName.setText(userGroup.getGroupName());
 73         holder.tv_numble.setText(userGroup.getOnline());
 74         return convertView;
 75     }
 76 
 77     @Override
 78     public View getChildView(int groupPosition, int childPosition,
 79             boolean isLastChild, View convertView, ViewGroup parent) {
 80         ChildHolder holder;
 81         if (convertView == null) {
 82             convertView = View.inflate(mContext, R.layout.item_child, null);
 83             holder = new ChildHolder(convertView);
 84             convertView.setTag(holder);
 85         }else {
 86             holder = (ChildHolder) convertView.getTag();
 87         }
 88         User user = getChild(groupPosition, childPosition);
 89         holder.iv_pic.setImageResource(user.getImgId());
 90         holder.tv_name.setText(user.getNickName());
 91         holder.tv_online.setText(user.isOnline()?"[在线]":"[离线]");
 92         return convertView;
 93     }
 94 
 95     @Override
 96     public boolean isChildSelectable(int groupPosition, int childPosition) {
 97         return true;
 98     }
 99     class GroupHolder{
100         TextView tv_groupName,tv_numble;
101         public GroupHolder(View convertView) {
102             tv_groupName = (TextView) convertView.findViewById(R.id.tv_groupName);
103             tv_numble = (TextView) convertView.findViewById(R.id.tv_numble);
104         }
105         
106     }
107     
108     class  ChildHolder{
109         ImageView iv_pic;
110         TextView tv_name;
111         TextView tv_online;
112         public ChildHolder(View convertView) {
113             iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
114             tv_name = (TextView) convertView.findViewById(R.id.tv_name);
115             tv_online = (TextView) convertView.findViewById(R.id.tv_online);
116         }
117         
118     }
119 }
 1 package com.example.lesson6_11_id19;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class UserGroup {
 7     String groupName;
 8     List<User> users = new ArrayList<User>();
 9     
10     
11     public String getGroupName() {
12         return groupName;
13     }
14 
15 
16     public void setGroupName(String groupName) {
17         this.groupName = groupName;
18     }
19 
20 
21     public List<User> getUsers() {
22         return users;
23     }
24 
25 
26     public void setUsers(List<User> users) {
27         this.users = users;
28     }
29 
30 
31     public UserGroup(String groupName, List<User> users) {
32         super();
33         this.groupName = groupName;
34         this.users = users;
35     }
36     // 获取在线的人数
37     public String getOnline(){
38         int online = 0;
39         for (int i = 0; i < users.size(); i++) {
40             if (users.get(i).isOnline) {
41                 online++;
42             }
43         }
44         return online+"/"+users.size();
45         
46     }
47     
48     // 定义一个User类
49     static class User{
50         int imgId;
51         String nickName;
52         boolean isOnline;
53         
54         public User(int imgId, String nickName, boolean isOnline) {
55             super();
56             this.imgId = imgId;
57             this.nickName = nickName;
58             this.isOnline = isOnline;
59         }
60 
61         public int getImgId() {
62             return imgId;
63         }
64 
65         public void setImgId(int imgId) {
66             this.imgId = imgId;
67         }
68 
69         public String getNickName() {
70             return nickName;
71         }
72 
73         public void setNickName(String nickName) {
74             this.nickName = nickName;
75         }
76 
77         public boolean isOnline() {
78             return isOnline;
79         }
80 
81         public void setOnline(boolean isOnline) {
82             this.isOnline = isOnline;
83         }
84         
85     }
86 }

技术分享技术分享技术分享

类似QQ在线离线好友界面