微信的气泡聊天是仿iPhone自带短信而设计出来的,不过感觉还不错可以尝试一下仿着微信的气泡聊天做一个Demo,给大家分享一下!效果图如下:

气泡聊天最终要的是素材,要用到9.png文件的素材,这样气泡会随着聊天内容的多少而改变气泡的大小且不失真。为了方便,我就直接在微信里面提取出来啦。

聊天的内容是用ListView来显示的,将聊天的内容封装成一个ChatMsgEntity类的对象里面,然后交给自定义的ListView适配器将聊天内容显示出来。

ChatMsgEntity.java比较简单,只是聊天的内容存储、设置和获取。

  1. package com.example.school;
  2.  
  3. public class ChatMsgEntity {
  4. private static final String TAG = ChatMsgEntity.class.getSimpleName();
  5. //名字
  6. private String name;
  7. //日期
  8. private String date;
  9. //聊天内容
  10. private String text;
  11. //是否为对方发来的信息
  12. private boolean isComMeg = true;
  13.  
  14. public String getName() {
  15. return name;
  16. }
  17.  
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21.  
  22. public String getDate() {
  23. return date;
  24. }
  25.  
  26. public void setDate(String date) {
  27. this.date = date;
  28. }
  29.  
  30. public String getText() {
  31. return text;
  32. }
  33.  
  34. public void setText(String text) {
  35. this.text = text;
  36. }
  37.  
  38. public boolean getMsgType() {
  39. return isComMeg;
  40. }
  41.  
  42. public void setMsgType(boolean isComMsg) {
  43. isComMeg = isComMsg;
  44. }
  45.  
  46. public ChatMsgEntity() {
  47. }
  48.  
  49. public ChatMsgEntity(String name, String date, String text, boolean isComMsg) {
  50. this.name = name;
  51. this.date = date;
  52. this.text = text;
  53. this.isComMeg = isComMsg;
  54. }
  55. }

显示ListView的适配器ChatMsgViewAdpater.java:

  1. package com.example.school;
  2.  
  3. import android.R.integer;
  4. import android.content.Context;
  5. import android.database.DataSetObserver;
  6.  
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.view.View.OnLongClickListener;
  12. import android.view.ViewGroup;
  13.  
  14. import android.widget.BaseAdapter;
  15. import android.widget.CheckBox;
  16. import android.widget.LinearLayout;
  17. import android.widget.TextView;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. public class ChatMsgViewAdapter extends BaseAdapter {
  23.  
  24. //ListView视图的内容由IMsgViewType决定
  25. public static interface IMsgViewType
  26. {
  27. //对方发来的信息
  28. int IMVT_COM_MSG = 0;
  29. //自己发出的信息
  30. int IMVT_TO_MSG = 1;
  31. }
  32.  
  33. private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();
  34. private List<ChatMsgEntity> data;
  35. private Context context;
  36. private LayoutInflater mInflater;
  37.  
  38. public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> data) {
  39. this.context = context;
  40. this.data = data;
  41. mInflater = LayoutInflater.from(context);
  42. }
  43.  
  44. //获取ListView的项个数
  45. public int getCount() {
  46. return data.size();
  47. }
  48.  
  49. //获取项
  50. public Object getItem(int position) {
  51. return data.get(position);
  52. }
  53.  
  54. //获取项的ID
  55. public long getItemId(int position) {
  56. return position;
  57. }
  58.  
  59. //获取项的类型
  60. public int getItemViewType(int position) {
  61. // TODO Auto-generated method stub
  62. ChatMsgEntity entity = data.get(position);
  63.  
  64. if (entity.getMsgType())
  65. {
  66. return IMsgViewType.IMVT_COM_MSG;
  67. }else{
  68. return IMsgViewType.IMVT_TO_MSG;
  69. }
  70.  
  71. }
  72.  
  73. //获取项的类型数
  74. public int getViewTypeCount() {
  75. // TODO Auto-generated method stub
  76. return 2;
  77. }
  78.  
  79. //获取View
  80. public View getView(int position, View convertView, ViewGroup parent) {
  81.  
  82. ChatMsgEntity entity = data.get(position);
  83. boolean isComMsg = entity.getMsgType();
  84.  
  85. ViewHolder viewHolder = null;
  86. if (convertView == null)
  87. {
  88. if (isComMsg)
  89. {
  90. //如果是对方发来的消息,则显示的是左气泡
  91. convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);
  92. }else{
  93. //如果是自己发出的消息,则显示的是右气泡
  94. convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
  95. }
  96.  
  97. viewHolder = new ViewHolder();
  98. viewHolder.tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
  99. viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
  100. viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
  101. viewHolder.isComMsg = isComMsg;
  102.  
  103. convertView.setTag(viewHolder);
  104. }else{
  105. viewHolder = (ViewHolder) convertView.getTag();
  106. }
  107. viewHolder.tvSendTime.setText(entity.getDate());
  108. viewHolder.tvUserName.setText(entity.getName());
  109. viewHolder.tvContent.setText(entity.getText());
  110.  
  111. return convertView;
  112. }
  113.  
  114. //通过ViewHolder显示项的内容
  115. static class ViewHolder {
  116. public TextView tvSendTime;
  117. public TextView tvUserName;
  118. public TextView tvContent;
  119. public boolean isComMsg = true;
  120. }
  121.  
  122. }

