Android环信设置聊天头像问题
最近在做即时通讯,一直搞不出用户头像,在网上找了好多方法也没实现,就想到了直接改EaseUi中的代码来实现头像的展示
首先在发送消息时,设置
在重写它的适配器,我是全复制下来重写改了一个名字
public class ChatEaseMessageAdapter extends BaseAdapter {private final static String TAG = "msg"; private Context context; private static final int HANDLER_MESSAGE_REFRESH_LIST = 0; private static final int HANDLER_MESSAGE_SELECT_LAST = 1; private static final int HANDLER_MESSAGE_SEEK_TO = 2; private static final int MESSAGE_TYPE_RECV_TXT = 0; private static final int MESSAGE_TYPE_SENT_TXT = 1; private static final int MESSAGE_TYPE_SENT_IMAGE = 2; private static final int MESSAGE_TYPE_SENT_LOCATION = 3; private static final int MESSAGE_TYPE_RECV_LOCATION = 4; private static final int MESSAGE_TYPE_RECV_IMAGE = 5; private static final int MESSAGE_TYPE_SENT_VOICE = 6; private static final int MESSAGE_TYPE_RECV_VOICE = 7; private static final int MESSAGE_TYPE_SENT_VIDEO = 8; private static final int MESSAGE_TYPE_RECV_VIDEO = 9; private static final int MESSAGE_TYPE_SENT_FILE = 10; private static final int MESSAGE_TYPE_RECV_FILE = 11; private static final int MESSAGE_TYPE_SENT_EXPRESSION = 12; private static final int MESSAGE_TYPE_RECV_EXPRESSION = 13; public int itemTypeCount; // reference to conversation object in chatsdk private EMConversation conversation; EMMessage[] messages = null; private String toChatUsername; private EaseChatMessageList.MessageListItemClickListener itemClickListener; private EaseCustomChatRowProvider customRowProvider; private boolean showUserNick; private boolean showAvatar; private Drawable myBubbleBg; private Drawable otherBuddleBg; private ListView listView; private EaseMessageListItemStyle itemStyle; private String avatar=""; public ChatEaseMessageAdapter(Context context, String username, int chatType, ListView listView) {this.context = context; this.listView = listView; toChatUsername = username; avatar= SharedPreUtils.getStringValue(context,"user_headpic",""); this.conversation = EMClient.getInstance().chatManager().getConversation(username, EaseCommonUtils.getConversationType(chatType), true); }Handler handler = new Handler() {private void refreshList() {// you should not call getAllMessages() in UI thread // otherwise there is problem when refreshing UI and there is new message arrive java.util.List<EMMessage> var = conversation.getAllMessages(); messages = var.toArray(new EMMessage[var.size()]); conversation.markAllMessagesAsRead(); notifyDataSetChanged(); }@Override public void handleMessage(android.os.Message message) {switch (message.what) {case HANDLER_MESSAGE_REFRESH_LIST:refreshList(); break; case HANDLER_MESSAGE_SELECT_LAST:if (messages != null && messages.length > 0) {listView.setSelection(messages.length - 1); }break; case HANDLER_MESSAGE_SEEK_TO:int position = message.arg1; listView.setSelection(position); break; default:break; }}}; public void refresh() {if (handler.hasMessages(HANDLER_MESSAGE_REFRESH_LIST)) {return; }android.os.Message msg = handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST); handler.sendMessage(msg); }/** * refresh and select the last */ public void refreshSelectLast() {final int TIME_DELAY_REFRESH_SELECT_LAST = 100; handler.removeMessages(HANDLER_MESSAGE_REFRESH_LIST); handler.removeMessages(HANDLER_MESSAGE_SELECT_LAST); handler.sendEmptyMessageDelayed(HANDLER_MESSAGE_REFRESH_LIST, TIME_DELAY_REFRESH_SELECT_LAST); handler.sendEmptyMessageDelayed(HANDLER_MESSAGE_SELECT_LAST, TIME_DELAY_REFRESH_SELECT_LAST); }/** * refresh and seek to the position */ public void refreshSeekTo(int position) {handler.sendMessage(handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST)); }public EMMessage getItem(int position) {if (messages != null && position < messages.length) {return messages[position]; }return null; }public long getItemId(int position) {return position; }/** * get count of messages */ public int getCount() {return messages == null ? 0 : messages.length; }/** * get number of message type, here 14 = (EMMessage.Type) * 2 */ public int getViewTypeCount() {if(customRowProvider != null && customRowProvider.getCustomChatRowTypeCount() > 0){return customRowProvider.getCustomChatRowTypeCount() + 14; }return 14; }/** * get type of item */ public int getItemViewType(int position) {EMMessage message = getItem(position); if (message == null) {return -1; }if(customRowProvider != null && customRowProvider.getCustomChatRowType(message) > 0){return customRowProvider.getCustomChatRowType(message) + 13; }if (message.getType() == EMMessage.Type.TXT) {if(message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)){return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_EXPRESSION : MESSAGE_TYPE_SENT_EXPRESSION; }return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_TXT : MESSAGE_TYPE_SENT_TXT; }if (message.getType() == EMMessage.Type.IMAGE) {return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_IMAGE : MESSAGE_TYPE_SENT_IMAGE; }if (message.getType() == EMMessage.Type.LOCATION) {return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_LOCATION : MESSAGE_TYPE_SENT_LOCATION; }if (message.getType() == EMMessage.Type.VOICE) {return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VOICE : MESSAGE_TYPE_SENT_VOICE; }if (message.getType() == EMMessage.Type.VIDEO) {return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VIDEO : MESSAGE_TYPE_SENT_VIDEO; }if (message.getType() == EMMessage.Type.FILE) {return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_FILE : MESSAGE_TYPE_SENT_FILE; }return -1;// invalid }protected EaseChatRow createChatRow(Context context, EMMessage message, int position) {EaseChatRow chatRow = null; if(customRowProvider != null && customRowProvider.getCustomChatRow(message, position, this) != null){return customRowProvider.getCustomChatRow(message, position, this); }switch (message.getType()) {case TXT:if(message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)){chatRow = new EaseChatRowBigExpression(context, message, position, this,avatar); }else{chatRow = new ChatEaseChatRowText(context, message, position, this,avatar); }break; case LOCATION:chatRow = new EaseChatRowLocation(context, message, position, this,avatar); break; case FILE:chatRow = new EaseChatRowFile(context, message, position, this,avatar); break; case IMAGE:chatRow = new EaseChatRowImage(context, message, position, this,avatar); break; case VOICE:chatRow = new EaseChatRowVoice(context, message, position, this,avatar); break; case VIDEO:chatRow = new EaseChatRowVideo(context, message, position, this,avatar); break; default:break; }return chatRow; }@SuppressLint("NewApi")public View getView(final int position, View convertView, ViewGroup parent) {EMMessage message = getItem(position); if(convertView == null){convertView = createChatRow(context, message, position); }//refresh ui with messages ((EaseChatRow)convertView).setUpView(message, position, itemClickListener, itemStyle); return convertView; }public void setItemStyle(EaseMessageListItemStyle itemStyle){this.itemStyle = itemStyle; }public void setItemClickListener(EaseChatMessageList.MessageListItemClickListener listener){itemClickListener = listener; }public void setCustomChatRowProvider(EaseCustomChatRowProvider rowProvider){customRowProvider = rowProvider; }public boolean isShowUserNick() {return showUserNick; }public boolean isShowAvatar() {return showAvatar; }public Drawable getMyBubbleBg() {return myBubbleBg; }public Drawable getOtherBubbleBg() {return otherBuddleBg; }}
环信中聊天布局的展示都是继承于EaseChatRow,它是在这里加载用户头像的,于是我就直接在这里改:
public abstract class EaseChatRow extends LinearLayout {protected static final String TAG = EaseChatRow.class.getSimpleName(); protected LayoutInflater inflater; protected Context context; protected BaseAdapter adapter; protected EMMessage message; protected int position; protected TextView timeStampView; protected ImageView userAvatarView; protected View bubbleLayout; protected TextView usernickView; protected TextView percentageView; protected ProgressBar progressBar; protected ImageView statusView; protected Activity activity; protected TextView ackedView; protected TextView deliveredView; protected EMCallBack messageSendCallback; protected EMCallBack messageReceiveCallback; protected MessageListItemClickListener itemClickListener; protected EaseMessageListItemStyle itemStyle; private String avatar=""; public EaseChatRow(Context context, EMMessage message, int position, BaseAdapter adapter,String avatar) {super(context); this.context = context; this.activity = (Activity) context; this.message = message; this.position = position; this.adapter = adapter; this.avatar=avatar; inflater = LayoutInflater.from(context); initView(); }private void initView() {onInflateView(); timeStampView = (TextView) findViewById(R.id.timestamp); userAvatarView = (ImageView) findViewById(R.id.iv_userhead); bubbleLayout = findViewById(R.id.bubble); usernickView = (TextView) findViewById(R.id.tv_userid); progressBar = (ProgressBar) findViewById(R.id.progress_bar); statusView = (ImageView) findViewById(R.id.msg_status); ackedView = (TextView) findViewById(R.id.tv_ack); deliveredView = (TextView) findViewById(R.id.tv_delivered); onFindViewById(); }/** * set property according message and postion * * @param message * @param position */ public void setUpView(EMMessage message, int position, MessageListItemClickListener itemClickListener, EaseMessageListItemStyle itemStyle) {this.message = message; this.position = position; this.itemClickListener = itemClickListener; this.itemStyle = itemStyle; setUpBaseView(); onSetUpView(); setClickListener(); }private void setUpBaseView() {// set nickname, avatar and background of bubble TextView timestamp = (TextView) findViewById(R.id.timestamp); if (timestamp != null) {if (position == 0) {timestamp.setText(DateUtils.getTimestampString(new Date(message.getMsgTime()))); timestamp.setVisibility(View.VISIBLE); } else {// show time stamp if interval with last message is > 30 seconds EMMessage prevMessage = (EMMessage) adapter.getItem(position - 1); if (prevMessage != null && DateUtils.isCloseEnough(message.getMsgTime(), prevMessage.getMsgTime())) {timestamp.setVisibility(View.GONE); } else {timestamp.setText(DateUtils.getTimestampString(new Date(message.getMsgTime()))); timestamp.setVisibility(View.VISIBLE); }}}if(userAvatarView != null) {//set nickname and avatar if (message.direct() == Direct.SEND) {EaseUserUtils.setUserAvatar(context, avatar, userAvatarView);//avatar是自己的用户头像 } else {EaseUserUtils.setUserAvatar(context, message.getStringAttribute("avatar",""), userAvatarView);//发送过来的用户头像 EaseUserUtils.setUserNick(message.getFrom(), usernickView); }}if(deliveredView != null){if (message.isDelivered()) {deliveredView.setVisibility(View.VISIBLE); } else {deliveredView.setVisibility(View.INVISIBLE); }}if(ackedView != null){if (message.isAcked()) {if (deliveredView != null) {deliveredView.setVisibility(View.INVISIBLE); }ackedView.setVisibility(View.VISIBLE); } else {ackedView.setVisibility(View.INVISIBLE); }}if (itemStyle != null) {if (userAvatarView != null) {if (itemStyle.isShowAvatar()) {userAvatarView.setVisibility(View.VISIBLE); EaseAvatarOptions avatarOptions = EaseUI.getInstance().getAvatarOptions(); if(avatarOptions != null && userAvatarView instanceof EaseImageView){EaseImageView avatarView = ((EaseImageView)userAvatarView); if(avatarOptions.getAvatarShape() != 0)avatarView.setShapeType(avatarOptions.getAvatarShape()); if(avatarOptions.getAvatarBorderWidth() != 0)avatarView.setBorderWidth(avatarOptions.getAvatarBorderWidth()); if(avatarOptions.getAvatarBorderColor() != 0)avatarView.setBorderColor(avatarOptions.getAvatarBorderColor()); if(avatarOptions.getAvatarRadius() != 0)avatarView.setRadius(avatarOptions.getAvatarRadius()); }} else {userAvatarView.setVisibility(View.GONE); }}if (usernickView != null) {if (itemStyle.isShowUserNick())usernickView.setVisibility(View.VISIBLE); else usernickView.setVisibility(View.GONE); }if (bubbleLayout != null) {if (message.direct() == Direct.SEND) {if (itemStyle.getMyBubbleBg() != null) {bubbleLayout.setBackgroundDrawable(((EaseMessageAdapter) adapter).getMyBubbleBg()); }} else if (message.direct() == Direct.RECEIVE) {if (itemStyle.getOtherBubbleBg() != null) {bubbleLayout.setBackgroundDrawable(((EaseMessageAdapter) adapter).getOtherBubbleBg()); }}}}}/** * set callback for sending message */ protected void setMessageSendCallback(){if(messageSendCallback == null){messageSendCallback = new EMCallBack() {@Override public void onSuccess() {updateView(); }@Override public void onProgress(final int progress, String status) {activity.runOnUiThread(new Runnable() {@Override public void run() {if(percentageView != null)percentageView.setText(progress + "%"); }}); }@Override public void onError(int code, String error) {updateView(code, error); }}; }message.setMessageStatusCallback(messageSendCallback); }/** * set callback for receiving message */ protected void setMessageReceiveCallback(){if(messageReceiveCallback == null){messageReceiveCallback = new EMCallBack() {@Override public void onSuccess() {updateView(); }@Override public void onProgress(final int progress, String status) {activity.runOnUiThread(new Runnable() {public void run() {if(percentageView != null){percentageView.setText(progress + "%"); }}}); }@Override public void onError(int code, String error) {updateView(); }}; }message.setMessageStatusCallback(messageReceiveCallback); }private void setClickListener() {if(bubbleLayout != null){bubbleLayout.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {if (itemClickListener != null){if(!itemClickListener.onBubbleClick(message)){// if listener return false, we call default handling onBubbleClick(); }}}}); bubbleLayout.setOnLongClickListener(new OnLongClickListener() {@Override public boolean onLongClick(View v) {if (itemClickListener != null) {itemClickListener.onBubbleLongClick(message); }return true; }}); }if (statusView != null) {statusView.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {if (itemClickListener != null) {itemClickListener.onResendClick(message); }}}); }if(userAvatarView != null){userAvatarView.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {if (itemClickListener != null) {if (message.direct() == Direct.SEND) {itemClickListener.onUserAvatarClick(EMClient.getInstance().getCurrentUser()); } else {itemClickListener.onUserAvatarClick(message.getFrom()); }}}}); userAvatarView.setOnLongClickListener(new OnLongClickListener() {@Override public boolean onLongClick(View v) {if(itemClickListener != null){if (message.direct() == Direct.SEND) {itemClickListener.onUserAvatarLongClick(EMClient.getInstance().getCurrentUser()); } else {itemClickListener.onUserAvatarLongClick(message.getFrom()); }return true; }return false; }}); }}protected void updateView() {activity.runOnUiThread(new Runnable() {public void run() {if (message.status() == EMMessage.Status.FAIL) {Toast.makeText(activity,activity.getString(R.string.send_fail) + activity.getString(R.string.connect_failuer_toast), Toast.LENGTH_SHORT).show(); }onUpdateView(); }}); }protected void updateView(final int errorCode, final String desc) {activity.runOnUiThread(new Runnable() {public void run() {if (errorCode == EMError.MESSAGE_INCLUDE_ILLEGAL_CONTENT) {Toast.makeText(activity,activity.getString(R.string.send_fail) + activity.getString(R.string.error_send_invalid_content), Toast.LENGTH_SHORT).show(); } else if (errorCode == EMError.GROUP_NOT_JOINED) {Toast.makeText(activity,activity.getString(R.string.send_fail) + activity.getString(R.string.error_send_not_in_the_group), Toast.LENGTH_SHORT).show(); } else {Toast.makeText(activity,activity.getString(R.string.send_fail) + activity.getString(R.string.connect_failuer_toast), Toast.LENGTH_SHORT).show(); }onUpdateView(); }}); }protected abstract void onInflateView(); /** * find view by id */ protected abstract void onFindViewById(); /** * refresh list view when message status change */ protected abstract void onUpdateView(); /** * setup view * */ protected abstract void onSetUpView(); /** * on bubble clicked */ protected abstract void onBubbleClick(); }
到此用户头像的问题就解决了
Android环信设置聊天头像问题相关推荐
- Android 环信 自定义聊天气泡
在EaseChatRowText.java文件中修改onInflatView方法里的布局
- Android Studio 环信IM聊天设置用户头像
环信IM中的DemoHelper提供了实现设置聊天头像的方法,但是我们有时候不希望按照demo的基础进行开发,或者把整个demo作为依赖导入主项目中.其实在easeui中提供了一个EaseUserPr ...
- android 头像简称,Android环信显示头像及昵称的简单方法
在我们日常应用开发中,很多时候产品需要IM聊天功能.考虑到时间.难度等问题一般会选择集成一些比较稳定的第三方SDK来实现功能.最近的项目也有IM需求,因为之前用过环信,感觉还不错,所以就再次使用了环信 ...
- android 环信客服修改自己的头像
因为自己项目目中遇到到了,有自己的需求,所以就修改了环信自己的头像,客服的头像也修改了,好了废话不多说了上干活 首先找到easeui中的类EaseUserUtils 不过我把图片都保存到本 ...
- 环信php修改头像,环信第二波 更改聊天头像,昵称等问题
基于上次那篇有关环信的个人集成方法被环信周刊官方收录以后,一直没有再写环信相关知识. 今天我将在这继续写一下关于环信获取用户头像和昵称的方法.环信官方文档里面有两种方法, 1:通过自己后台根据用户ID ...
- Android环信3.0即时通讯云入门指北
Android环信3.0即时通讯云入门指北 官方文档 http://docs-im.easemob.com/im/android/sdk/import 基础集成 http://docs-im.ease ...
- android项目模块导入eclipse编译报错,android环信demo导入eclipse编译出错
官网最新下载的android环信sdk,导入编译出错:java.lang.RuntimeException: Unable to instantiate application com.hyphena ...
- 聊天服务器 单机性能,环信即时聊天服务器
环信即时聊天服务器 内容精选 换一换 本章节通过示例项目"小蝌蚪即时交互游戏"介绍如何使用DevCloud开发基于PHP语言的H5应用.项目名称:小蝌蚪即时交互游戏.项目简介:小蝌 ...
- Android 环信IM接受离线消息
Android 环信IM接受离线消息 已经按照官方文档上面写了,结果还是没有接受到离线消息,但是离线消息确实显示在了列表里面. 实际上 android 环信IM是有这个方法的,只是平时没有开启而已.我 ...
最新文章
- mysql timestamp json_mysql中timestamp,datetime,int类型的区别与优劣
- Django中使用UpdateView修改数据后,返回列表页
- java web 总结,Java Web 相关概念经典总结(一)
- python绘制灰度图片直方图-opencv+python 统计及绘制直方图
- jquery ajax跨域asp,jQuery跨域调用Asp.Net Web API
- Shell教程(二):变量、特殊变量
- MongoDB学习笔记~MongoDB实体中的值对象
- python doc_2019-2020年Python3中文教程完整版.doc
- Spring 框架基础(02):Bean的生命周期,作用域,装配总结
- 机器学习分类_机器学习之简单分类模型
- 流利说CFO辞职 公司上市一年多股价跌去一半
- mysql觸發器_mysql觸發器
- 渗透测试流程信息收集
- [Ext JS6]路由(Routing)及使用
- 正常的vite创建项目并且安装vue router,vant的代码示例
- C#转换人民币大小金额
- 英特尔vPro博锐技术激活
- 如何成为一名程序员?
- mysql创建、修改、删除索引和主键
- 午芯高科WXP380气压传感器