微信聊天窗口的信息效果类似iphone上的短信效果,以气泡的形式展现,在Android上,实现这种效果主要用到ListView和BaseAdapter,配合布局以及相关素材,就可以自己做出这个效果,素材可以下一个微信的APK,然后把后缀名改成zip,直接解压,就可以得到微信里面的所有素材了。首先看一下我实现的效果:

以下是工程目录结构:

接下来就是如何实现这个效果的代码:

main.xml,这个是主布局文件,显示listview和上下两部分内容。

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#f0f0e0" >
  6. <RelativeLayout
  7. android:id="@+id/rl_top"
  8. android:layout_width="fill_parent"
  9. android:layout_alignParentTop="true"
  10. android:layout_height="wrap_content">
  11. <TextView
  12. android:layout_width="fill_parent"
  13. android:layout_height="44dp"
  14. android:gravity="center"
  15. android:textSize="18sp"
  16. android:background="#486a9a"
  17. android:textColor="@android:color/white"
  18. android:text="Chat"/>
  19. </RelativeLayout>
  20. <RelativeLayout
  21. android:id="@+id/rl_bottom"
  22. android:layout_alignParentBottom="true"
  23. android:layout_width="fill_parent"
  24. android:background="#486a9a"
  25. android:paddingTop="5dp"
  26. android:layout_height="wrap_content">
  27. <Button
  28. android:id="@+id/btn_send"
  29. android:layout_width="70dp"
  30. android:layout_height="50dp"
  31. android:layout_alignParentRight="true"
  32. android:layout_centerVertical="true"
  33. android:layout_marginRight="10dp"
  34. android:text="Send" />
  35. <EditText
  36. android:id="@+id/et_content"
  37. android:layout_width="fill_parent"
  38. android:layout_height="50dp"
  39. android:layout_centerVertical="true"
  40. android:layout_marginLeft="10dp"
  41. android:layout_marginRight="10dp"
  42. android:layout_toLeftOf="@id/btn_send"
  43. android:textSize="16sp"/>
  44. </RelativeLayout>
  45. <ListView
  46. android:id="@+id/listview"
  47. android:layout_width="fill_parent"
  48. android:layout_height="fill_parent"
  49. android:layout_above="@id/rl_bottom"
  50. android:layout_below="@id/rl_top"
  51. android:layout_marginLeft="10dp"
  52. android:layout_marginRight="10dp"
  53. android:layout_marginTop="10dp"
  54. android:cacheColorHint="#00000000"
  55. android:divider="@null"
  56. android:listSelector="#00000000"
  57. android:dividerHeight="3dp"
  58. android:scrollbars="none"/>
  59. </RelativeLayout>

然后就是listview中两种类型item的布局文件,分别是接收信息的item效果和发送信息的item效果

chat_from_item.xml是接收信息的item布局:

[html] view plaincopy
  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:orientation="vertical"
  5. android:paddingBottom="5dp"
  6. android:layout_height="wrap_content" >
  7. <TextView
  8. android:id="@+id/tv_time"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="center_horizontal"
  12. android:background="#bfbfbf"
  13. android:paddingTop="2dp"
  14. android:paddingBottom="2dp"
  15. android:paddingLeft="4dp"
  16. android:paddingRight="4dp"
  17. android:textColor="#ffffff"
  18. android:textSize="12sp" />
  19. <RelativeLayout
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_marginTop="5dp" >
  23. <ImageView
  24. android:id="@+id/iv_user_image"
  25. android:layout_width="50dp"
  26. android:layout_height="50dp"
  27. android:layout_alignParentLeft="true"
  28. android:layout_alignParentTop="true"
  29. android:background="@drawable/mypic"
  30. android:focusable="false" />
  31. <TextView
  32. android:id="@+id/tv_content"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_marginLeft="5dp"
  36. android:layout_toRightOf="@+id/iv_user_image"
  37. android:background="@drawable/chatfrom_bg"
  38. android:gravity="left|center"
  39. android:clickable="true"
  40. android:focusable="true"
  41. android:lineSpacingExtra="2dp"
  42. android:minHeight="50dp"
  43. android:textColor="#ff000000"
  44. android:textSize="14sp" />
  45. </RelativeLayout>
  46. </LinearLayout>

