适用于Android开发的简单聊天软件
程序员文章站
2024-03-04 21:48:36
适用于android 开发。是一个简单的聊天软件,包括知识点,各个控件的运用(expandablelistview,viewpager,spinner,linearlayo...
适用于android 开发。是一个简单的聊天软件,包括知识点,各个控件的运用(expandablelistview,viewpager,spinner,linearlayout,relativelayot),自定义的viaimageview(自定义xml属性),sql 的写入,读取等操作。
1. viaimageview.java
package com.farina.farinaimagelib; import android.content.context; import android.content.res.typedarray; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.graphics.porterduff; import android.graphics.porterduffxfermode; import android.graphics.rectf; import android.graphics.xfermode; import android.graphics.drawable.drawable; import android.graphics.drawable.ninepatchdrawable; import android.util.attributeset; import android.widget.imageview; /** * created by farinazhang on 2016/5/25. */ public class viaimageview extends imageview { private int shapetype=-1; private int borderwidth=0; //the image 's border width private int bordercolor= color.black; //the image 's border color private boolean usedefaultstyle =false; //draw a common imageview private static final xfermode mask_xfermode; private int animtype=-1; //动画类型 private int mwidth; //图片的宽度 private int mheight; //图片的高度 static { porterduff.mode localmode = porterduff.mode.dst_in; mask_xfermode = new porterduffxfermode(localmode); } public static enum shape {circle,triangle,star,hexagonal,rectangle}; public viaimageview(context context){ super(context); init(context, null, 0); } public viaimageview(context context, attributeset attrs){ super(context, attrs); init(context, attrs, 0); } public viaimageview(context context,attributeset attrs,int defstyle){ super(context, attrs, defstyle); init(context, attrs, defstyle); } public void init(context context,attributeset attrs,int defstyle){ if(attrs!=null){ typedarray a = getcontext().obtainstyledattributes( attrs,r.styleable.viaimage, defstyle, 0); shapetype = (int)a.getinteger(r.styleable.viaimage_shapetype,shapetype); borderwidth=(int)a.getdimension(r.styleable.viaimage_borderwidth,borderwidth); bordercolor = a.getcolor(r.styleable.viaimage_bordercolor, bordercolor); a.recycle(); } } /** *外部接口 *设置imageview的属性,包括形状,边框颜色,边框宽度 **/ public void setimageviewproperty(int shapetype,int width,int color){ this.shapetype = shapetype; this.borderwidth = width; this.bordercolor = color; postinvalidate(); } public void setimageviewshape(int shapetype){ this.shapetype = shapetype; postinvalidate(); } public void setimageborderwidth(int width){ this.borderwidth = width; postinvalidate(); } public void setimagebordercolor(int color){ this.bordercolor = color; postinvalidate(); } /** 外部接口 * 设置是否以默认格式绘制,普通格式 */ public void setdrawcommonimage(boolean iscommon){ this.usedefaultstyle=iscommon; } @override protected void ondraw(canvas canvas){ if(usedefaultstyle){ super.ondraw(canvas); return; } final drawable localdrawable=getdrawable(); if(localdrawable==null)return; if(localdrawable instanceof ninepatchdrawable)return; mwidth=getwidth(); mheight=getheight(); int layer=canvas.savelayer(0.0f,0.0f,mwidth,mheight,null,canvas.all_save_flag); localdrawable.setbounds(0,0,mwidth,mheight); /*将drawable绑定到bitmap(this.mask)上面(drawable 只能通过bitmap显示出来)*/ localdrawable.draw(canvas); createshapebitmap(canvas); /*将画布复制到layer上*/ canvas.restoretocount(layer); if(borderwidth!=0){ drawborder(canvas); } } private void createshapebitmap(canvas canvas){ paint mypaint; mypaint = new paint(); mypaint.setfilterbitmap(false); mypaint.setantialias(true); mypaint.setxfermode(mask_xfermode); bitmap.config localconfig=bitmap.config.argb_8888; bitmap localbitmap=bitmap.createbitmap(mwidth,mheight,localconfig); canvas localcanvas=new canvas(localbitmap); paint localpaint = new paint(); localpaint.setantialias(true); int padding=borderwidth; int radius = ((mwidth>mheight)?mheight:mwidth)/2-padding; switch(shapetype){ case 0://圆形; { localcanvas.drawcircle(mwidth/2,mheight/2, radius, localpaint); break; } case 1://三角形; { path path = new path(); path.moveto(padding, padding); path.lineto(mwidth / 2, (float)(mwidth*math.cos(degree2radian(30))-padding)); path.lineto(mwidth-padding, padding); path.close(); localcanvas.drawpath(path, localpaint); break; } case 2://五角星形; { path path = new path(); float radian = degree2radian(36);// 36为五角星的角度 float radius_in = (float) (radius * math.sin(radian / 2) / math .cos(radian)); // 中间五边形的半径 path.moveto((float) (radius * math.cos(radian / 2)), 0);// 此点为多边形的起点 path.lineto((float) (radius * math.cos(radian / 2) + radius_in * math.sin(radian)), (float) (radius - radius * math.sin(radian / 2))); path.lineto((float) (radius * math.cos(radian / 2) * 2), (float) (radius - radius * math.sin(radian / 2))); path.lineto((float) (radius * math.cos(radian / 2) + radius_in * math.cos(radian / 2)), (float) (radius + radius_in * math.sin(radian / 2))); path.lineto( (float) (radius * math.cos(radian / 2) + radius * math.sin(radian)), (float) (radius + radius * math.cos(radian))); path.lineto((float) (radius * math.cos(radian / 2)), (float) (radius + radius_in)); path.lineto( (float) (radius * math.cos(radian / 2) - radius * math.sin(radian)), (float) (radius + radius * math.cos(radian))); path.lineto((float) (radius * math.cos(radian / 2) - radius_in * math.cos(radian / 2)), (float) (radius + radius_in * math.sin(radian / 2))); path.lineto(0, (float) (radius - radius * math.sin(radian / 2))); path.lineto((float) (radius * math.cos(radian / 2) - radius_in * math.sin(radian)), (float) (radius - radius * math.sin(radian / 2))); path.close();// 使这些点构成封闭的多边形 localcanvas.drawpath(path, localpaint); break; } case 3://正六边形; { path path= new path(); float radian = degree2radian(30); path.moveto((float)(radius*math.sin(radian)), 0); path.lineto((float)(radius*math.sin(radian)+radius), 0); path.lineto((float)(2*radius), (float)(radius*math.cos(radian)) ); path.lineto((float)(radius*math.sin(radian)+radius),(float)(2*(radius*math.cos(radian)))); path.lineto((float)(radius*math.sin(radian)),(float)(2*(radius*math.cos(radian)))); path.lineto(0,(float)(radius*math.cos(radian))); path.close(); localcanvas.drawpath(path,localpaint); break; } default://方形 { int realwidth = (mwidth>mheight)?mheight:mwidth; int x = padding +(mwidth-realwidth)/2; int y = padding +(mheight-realwidth)/2; rectf localrectf=new rectf(x, y, realwidth-padding*2, realwidth-padding*2); localcanvas.drawrect(localrectf, localpaint); break; } } /*将bitmap 画到canvas上*/ canvas.drawbitmap(localbitmap,0.0f,0.0f,mypaint); } //画边框 private void drawborder(canvas canvas){ paint localpaint = new paint(); localpaint.setcolor(bordercolor); localpaint.setstyle(paint.style.stroke); localpaint.setstrokewidth(borderwidth); localpaint.setantialias(true); int radius=((mwidth>mheight)?mheight:mwidth)/2-borderwidth/2; int padding = borderwidth/2; switch(shapetype){ case 0://圆形; { canvas.drawcircle(mwidth/2, mheight/2,radius,localpaint); break; } case 1://三角形; { path path = new path(); path.moveto(padding, padding); path.lineto(mwidth / 2, (float)(mwidth*math.cos(degree2radian(30))-padding)); path.lineto(mwidth-padding, padding); path.close(); canvas.drawpath(path, localpaint); break; } case 2://五角星形; { path path = new path(); float radian = degree2radian(36);// 36为五角星的角度 float radius_in = (float) (radius * math.sin(radian / 2) / math .cos(radian)); // 中间五边形的半径 path.moveto((float) (radius * math.cos(radian / 2)), 0);// 此点为多边形的起点 path.lineto((float) (radius * math.cos(radian / 2) + radius_in * math.sin(radian)), (float) (radius - radius * math.sin(radian / 2))); path.lineto((float) (radius * math.cos(radian / 2) * 2), (float) (radius - radius * math.sin(radian / 2))); path.lineto((float) (radius * math.cos(radian / 2) + radius_in * math.cos(radian / 2)), (float) (radius + radius_in * math.sin(radian / 2))); path.lineto( (float) (radius * math.cos(radian / 2) + radius * math.sin(radian)), (float) (radius + radius * math.cos(radian))); path.lineto((float) (radius * math.cos(radian / 2)), (float) (radius + radius_in)); path.lineto( (float) (radius * math.cos(radian / 2) - radius * math.sin(radian)), (float) (radius + radius * math.cos(radian))); path.lineto((float) (radius * math.cos(radian / 2) - radius_in * math.cos(radian / 2)), (float) (radius + radius_in * math.sin(radian / 2))); path.lineto(0, (float) (radius - radius * math.sin(radian / 2))); path.lineto((float) (radius * math.cos(radian / 2) - radius_in * math.sin(radian)), (float) (radius - radius * math.sin(radian / 2))); path.close();// 使这些点构成封闭的多边形 canvas.drawpath(path, localpaint); break; } case 3://正六边形; { path path= new path(); float radian = degree2radian(30); path.moveto((float)(radius*math.sin(radian)), 0); path.lineto((float)(radius*math.sin(radian)+radius), 0); path.lineto((float)(2*radius), (float)(radius*math.cos(radian)) ); path.lineto((float)(radius*math.sin(radian)+radius),(float)(2*(radius*math.cos(radian)))); path.lineto((float)(radius*math.sin(radian)),(float)(2*(radius*math.cos(radian)))); path.lineto(0,(float)(radius*math.cos(radian))); path.close(); canvas.drawpath(path,localpaint); break; } default://方形 { int realwidth = (mwidth>mheight)?mheight:mwidth-borderwidth; int x = padding +(mwidth-realwidth)/2; int y = padding +(mheight-realwidth)/2; rectf localrectf=new rectf(x, y, realwidth-padding*2, realwidth-padding*2); canvas.drawrect(localrectf, localpaint); break; } } } //将角度转化为弧度 private float degree2radian(int degree) { // todo auto-generated method stub return (float) (math.pi * degree / 180); } }
2. contactexpendlistadapter.java
package com.farina.adapter; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.imageview; import android.widget.textview; import com.farina.data.friendentity; import com.farina.data.friendlistdata; import com.farina.farinaimagelib.viaimageview; import com.farina.libtest.r; import java.util.list; /** * created by farinazhang on 2016/7/5. */ public class contactexpendlistadapter extends baseexpandablelistadapter { private context mcontext; private list<friendlistdata> mlistdata; public contactexpendlistadapter(context context, list<friendlistdata> data){ mcontext = context; mlistdata = data; } @override public int getgroupcount(){ return mlistdata.size(); } @override public int getchildrencount(int groupposition){ return mlistdata.get(groupposition).getfriendchildlist().size(); } @override public object getgroup(int groupposition){ return mlistdata.get(groupposition).getgroupname(); } @override public object getchild(int groupposition, int childposition){ return mlistdata.get(groupposition).getfriendchildlist().get(childposition); } @override public long getgroupid(int groupposition){ return groupposition; } @override public long getchildid(int groupposition, int childposition){ return childposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent){ groupholder viewholder =null; if(convertview == null){ convertview = layoutinflater.from(mcontext).inflate(r.layout.contact_list_group, null); viewholder = new groupholder(); viewholder.icon = (imageview)convertview.findviewbyid(r.id.icon); viewholder.groupname = (textview)convertview.findviewbyid(r.id.goup_name); convertview.settag(viewholder); }else{ viewholder = (groupholder)convertview.gettag(); } viewholder.groupname.settext(mlistdata.get(groupposition).getgroupname()); if(isexpanded){ viewholder.icon.setimageresource(r.mipmap.arrow_down); }else{ viewholder.icon.setimageresource(r.mipmap.arrow_up); } return convertview; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent){ memberholder viewholder = null; if(convertview == null){ convertview = layoutinflater.from(mcontext).inflate(r.layout.contact_list_member, null); viewholder= new memberholder(); viewholder.memberimg = (viaimageview) convertview.findviewbyid(r.id.member_img); viewholder.membername = (textview)convertview.findviewbyid(r.id.member_name); convertview.settag(viewholder); }else{ viewholder =(memberholder)convertview.gettag(); } friendentity entity= mlistdata.get(groupposition).getfriendchildlist().get(childposition); string icnpath = entity.geticnpath(); if(icnpath == null||icnpath.length()<=0) { viewholder.memberimg.setimagedrawable(mcontext.getresources().getdrawable(r.mipmap.user_img)); } viewholder.membername.settext(entity.getname()); return convertview; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } @override public boolean hasstableids(){ return true; } private class memberholder{ viaimageview memberimg; textview membername; } private class groupholder{ imageview icon; textview groupname; } }
3. chatinfomanager.java
package com.farina.db; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import com.farina.data.friendentity; import com.farina.data.friendlistdata; import com.farina.data.groupentity; import com.farina.data.messageentity; import com.farina.data.userentity; import java.util.arraylist; import java.util.list; /** * created by farinazhang on 2016/7/14. */ public class chatinfomanager { private context mcontext; private chatdbhelper mdbhelper; //table user string private final string user_table_name="users"; private final string user_id="id"; private final string user_count_number="countnumber"; private final string user_nikename="nikename"; private final string user_icon_path="iconpath"; private final string user_phone_number="phonenumber"; private final string user_mail_addr="mailaddr"; private final string user_qq_number="qqnumber"; private final string user_weixin_number="weixinnumber"; //table friends string private final string friend_table_name="friends"; private final string friend_id="id"; private final string friend_host_id="hostid"; private final string friend_group_id="groupid"; private final string friend_name="friendname"; private final string friend_icon_path="iconpath"; private final string friend_belive="belive"; //table groups string private final string group_table_name="groups"; private final string group_id="id"; private final string group_host_id="hostid"; private final string group_name="groupname"; //table messages string private final string message_table_name="messages"; private final string message_id="id"; private final string message_message="message"; private final string message_from_id="fromid"; private final string message_to_id="toid"; private final string message_readed="readed"; private final string message_type="type"; private final string message_time="time"; public chatinfomanager(context context){ mcontext = context; mdbhelper = chatdbhelper.getinstance(mcontext); } public void adduser(userentity user){ sqlitedatabase db = null; try { db = mdbhelper.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(user_id,user.getid()); values.put(user_count_number,user.getcountnumber()); values.put(user_nikename,user.getnikename()); values.put(user_icon_path,user.geticonpath()); values.put(user_phone_number,user.getphonenumber()); values.put(user_mail_addr,user.getmailnumber()); values.put(user_qq_number,user.getqqnumber()); values.put(user_weixin_number,user.getweixinnumber()); db.insert(user_table_name, null, values); } catch (exception e) { e.printstacktrace(); // todo: handle exception } finally { db.close(); } } public userentity getuserdata(int index){ userentity user= new userentity(); sqlitedatabase db =null; cursor cursor = null; try { db = mdbhelper.getreadabledatabase(); cursor = db.query(user_table_name, null, user_id + "=" + index, null, null, null, null); if (cursor != null) { user.setid(index); user.setcountnumber(cursor.getstring(cursor.getcolumnindex(user_count_number))); user.setnikename(cursor.getstring(cursor.getcolumnindex(user_nikename))); user.seticonpath(cursor.getstring(cursor.getcolumnindex(user_icon_path))); user.setphonenumber(cursor.getstring(cursor.getcolumnindex(user_phone_number))); user.setmailnumber(cursor.getstring(cursor.getcolumnindex(user_mail_addr))); user.setqqnumber(cursor.getstring(cursor.getcolumnindex(user_qq_number))); user.setweixinnumber(cursor.getstring(cursor.getcolumnindex(user_weixin_number))); } else { return null; } }catch (exception e){ e.printstacktrace(); }finally{ db.close(); } return user; } public list<userentity> getuserlist(){ list<userentity> list =new arraylist<userentity>(); sqlitedatabase db =null; cursor cursor=null; try{ db=mdbhelper.getreadabledatabase(); cursor=db.query(user_table_name,new string[]{user_id,user_count_number,user_nikename,user_icon_path},null,null,null,null,null); if(cursor!=null){ while (cursor.movetonext()) { userentity user= new userentity(); user.setid(cursor.getint(cursor.getcolumnindex(user_id))); user.setcountnumber(cursor.getstring(cursor.getcolumnindex(user_count_number))); user.setnikename(cursor.getstring(cursor.getcolumnindex(user_nikename))); user.seticonpath(cursor.getstring(cursor.getcolumnindex(user_icon_path))); list.add(user); } } }catch(exception e){ e.printstacktrace(); }finally{ db.close(); } return list; } public void addfriend(friendentity friend){ sqlitedatabase db=null; try{ db=mdbhelper.getwritabledatabase(); if(db==null)return ; contentvalues values = new contentvalues(); values.put(friend_id,friend.getid()); values.put(friend_host_id,friend.gethostid()); values.put(friend_group_id,friend.getgroupid()); values.put(friend_name,friend.getname()); values.put(friend_icon_path,friend.geticnpath()); values.put(friend_belive,friend.getbelive()); db.insert(friend_table_name,null,values); }catch(exception e){ e.printstacktrace(); }finally { db.close(); } } public list<friendlistdata> getfriendlist(){ sqlitedatabase db=null; list<friendlistdata> friendlist=new arraylist<friendlistdata>(); list<groupentity> grouplist= getgrouplist(); try{ db = mdbhelper.getwritabledatabase(); if (db == null) return null; for(int i=0;i<grouplist.size();i++) { friendlistdata friendgroup= new friendlistdata(); string groupname = grouplist.get(i).getname(); friendgroup.setgroupname(groupname); list<friendentity> groupchild=new arraylist<friendentity>(); cursor cursor = db.query(friend_table_name, null, group_name+"="+groupname, null, null, friend_id + "asc", null); while (cursor.movetonext()) { friendentity entity = new friendentity(); entity.setid(cursor.getint(cursor.getcolumnindex(friend_id))); entity.setgroupid(cursor.getint(cursor.getcolumnindex(friend_group_id))); entity.sethostid(cursor.getint(cursor.getcolumnindex(friend_host_id))); entity.setname(cursor.getstring(cursor.getcolumnindex(friend_name))); entity.seticnpath(cursor.getstring(cursor.getcolumnindex(friend_icon_path))); string belive = cursor.getstring(cursor.getcolumnindex(friend_belive)); entity.setbelive(belive.equals("1")); groupchild.add(entity); } friendgroup.setfriendchildlist(groupchild); friendlist.add(friendgroup); } }catch(exception e){ e.printstacktrace(); }finally { db.close(); } return friendlist; } public list<groupentity> getgrouplist(){ list<groupentity> grouplist = null; sqlitedatabase db=null; try{ db= mdbhelper.getreadabledatabase(); cursor cursor =db.query(group_table_name,null,null,null,null,null,null); while(cursor.movetonext()){ groupentity entity =new groupentity(); entity.setid(cursor.getint(cursor.getcolumnindex(group_id))); entity.sethostid(cursor.getint(cursor.getcolumnindex(group_host_id))); entity.setgroupname(cursor.getstring(cursor.getcolumnindex(group_name))); grouplist.add(entity); } }catch(exception e){ e.printstacktrace(); }finally{ db.close(); } return grouplist; } public void addgroupmember(groupentity group){ sqlitedatabase db=null; try{ db=mdbhelper.getwritabledatabase(); contentvalues values=new contentvalues(); values.put(group_id,group.getid()); values.put(group_host_id,group.gethostid()); values.put(group_name,group.getname()); db.insert(group_table_name,null,values); }catch(exception e){ e.printstacktrace(); }finally { db.close(); } } public list<messageentity> getmessagelist(string loadid){ list<messageentity> list = new arraylist<messageentity>(); sqlitedatabase db=null; cursor cursor=null; try{ db=mdbhelper.getreadabledatabase(); cursor=db.query(message_table_name,null, message_from_id+"=?,"+message_to_id+"=?", new string[]{loadid}, null, loadid,message_time+"des",null); while(cursor.movetonext()){ messageentity entity =new messageentity(); entity.setid(cursor.getint(cursor.getcolumnindex(message_id))); entity.setfromid(cursor.getint(cursor.getcolumnindex(message_from_id))); entity.settoid(cursor.getint(cursor.getcolumnindex(message_to_id))); entity.setmessagestr(cursor.getstring(cursor.getcolumnindex(message_message))); entity.settype(cursor.getstring(cursor.getcolumnindex(message_type))); entity.settime(cursor.getstring(cursor.getcolumnindex(message_time))); list.add(entity); } }catch (exception e){ e.printstacktrace(); }finally{ db.close(); } return list; } public void setmessageitem(messageentity message){ sqlitedatabase db=null; try{ db=mdbhelper.getwritabledatabase(); if(db==null)return; contentvalues values = new contentvalues(); values.put(message_id,message.getid()); values.put(message_from_id,message.getfromid()); values.put(message_to_id,message.gettoid()); values.put(message_message,message.getmessagestr()); values.put(message_type,message.gettype()); values.put(message_time,message.gettime()); db.insert(message_table_name,null,values); }catch(exception e){ e.printstacktrace(); }finally { db.close(); } } }
源码下载地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。