仿QQ聊天(4)—简单的聊天页面实现

点击好友item,跳转到PersonalCardActivity(名片页面),

这里写图片描述
之前一直在想怎么把这个圆形头像放到这个位置,想到的办法就是就一个帧布局,给帧布局指定一个坐标,让他固定在那里,这样就能浮在神仙姐姐和Timor小队长中间的位置。但实际上,这个页面是可以滑动的,如果把坐标定死,它就不能滑动了,想了很久也没弄好。
后来胸屌告诉我一个方法,把Timor小队长这张图片,还有神仙姐姐这个item。统一放到一个帧布局中,然后再固定神仙姐姐的头像。这样一来神仙姐姐的头像就会和这个帧布局是一体的,就能一起滑动了,具体的布局代码可以去看源代码。
胸屌真聪明,胸屌屌真大。
之前我的思想一直拘泥在这一个小小的神仙姐姐头像,一直想着怎么去布局它,但是如果把目光放大一点,把他放在这个帧布局中,这问题一下就解决了。
很多时候,我们是不是也该把自己的目光放大一点,长远一点?
点击发消息按钮跳转到ChatActivity(聊天界面)。

如果只实现简单的文字发送的话就很简单了,但是发送表情和图片就有点难了。我的效果虽然做出来了,但是实在是写的很差。以后再改,这里先实现最简单的发送文字的聊天界面。
这里写图片描述
首先需要写一个消息实体类:ChatMsgEntity

private String text;//消息内容
private String date;//发送消息的日期
private Boolean isComMsg;//是我还是队友发送的消息

类中有三个属性,然后是set and get方法。
模拟两个消息实体类,并把他们放到一个list集合中保存。

private List<ChatMsgEntity> listEntity = new ArrayList<ChatMsgEntity>();

SimpleDateFormat formatter = new SimpleDateFormat("MM.dd  HH:mm");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String str = formatter.format(curDate);

        ChatMsgEntity entity1 = new ChatMsgEntity();
        entity1.setDate(str);
        entity1.setText("队友,来打游戏吧");
        entity1.setMsgType(false);

        ChatMsgEntity entity2 = new ChatMsgEntity();
        entity2.setDate(str);
        entity2.setText("不打了,你自己玩儿吧");
        entity2.setMsgType(true);
        listEntity.add(entity1);
        listEntity.add(entity2);

        adapter = new ChatMsgAdapter(context, listEntity,
                friendsInfo);
        lv_chat.setAdapter(adapter);

lv_chat就是中间部分的listview,然后用适配器给listView添加数据。

package com.example.adapter;

import java.util.List;

import com.example.bean.ChatMsgEntity;
import com.example.bean.FriendsInfo;
import com.example.testqqchatui.R;
import com.example.utils.CircleImageView;
import com.example.utils.FileUtils;
import com.example.utils.RoundImageView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author 作者:xsl
 * @version 创建时间 :2015/10/27
 * 类说明:聊天适配器
 */

public class ChatMsgAdapter extends BaseAdapter{
    private Context context;
    private List<ChatMsgEntity> listEntity;
    private FriendsInfo friendsInfo;
    private LayoutInflater inflater;

    public ChatMsgAdapter(Context context,
            List<ChatMsgEntity> listEntity,FriendsInfo friendsInfo) {
        this.context = context;
        this.listEntity = listEntity;
        this.friendsInfo = friendsInfo;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listEntity.size();
    }

    @Override
    public Object getItem(int position) {
        return listEntity.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("null")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChatMsgEntity entity = listEntity.get(position);
        View view = null;
        //判断消息的类型,根据类型判断需要加载左边还是右边的item
        if(entity.getMsgType()){
            view = inflater.inflate(R.layout.chatting_item_msg_text_left,null);
            //头像
            RoundImageView iv_userhead = (RoundImageView)view.findViewById(R.id.iv_userhead);
            iv_userhead.setImageResource(friendsInfo.getTouxiang());
        }else{
            view = inflater.inflate(R.layout.chatting_item_msg_text_right,null);
            RoundImageView iv_userhead = (RoundImageView)view.findViewById(R.id.iv_userhead);
            iv_userhead.setImageResource(R.drawable.header_cat);
        }
        //时间
        TextView tv_sendtime = (TextView)view.findViewById(R.id.tv_sendtime);
        //消息的内容
        TextView tv_chatcontent = (TextView)view.findViewById(R.id.tv_chatcontent);
        //这里可以先不用看,这个是发送图片的
        if(entity.getText().startsWith("file:///")){
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            options.inJustDecodeBounds = false;
            //options.inSampleSize = 2;
            options.inSampleSize = calculateInSampleSize(options, 480, 800);
            Bitmap bmp = BitmapFactory.decodeFile(entity.getText().substring(5), options);

            ImageSpan imageSpan = new ImageSpan(bmp);
            SpannableString spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            tv_chatcontent.append(spannableString);
        }else{
        //这个是发送文字和判断表情的
            SpannableString spannableString = FileUtils.getExpressionString(context, entity.getText());
            tv_chatcontent.setText(spannableString);
        }
        tv_sendtime.setText(entity.getDate());
        return view;
    }

    private int calculateInSampleSize(Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;  
        final int width = options.outWidth;  
        int inSampleSize = 1;  

        if (height > reqHeight || width > reqWidth) {  
            final int heightRatio = Math.round((float) height  
                    / (float) reqHeight);  
            final int widthRatio = Math.round((float) width / (float) reqWidth);  
            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;  
        }  

        return inSampleSize;  
    }
}

我的代码写的很乱,这个我会点,如果有人看不懂的话,这个可以问问我。嘿嘿。

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值