chat_to_item.xml是发送信息item的布局:

[html] view plaincopy
  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:orientation="vertical"
  5. android:paddingBottom="5dp"
  6. android:layout_height="wrap_content" >
  7. <TextView
  8. android:id="@+id/tv_time"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:background="#bfbfbf"
  12. android:layout_gravity="center_horizontal"
  13. android:paddingTop="2dp"
  14. android:paddingBottom="2dp"
  15. android:paddingLeft="4dp"
  16. android:paddingRight="4dp"
  17. android:textColor="#ffffff"
  18. android:textSize="12sp" />
  19. <RelativeLayout
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_marginTop="5dp" >
  23. <ImageView
  24. android:id="@+id/iv_user_image"
  25. android:layout_width="50dp"
  26. android:layout_height="50dp"
  27. android:layout_alignParentRight="true"
  28. android:layout_alignParentTop="true"
  29. android:background="@drawable/mypic"
  30. android:focusable="false" />
  31. <TextView
  32. android:id="@+id/tv_content"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_marginRight="5dp"
  36. android:layout_toLeftOf="@+id/iv_user_image"
  37. android:background="@drawable/chatto_bg"
  38. android:gravity="left|center"
  39. android:clickable="true"
  40. android:focusable="true"
  41. android:lineSpacingExtra="2dp"
  42. android:textColor="#ff000000"
  43. android:textSize="14sp" />
  44. </RelativeLayout>
  45. </LinearLayout>

布局完成后新建一个实体类ChatEntity.java:

[java] view plaincopy
  1. public class ChatEntity {
  2. private int userImage;
  3. private String content;
  4. private String chatTime;
  5. private boolean isComeMsg;
  6. public int getUserImage() {
  7. return userImage;
  8. }
  9. public void setUserImage(int userImage) {
  10. this.userImage = userImage;
  11. }
  12. public String getContent() {
  13. return content;
  14. }
  15. public void setContent(String content) {
  16. this.content = content;
  17. }
  18. public String getChatTime() {
  19. return chatTime;
  20. }
  21. public void setChatTime(String chatTime) {
  22. this.chatTime = chatTime;
  23. }
  24. public boolean isComeMsg() {
  25. return isComeMsg;
  26. }
  27. public void setComeMsg(boolean isComeMsg) {
  28. this.isComeMsg = isComeMsg;
  29. }
  30. }

最后就是主Activity,这里面包括了自己写的BaseAdapter:

