转-Android仿微信气泡聊天界面设计
微信的气泡聊天是仿iPhone自带短信而设计出来的,不过感觉还不错可以尝试一下仿着微信的气泡聊天做一个Demo,给大家分享一下!效果图如下:
气泡聊天最终要的是素材,要用到9.png文件的素材,这样气泡会随着聊天内容的多少而改变气泡的大小且不失真。为了方便,我就直接在微信里面提取出来啦。
聊天的内容是用ListView来显示的,将聊天的内容封装成一个ChatMsgEntity类的对象里面,然后交给自定义的ListView适配器将聊天内容显示出来。
ChatMsgEntity.java比较简单,只是聊天的内容存储、设置和获取。
- package com.example.school;
- public class ChatMsgEntity {
- private static final String TAG = ChatMsgEntity.class.getSimpleName();
- //名字
- private String name;
- //日期
- private String date;
- //聊天内容
- private String text;
- //是否为对方发来的信息
- private boolean isComMeg = true;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getDate() {
- return date;
- }
- public void setDate(String date) {
- this.date = date;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- public boolean getMsgType() {
- return isComMeg;
- }
- public void setMsgType(boolean isComMsg) {
- isComMeg = isComMsg;
- }
- public ChatMsgEntity() {
- }
- public ChatMsgEntity(String name, String date, String text, boolean isComMsg) {
- this.name = name;
- this.date = date;
- this.text = text;
- this.isComMeg = isComMsg;
- }
- }
显示ListView的适配器ChatMsgViewAdpater.java:
- package com.example.school;
- import android.R.integer;
- import android.content.Context;
- import android.database.DataSetObserver;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnLongClickListener;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- public class ChatMsgViewAdapter extends BaseAdapter {
- //ListView视图的内容由IMsgViewType决定
- public static interface IMsgViewType
- {
- //对方发来的信息
- int IMVT_COM_MSG = 0;
- //自己发出的信息
- int IMVT_TO_MSG = 1;
- }
- private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();
- private List<ChatMsgEntity> data;
- private Context context;
- private LayoutInflater mInflater;
- public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> data) {
- this.context = context;
- this.data = data;
- mInflater = LayoutInflater.from(context);
- }
- //获取ListView的项个数
- public int getCount() {
- return data.size();
- }
- //获取项
- public Object getItem(int position) {
- return data.get(position);
- }
- //获取项的ID
- public long getItemId(int position) {
- return position;
- }
- //获取项的类型
- public int getItemViewType(int position) {
- // TODO Auto-generated method stub
- ChatMsgEntity entity = data.get(position);
- if (entity.getMsgType())
- {
- return IMsgViewType.IMVT_COM_MSG;
- }else{
- return IMsgViewType.IMVT_TO_MSG;
- }
- }
- //获取项的类型数
- public int getViewTypeCount() {
- // TODO Auto-generated method stub
- return 2;
- }
- //获取View
- public View getView(int position, View convertView, ViewGroup parent) {
- ChatMsgEntity entity = data.get(position);
- boolean isComMsg = entity.getMsgType();
- ViewHolder viewHolder = null;
- if (convertView == null)
- {
- if (isComMsg)
- {
- //如果是对方发来的消息,则显示的是左气泡
- convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);
- }else{
- //如果是自己发出的消息,则显示的是右气泡
- convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
- }
- viewHolder = new ViewHolder();
- viewHolder.tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
- viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
- viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
- viewHolder.isComMsg = isComMsg;
- convertView.setTag(viewHolder);
- }else{
- viewHolder = (ViewHolder) convertView.getTag();
- }
- viewHolder.tvSendTime.setText(entity.getDate());
- viewHolder.tvUserName.setText(entity.getName());
- viewHolder.tvContent.setText(entity.getText());
- return convertView;
- }
- //通过ViewHolder显示项的内容
- static class ViewHolder {
- public TextView tvSendTime;
- public TextView tvUserName;
- public TextView tvContent;
- public boolean isComMsg = true;
- }
- }
对方发来消息的显示布局chatting_item_msg_text_left.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:padding="6dp">
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:gravity="center_horizontal">
- <TextView
- android:id="@+id/tv_sendtime"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="@style/chat_text_date_style"/>
- </LinearLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dp" >
- <ImageView
- android:id="@+id/iv_userhead"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:background="@drawable/mini_avatar_shadow"/>
- <TextView
- android:id="@+id/tv_chatcontent"
- android:layout_toRightOf="@id/iv_userhead"
- android:layout_marginLeft="10dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/chatfrom_bg"
- style="@style/chat_content_date_style"/>
- <TextView
- android:id="@+id/tv_username"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/iv_userhead"
- android:layout_alignParentLeft="true"
- android:layout_toLeftOf="@id/tv_chatcontent"
- style="@style/chat_text_name_style"/>
- </RelativeLayout>
- </LinearLayout>
chatting_item_msg_text_right.xml和上面差不多,只是气泡和头像的方向在右边,就不贴代码了。
主界面的布局chat.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#f0f0e0" >
- <RelativeLayout
- android:id="@+id/rl_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/title" >
- <Button
- android:id="@+id/btn_back"
- android:gravity="center_vertical"
- android:layout_marginLeft="5dp"
- android:paddingLeft="18dp"
- android:textSize="18.0sp"
- android:textColor="#ffffff"
- android:background="@drawable/selector_btn_back"
- android:layout_width="70dp"
- android:layout_height="wrap_content"
- android:text="@string/back" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/school_title_name"
- android:layout_centerInParent="true"
- style="@style/my_txt"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/campus_info"
- android:layout_centerVertical="true"
- android:layout_alignParentRight="true"
- android:layout_marginRight="15dp"/>
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/rl_bottom"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:background="@drawable/layout_bg1" >
- <Button
- android:id="@+id/btn_send"
- android:layout_width="60dp"
- android:layout_height="40dp"
- android:layout_alignParentRight="true"
- android:layout_marginRight="10dp"
- android:layout_centerVertical="true"
- android:text="@string/send" />
- <EditText
- android:id="@+id/et_sendmessage"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:layout_toLeftOf="@id/btn_send"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:background="@drawable/edittext1"
- android:layout_centerVertical="true"
- android:singleLine="true"
- android:textSize="18sp"/>
- </RelativeLayout>
- <ListView
- android:id="@+id/listview"
- android:layout_below="@id/rl_layout"
- android:layout_above="@id/rl_bottom"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10.0dip"
- android:layout_marginTop="10.0dip"
- android:layout_marginRight="10.0dip"
- android:divider="@null"
- android:dividerHeight="5dp"
- android:scrollbars="none"
- android:cacheColorHint="#00000000"/>
- </RelativeLayout>
ChatActivity.java
- package com.example.chat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import com.example.school.R;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.View;
- import android.view.Window;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- public class ChatActivity extends Activity implements OnClickListener {
- private Button mBtnSend;
- private Button mBtnBack;
- private EditText mEditTextContent;
- //聊天内容的适配器
- private ChatMsgViewAdapter mAdapter;
- private ListView mListView;
- //聊天的内容
- private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
- setContentView(R.layout.chat);
- initView();
- initData();
- }
- //初始化视图
- private void initView() {
- mListView = (ListView) findViewById(R.id.listview);
- mBtnBack = (Button) findViewById(R.id.btn_back);
- mBtnBack.setOnClickListener(this);
- mBtnSend = (Button) findViewById(R.id.btn_send);
- mBtnSend.setOnClickListener(this);
- mEditTextContent = (EditText) findViewById(R.id.et_sendmessage);
- }
- private String[] msgArray = new String[]{" 孩子们,要好好学习,天天向上!要好好听课,不要翘课!不要挂科,多拿奖学金!三等奖学金的争取拿二等,二等的争取拿一等,一等的争取拿励志!",
- "姚妈妈还有什么吩咐...",
- "还有,明天早上记得跑操啊,不来的就扣德育分!",
- "德育分是什么?扣了会怎么样?",
- "德育分会影响奖学金评比,严重的话,会影响毕业",
- "哇!学院那么不人道?",
- "你要是你不听话,我当场让你不能毕业!",
- "姚妈妈,我知错了(- -我错在哪了...)"};
- private String[]dataArray = new String[]{"2012-09-01 18:00", "2012-09-01 18:10",
- "2012-09-01 18:11", "2012-09-01 18:20",
- "2012-09-01 18:30", "2012-09-01 18:35",
- "2012-09-01 18:40", "2012-09-01 18:50"};
- private final static int COUNT = 8;
- //初始化要显示的数据
- private void initData() {
- for(int i = 0; i < COUNT; i++) {
- ChatMsgEntity entity = new ChatMsgEntity();
- entity.setDate(dataArray[i]);
- if (i % 2 == 0)
- {
- entity.setName("姚妈妈");
- entity.setMsgType(true);
- }else{
- entity.setName("Shamoo");
- entity.setMsgType(false);
- }
- entity.setText(msgArray[i]);
- mDataArrays.add(entity);
- }
- mAdapter = new ChatMsgViewAdapter(this, mDataArrays);
- mListView.setAdapter(mAdapter);
- }
- public void onClick(View view) {
- // TODO Auto-generated method stub
- switch(view.getId()) {
- case R.id.btn_back:
- back();
- break;
- case R.id.btn_send:
- send();
- break;
- }
- }
- private void send()
- {
- String contString = mEditTextContent.getText().toString();
- if (contString.length() > 0)
- {
- ChatMsgEntity entity = new ChatMsgEntity();
- entity.setDate(getDate());
- entity.setName("");
- entity.setMsgType(false);
- entity.setText(contString);
- mDataArrays.add(entity);
- mAdapter.notifyDataSetChanged();
- mEditTextContent.setText("");
- mListView.setSelection(mListView.getCount() - 1);
- }
- }
- //获取日期
- private String getDate() {
- Calendar c = Calendar.getInstance();
- String year = String.valueOf(c.get(Calendar.YEAR));
- String month = String.valueOf(c.get(Calendar.MONTH));
- String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
- String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
- String mins = String.valueOf(c.get(Calendar.MINUTE));
- StringBuffer sbBuffer = new StringBuffer();
- sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":" + mins);
- return sbBuffer.toString();
- }
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- back();
- return true;
- }
- private void back() {
- finish();
- }
- }
谢谢大家的支持!链接:点击打开链接
需要一分资源分,大家评论一下那一分就还回来了,哈哈~~
转自http://blog.csdn.net/pocoyoshamoo/article/details/9674385
转-Android仿微信气泡聊天界面设计的更多相关文章
- Android仿微信气泡聊天界面设计
微信的气泡聊天是仿iPhone自带短信而设计出来的,不过感觉还不错可以尝试一下仿着微信的气泡聊天做一个Demo,给大家分享一下!效果图如下: 气泡聊天最终要的是素材,要用到9.png文件的素材,这样气 ...
- android 仿QQ气泡聊天界面
1.现在的QQ,微信等一些APP的聊天界面都是气泡聊天界面,左边是接收到的消息,右边是发送的消息, 这个效果其实就是一个ListView在加载它的Item的时候,分别用了不同的布局xml文件. 2.效 ...
- Android仿微信SlideView聊天列表滑动删除效果
package com.ryg.slideview; import com.ryg.slideview.MainActivity.MessageItem; //Download by http://w ...
- 【手把手教程】uniapp + vue 从0搭建仿微信App聊天应用:腾讯云TXIM即时通讯的最佳实践
基于uniapp + vue 实现仿微信App聊天应用实践,实现以下功能 1: 用户登陆 2: 聊天会话管理 3: 文本/图片/视频/定位消息收发 4: 贴图表情消息收发 5: 一对一语音视频在线通话 ...
- uniapp+nvue实现仿微信App聊天应用 —— 成功实现好友聊天+语音视频通话功能
基于uniapp + nvue实现的uniapp仿微信App聊天应用 txim 实例项目,实现了以下功能. 1: 聊天会话管理 2: 好友列表 3: 文字.语音.视频.表情.位置等聊天消息收发 4: ...
- Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等
仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...
- Tauri-Vue3桌面端聊天室|tauri+vite3仿微信|tauri聊天程序EXE
基于tauri+vue3.js+vite3跨桌面端仿微信聊天实例TauriVue3Chat. tauri-chat 运用最新tauri+vue3+vite3+element-plus+v3layer等 ...
- Android 仿微信小视频录制
Android 仿微信小视频录制 WechatShortVideo和WechatShortVideo文章
- android 仿微信聊天界面,以及语音录制功能
extends:http://104zz.iteye.com/blog/1709840 本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图: 第一:chat.xml设计 ...
随机推荐
- 一款bootstrap树形js
http://www.htmleaf.com/Demo/201502141380.html
- Ubuntu 16.04 nvidia安装
Ubuntu更新完NVIDIA驱动后,重启电脑进入不了系统,一直处于登录界面.后来重启电脑时发现我进入不了系统了,输入我的登录密码会发现屏幕一闪,然后又重新跳回到登录界面,就是进入了login loo ...
- 夺命雷公狗-----React---2--组建
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JS中同名函数有效执行顺序
html中如果出现函数同名时:如果有多个外部引入的js文件,例如a.js和b.js(引入顺序假定是a.js,然后是b.js),同时html中本身也有内部的js.那么针对 出现函数名一样的情况时,无论他 ...
- JQ 操作 radio、checkbox 、select
MXS&Vincene ─╄OvЁ &0000026─╄OvЁ MXS&Vincene MXS&Vincene ─╄OvЁ:今天很残酷,明天更残酷,后天很美好, ...
- IOS手势UIGestureRecognizer
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有6个子类处理具体的手势: 1.UITapGestureRecognizer (任意手指任意次数的点击) // 点击次数 ...
- 嵌入式Linux内核制作【转】
本文转载自:http://blog.csdn.net/coding__madman/article/details/51291316 1. Linux体系结构 从整体上来分,linux可以分为User ...
- Java 中System里getProperty(something)
Java 中System里getProperty 方法获得系统参数 Key Description of Associated Value 中文描述 java.version Java Runtime ...
- HTML5正则表单式验证电子邮件
<input type="text" title="email" required pattern="[^@]+@[^@]+\.[a-zA-Z] ...
- LA 3713 宇航员分组
题目链接:http://vjudge.net/contest/142615#problem/B 题意:有A,B,C三个人物要分配个N个宇航员,每个宇航员恰好要分配一个任务,设平均年龄为X,只有年龄大于 ...