对方发来消息的显示布局chatting_item_msg_text_left.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. android:padding="6dp">
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical"
  11. android:gravity="center_horizontal">
  12. <TextView
  13. android:id="@+id/tv_sendtime"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. style="@style/chat_text_date_style"/>
  17. </LinearLayout>
  18. <RelativeLayout
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_marginTop="5dp" >
  22. <ImageView
  23. android:id="@+id/iv_userhead"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:focusable="false"
  27. android:layout_alignParentLeft="true"
  28. android:layout_alignParentTop="true"
  29. android:background="@drawable/mini_avatar_shadow"/>
  30. <TextView
  31. android:id="@+id/tv_chatcontent"
  32. android:layout_toRightOf="@id/iv_userhead"
  33. android:layout_marginLeft="10dp"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:background="@drawable/chatfrom_bg"
  37. style="@style/chat_content_date_style"/>
  38. <TextView
  39. android:id="@+id/tv_username"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_below="@id/iv_userhead"
  43. android:layout_alignParentLeft="true"
  44. android:layout_toLeftOf="@id/tv_chatcontent"
  45. style="@style/chat_text_name_style"/>
  46. </RelativeLayout>
  47. </LinearLayout>

chatting_item_msg_text_right.xml和上面差不多,只是气泡和头像的方向在右边,就不贴代码了。

主界面的布局chat.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#f0f0e0" >
  7. <RelativeLayout
  8. android:id="@+id/rl_layout"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:background="@drawable/title" >
  12. <Button
  13. android:id="@+id/btn_back"
  14. android:gravity="center_vertical"
  15. android:layout_marginLeft="5dp"
  16. android:paddingLeft="18dp"
  17. android:textSize="18.0sp"
  18. android:textColor="#ffffff"
  19. android:background="@drawable/selector_btn_back"
  20. android:layout_width="70dp"
  21. android:layout_height="wrap_content"
  22. android:text="@string/back" />
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="@string/school_title_name"
  27. android:layout_centerInParent="true"
  28. style="@style/my_txt"/>
  29. <ImageView
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:background="@drawable/campus_info"
  33. android:layout_centerVertical="true"
  34. android:layout_alignParentRight="true"
  35. android:layout_marginRight="15dp"/>
  36. </RelativeLayout>
  37. <RelativeLayout
  38. android:id="@+id/rl_bottom"
  39. android:layout_width="fill_parent"
  40. android:layout_height="wrap_content"
  41. android:layout_alignParentBottom="true"
  42. android:background="@drawable/layout_bg1" >
  43. <Button
  44. android:id="@+id/btn_send"
  45. android:layout_width="60dp"
  46. android:layout_height="40dp"
  47. android:layout_alignParentRight="true"
  48. android:layout_marginRight="10dp"
  49. android:layout_centerVertical="true"
  50. android:text="@string/send" />
  51. <EditText
  52. android:id="@+id/et_sendmessage"
  53. android:layout_width="fill_parent"
  54. android:layout_height="40dp"
  55. android:layout_toLeftOf="@id/btn_send"
  56. android:layout_marginLeft="10dp"
  57. android:layout_marginRight="10dp"
  58. android:background="@drawable/edittext1"
  59. android:layout_centerVertical="true"
  60. android:singleLine="true"
  61. android:textSize="18sp"/>
  62. </RelativeLayout>
  63. <ListView
  64. android:id="@+id/listview"
  65. android:layout_below="@id/rl_layout"
  66. android:layout_above="@id/rl_bottom"
  67. android:layout_width="fill_parent"
  68. android:layout_height="fill_parent"
  69. android:layout_marginLeft="10.0dip"
  70. android:layout_marginTop="10.0dip"
  71. android:layout_marginRight="10.0dip"
  72. android:divider="@null"
  73. android:dividerHeight="5dp"
  74. android:scrollbars="none"
  75. android:cacheColorHint="#00000000"/>
  76. </RelativeLayout>