[java] view plaincopy
  1. public class ChatDemoActivity extends Activity {
  2. private Button sendButton = null;
  3. private EditText contentEditText = null;
  4. private ListView chatListView = null;
  5. private List<ChatEntity> chatList = null;
  6. private ChatAdapter chatAdapter = null;
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. requestWindowFeature(Window.FEATURE_NO_TITLE);
  11. setContentView(R.layout.main);
  12. contentEditText = (EditText) this.findViewById(R.id.et_content);
  13. sendButton = (Button) this.findViewById(R.id.btn_send);
  14. chatListView = (ListView) this.findViewById(R.id.listview);
  15. chatList = new ArrayList<ChatEntity>();
  16. ChatEntity chatEntity = null;
  17. for (int i = 0; i < 2; i++) {
  18. chatEntity = new ChatEntity();
  19. if (i % 2 == 0) {
  20. chatEntity.setComeMsg(false);
  21. chatEntity.setContent("Hello");
  22. chatEntity.setChatTime("2012-09-20 15:12:32");
  23. }else {
  24. chatEntity.setComeMsg(true);
  25. chatEntity.setContent("Hello,nice to meet you!");
  26. chatEntity.setChatTime("2012-09-20 15:13:32");
  27. }
  28. chatList.add(chatEntity);
  29. }
  30. chatAdapter = new ChatAdapter(this,chatList);
  31. chatListView.setAdapter(chatAdapter);
  32. sendButton.setOnClickListener(new OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. if (!contentEditText.getText().toString().equals("")) {
  36. //发送消息
  37. send();
  38. }else {
  39. Toast.makeText(ChatDemoActivity.this, "Content is empty", Toast.LENGTH_SHORT).show();
  40. }
  41. }
  42. });
  43. }
  44. private void send(){
  45. ChatEntity chatEntity = new ChatEntity();
  46. chatEntity.setChatTime("2012-09-20 15:16:34");
  47. chatEntity.setContent(contentEditText.getText().toString());
  48. chatEntity.setComeMsg(false);
  49. chatList.add(chatEntity);
  50. chatAdapter.notifyDataSetChanged();
  51. chatListView.setSelection(chatList.size() - 1);
  52. contentEditText.setText("");
  53. }
  54. private  class ChatAdapter extends BaseAdapter{
  55. private Context context = null;
  56. private List<ChatEntity> chatList = null;
  57. private LayoutInflater inflater = null;
  58. private int COME_MSG = 0;
  59. private int TO_MSG = 1;
  60. public ChatAdapter(Context context,List<ChatEntity> chatList){
  61. this.context = context;
  62. this.chatList = chatList;
  63. inflater = LayoutInflater.from(this.context);
  64. }
  65. @Override
  66. public int getCount() {
  67. return chatList.size();
  68. }
  69. @Override
  70. public Object getItem(int position) {
  71. return chatList.get(position);
  72. }
  73. @Override
  74. public long getItemId(int position) {
  75. return position;
  76. }
  77. @Override
  78. public int getItemViewType(int position) {
  79. // 区别两种view的类型,标注两个不同的变量来分别表示各自的类型
  80. ChatEntity entity = chatList.get(position);
  81. if (entity.isComeMsg())
  82. {
  83. return COME_MSG;
  84. }else{
  85. return TO_MSG;
  86. }
  87. }
  88. @Override
  89. public int getViewTypeCount() {
  90. // 这个方法默认返回1,如果希望listview的item都是一样的就返回1,我们这里有两种风格,返回2
  91. return 2;
  92. }
  93. @Override
  94. public View getView(int position, View convertView, ViewGroup parent) {
  95. ChatHolder chatHolder = null;
  96. if (convertView == null) {
  97. chatHolder = new ChatHolder();
  98. if (chatList.get(position).isComeMsg()) {
  99. convertView = inflater.inflate(R.layout.chat_from_item, null);
  100. }else {
  101. convertView = inflater.inflate(R.layout.chat_to_item, null);
  102. }
  103. chatHolder.timeTextView = (TextView) convertView.findViewById(R.id.tv_time);
  104. chatHolder.contentTextView = (TextView) convertView.findViewById(R.id.tv_content);
  105. chatHolder.userImageView = (ImageView) convertView.findViewById(R.id.iv_user_image);
  106. convertView.setTag(chatHolder);
  107. }else {
  108. chatHolder = (ChatHolder)convertView.getTag();
  109. }
  110. chatHolder.timeTextView.setText(chatList.get(position).getChatTime());
  111. chatHolder.contentTextView.setText(chatList.get(position).getContent());
  112. chatHolder.userImageView.setImageResource(chatList.get(position).getUserImage());
  113. return convertView;
  114. }
  115. private class ChatHolder{
  116. private TextView timeTextView;
  117. private ImageView userImageView;
  118. private TextView contentTextView;
  119. }
  120. }
  121. }

源代码下载:ChatDemo