ChatActivity.java

  1. package com.example.chat;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Calendar;
  5. import java.util.List;
  6.  
  7. import com.example.school.R;
  8.  
  9. import android.os.Bundle;
  10. import android.app.Activity;
  11. import android.content.Intent;
  12. import android.view.KeyEvent;
  13. import android.view.Menu;
  14. import android.view.View;
  15. import android.view.Window;
  16. import android.view.View.OnClickListener;
  17. import android.widget.AdapterView;
  18. import android.widget.AdapterView.OnItemClickListener;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.ListView;
  22. import android.widget.TextView;
  23.  
  24. public class ChatActivity extends Activity implements OnClickListener {
  25. private Button mBtnSend;
  26. private Button mBtnBack;
  27. private EditText mEditTextContent;
  28. //聊天内容的适配器
  29. private ChatMsgViewAdapter mAdapter;
  30. private ListView mListView;
  31. //聊天的内容
  32. private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();
  33.  
  34. @Override
  35. public void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
  38. setContentView(R.layout.chat);
  39. initView();
  40. initData();
  41. }
  42.  
  43. //初始化视图
  44. private void initView() {
  45. mListView = (ListView) findViewById(R.id.listview);
  46. mBtnBack = (Button) findViewById(R.id.btn_back);
  47. mBtnBack.setOnClickListener(this);
  48. mBtnSend = (Button) findViewById(R.id.btn_send);
  49. mBtnSend.setOnClickListener(this);
  50. mEditTextContent = (EditText) findViewById(R.id.et_sendmessage);
  51. }
  52.  
  53. private String[] msgArray = new String[]{" 孩子们,要好好学习,天天向上!要好好听课,不要翘课!不要挂科,多拿奖学金!三等奖学金的争取拿二等,二等的争取拿一等,一等的争取拿励志!",
  54. "姚妈妈还有什么吩咐...",
  55. "还有,明天早上记得跑操啊,不来的就扣德育分!",
  56. "德育分是什么?扣了会怎么样?",
  57. "德育分会影响奖学金评比,严重的话,会影响毕业",
  58. "哇!学院那么不人道?",
  59. "你要是你不听话,我当场让你不能毕业!",
  60. "姚妈妈,我知错了(- -我错在哪了...)"};
  61.  
  62. private String[]dataArray = new String[]{"2012-09-01 18:00", "2012-09-01 18:10",
  63. "2012-09-01 18:11", "2012-09-01 18:20",
  64. "2012-09-01 18:30", "2012-09-01 18:35",
  65. "2012-09-01 18:40", "2012-09-01 18:50"};
  66. private final static int COUNT = 8;
  67.  
  68. //初始化要显示的数据
  69. private void initData() {
  70. for(int i = 0; i < COUNT; i++) {
  71. ChatMsgEntity entity = new ChatMsgEntity();
  72. entity.setDate(dataArray[i]);
  73. if (i % 2 == 0)
  74. {
  75. entity.setName("姚妈妈");
  76. entity.setMsgType(true);
  77. }else{
  78. entity.setName("Shamoo");
  79. entity.setMsgType(false);
  80. }
  81.  
  82. entity.setText(msgArray[i]);
  83. mDataArrays.add(entity);
  84. }
  85. mAdapter = new ChatMsgViewAdapter(this, mDataArrays);
  86. mListView.setAdapter(mAdapter);
  87. }
  88.  
  89. public void onClick(View view) {
  90. // TODO Auto-generated method stub
  91. switch(view.getId()) {
  92. case R.id.btn_back:
  93. back();
  94. break;
  95. case R.id.btn_send:
  96. send();
  97. break;
  98. }
  99. }
  100.  
  101. private void send()
  102. {
  103. String contString = mEditTextContent.getText().toString();
  104. if (contString.length() > 0)
  105. {
  106. ChatMsgEntity entity = new ChatMsgEntity();
  107. entity.setDate(getDate());
  108. entity.setName("");
  109. entity.setMsgType(false);
  110. entity.setText(contString);
  111. mDataArrays.add(entity);
  112. mAdapter.notifyDataSetChanged();
  113. mEditTextContent.setText("");
  114. mListView.setSelection(mListView.getCount() - 1);
  115. }
  116. }
  117.  
  118. //获取日期
  119. private String getDate() {
  120. Calendar c = Calendar.getInstance();
  121. String year = String.valueOf(c.get(Calendar.YEAR));
  122. String month = String.valueOf(c.get(Calendar.MONTH));
  123. String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
  124. String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
  125. String mins = String.valueOf(c.get(Calendar.MINUTE));
  126. StringBuffer sbBuffer = new StringBuffer();
  127. sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":" + mins);
  128. return sbBuffer.toString();
  129. }
  130. public boolean onKeyDown(int keyCode, KeyEvent event) {
  131. back();
  132. return true;
  133. }
  134.  
  135. private void back() {
  136. finish();
  137. }
  138. }

谢谢大家的支持!链接:点击打开链接

需要一分资源分,大家评论一下那一分就还回来了,哈哈~~

转自http://blog.csdn.net/pocoyoshamoo/article/details/9674385

转-Android仿微信气泡聊天界面设计的更多相关文章

  1. Android仿微信气泡聊天界面设计

    微信的气泡聊天是仿iPhone自带短信而设计出来的,不过感觉还不错可以尝试一下仿着微信的气泡聊天做一个Demo,给大家分享一下!效果图如下: 气泡聊天最终要的是素材,要用到9.png文件的素材,这样气 ...

  2. android 仿QQ气泡聊天界面

    1.现在的QQ,微信等一些APP的聊天界面都是气泡聊天界面,左边是接收到的消息,右边是发送的消息, 这个效果其实就是一个ListView在加载它的Item的时候,分别用了不同的布局xml文件. 2.效 ...

  3. Android仿微信SlideView聊天列表滑动删除效果

    package com.ryg.slideview; import com.ryg.slideview.MainActivity.MessageItem; //Download by http://w ...

  4. 【手把手教程】uniapp + vue 从0搭建仿微信App聊天应用:腾讯云TXIM即时通讯的最佳实践

    基于uniapp + vue 实现仿微信App聊天应用实践,实现以下功能 1: 用户登陆 2: 聊天会话管理 3: 文本/图片/视频/定位消息收发 4: 贴图表情消息收发 5: 一对一语音视频在线通话 ...

  5. uniapp+nvue实现仿微信App聊天应用 —— 成功实现好友聊天+语音视频通话功能

    基于uniapp + nvue实现的uniapp仿微信App聊天应用 txim 实例项目,实现了以下功能. 1: 聊天会话管理 2: 好友列表 3: 文字.语音.视频.表情.位置等聊天消息收发 4: ...

  6. Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等

    仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...

  7. Tauri-Vue3桌面端聊天室|tauri+vite3仿微信|tauri聊天程序EXE

    基于tauri+vue3.js+vite3跨桌面端仿微信聊天实例TauriVue3Chat. tauri-chat 运用最新tauri+vue3+vite3+element-plus+v3layer等 ...

  8. Android 仿微信小视频录制

    Android 仿微信小视频录制 WechatShortVideo和WechatShortVideo文章

  9. android 仿微信聊天界面,以及语音录制功能

    extends:http://104zz.iteye.com/blog/1709840 本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图:     第一:chat.xml设计 ...

随机推荐

  1. 一款bootstrap树形js

    http://www.htmleaf.com/Demo/201502141380.html

  2. Ubuntu 16.04 nvidia安装

    Ubuntu更新完NVIDIA驱动后,重启电脑进入不了系统,一直处于登录界面.后来重启电脑时发现我进入不了系统了,输入我的登录密码会发现屏幕一闪,然后又重新跳回到登录界面,就是进入了login loo ...

  3. 夺命雷公狗-----React---2--组建

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. JS中同名函数有效执行顺序

    html中如果出现函数同名时:如果有多个外部引入的js文件,例如a.js和b.js(引入顺序假定是a.js,然后是b.js),同时html中本身也有内部的js.那么针对 出现函数名一样的情况时,无论他 ...

  5. JQ 操作 radio、checkbox 、select

    MXS&Vincene  ─╄OvЁ  &0000026─╄OvЁ  MXS&Vincene MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好, ...

  6. IOS手势UIGestureRecognizer

    UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有6个子类处理具体的手势: 1.UITapGestureRecognizer (任意手指任意次数的点击) // 点击次数 ...

  7. 嵌入式Linux内核制作【转】

    本文转载自:http://blog.csdn.net/coding__madman/article/details/51291316 1. Linux体系结构 从整体上来分,linux可以分为User ...

  8. Java 中System里getProperty(something)

    Java 中System里getProperty 方法获得系统参数 Key Description of Associated Value 中文描述 java.version Java Runtime ...

  9. HTML5正则表单式验证电子邮件

    <input type="text" title="email" required pattern="[^@]+@[^@]+\.[a-zA-Z] ...

  10. LA 3713 宇航员分组

    题目链接:http://vjudge.net/contest/142615#problem/B 题意:有A,B,C三个人物要分配个N个宇航员,每个宇航员恰好要分配一个任务,设平均年龄为X,只有年龄大于 ...