仿微信聊天气泡效果实现,有源代码(一)相关推荐

  1. 仿微信聊天气泡效果实现

    微信聊天窗口的信息效果类似iphone上的短信效果,以气泡的形式展现,在Android上,实现这种效果主要用到ListView和BaseAdapter,配合布局以及相关素材,就可以自己做出这个效果,素 ...

  2. HTML5实现微信聊天气泡效果

    最近做一个HybridApp,前端有一个群聊的功能,于是就想模仿微信的聊天界面,先看效果图: HTML代码: <!DOCTYPE html> <html lang="en& ...

  3. php 仿微信朋友圈,HTML5仿微信聊天界面和朋友圈代码

    这几天使用H5开发了一个仿微信聊天前端界面,尤其微信底部编辑器那块处理的很好,使用HTML5来开发,虽说功能效果并没有微信那么全,但是也相当不错了,可以发送消息.表情,发送的消息自动回滚定位到底部,另 ...

  4. android 仿微信聊天气泡显示图片,实现仿照微信聊天气泡里显示图片效果的自定义View...

    第一部分是自定义的气泡效果的View,代码如下 public class BitmapShapeView extends View { private Paint mPaint; private Pa ...

  5. android 仿微信聊天气泡显示图片,怎么实现微信聊天时的气泡图(一)

    首先,微信聊天的时候气泡图是什么样呢?上图, , 要实现这种气泡图,要怎么做? 主要的就是实现那个小三角吗?首先想到的肯定是使用伪元素+定位,对!!! 那我们来试一下,上代码点这里,这样很轻松的实现了 ...

  6. 自定义Android聊天气泡ChatView。仿微信聊天气泡,能自定义边框,颜色,点击特效。

    原博客地址:https://blog.csdn.net/weixin_40400031/article/details/90755036 引言:最近公司准备做一款即时通讯的APP,就照着微信的功能模块 ...

  7. WPF 仿微信聊天气泡

    先上图 GitHub 参考文章: WPF下聊天气泡的实现

  8. android 仿微信聊天气泡显示图片,仿微信聊天气泡 图片尖角 按下变暗

    实现微信气泡图片尖角 //-------------gen corner bitmap flow------------------------ //load the bg: .9.png which ...

  9. Android 仿微信聊天气泡

    第一次写博客,遇见了这样的需求,当时看见那个角就有点触了,想到了自定义去实现但是思路不是很明确,跟老大请教了下,给了我思路就开始上手,但是还是出不来想要的效果,最后功夫不负有心人啊,上效果图吧, 参考 ...

最新文章

  1. Oracle中的字符处理方法
  2. Windows 10下安装Miniconda3
  3. pandas将满足某列的值挑出
  4. 关于学生信息录入(文件操作)的心得体会
  5. 如何保存Tensorflow中的Tensor参数,保存训练中的中间参数,存储卷积层的数据
  6. Linux redhat下安装swftools(转载后修改)
  7. MySQL 关闭子表的外键约束检察
  8. 编辑器,webstorm,phpstorm系列配置方法汇总-笔记
  9. ​MobileViT 它来了!Apple 提出轻量、通用、适用于移动设备的Transformer!
  10. 批处理框架 Spring Batch,数据迁移量过大如何保证内存?
  11. 公司内网与外网连通中的一些小问题(达内)
  12. C语言之数组的正向逆向输出
  13. 运用流体布局的html代码,div+css布局之流体浮动布局_html/css_WEB-ITnose
  14. win2003控制面板不见了,打开“控制面板”的方法,安全策略
  15. 巴比特 | 元宇宙每日必读:HTC 宣布推出首款元宇宙手机,售价约2700元人民币,都有哪些新玩法?...
  16. 【系统集成项目管理】之项目质量管理
  17. html 保存 mysql file_前端HTML5几种存储方式的总结
  18. Ant Design Pro右上角个人设置管理
  19. 图像基础知识学习笔记
  20. cocos2dx 植物大战僵尸 18 土豆雷

热门文章

  1. arduino nano学习过程中的相关程序
  2. abb机器人指令手册_爆!!ABB机器人支持OPC UA了
  3. 显示远程服务器图片与映射远程服务器到本地
  4. 实验十六 matplotlib数据可视化
  5. matlab出图时汉字都变成方框_汉字显示成方框的问题
  6. TCP三次握手、四次挥手
  7. Manacher (马拉车)算法
  8. 2012-1-18 不死何生 向死而生
  9. Termux安装数据库(手机安装数据库)...
  10. 王小川想走张一鸣和黄峥的老路,但终点不同