聊天z

1 篇文章 0 订阅
1 篇文章 0 订阅

           看见下面的导航栏

            我们先实现这个:

            底部我们就用了一个自定义的View

             

BottomControlView 这个类就是我们的底部导航栏
public class BottomControlView extends RelativeLayout {

    private ImageView optionView;
    private ImageView giftView;

    public BottomControlView(Context context) {
        super(context);
        init();
    }

    public BottomControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BottomControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_bottom_control, this, true);
        findAllViews();
    }

    public void setIsHost(boolean isHost) {
        if (isHost) {
            giftView.setVisibility(INVISIBLE);
            optionView.setVisibility(VISIBLE);
        } else {
            optionView.setVisibility(INVISIBLE);
            giftView.setVisibility(VISIBLE);
        }
    }

    private void findAllViews() {
        findViewById(R.id.chat).setOnClickListener(clickListener);
        findViewById(R.id.close).setOnClickListener(clickListener);
        giftView = (ImageView) findViewById(R.id.gift);
        giftView.setOnClickListener(clickListener);
        optionView = (ImageView) findViewById(R.id.option);
        optionView.setOnClickListener(clickListener);
    }

    private OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.chat) {
                // 显示聊天操作栏
                if (mOnControlListener != null) {
                    mOnControlListener.onChatClick();
                }
            } else if (view.getId() == R.id.close) {
                // 关闭直播
                if (mOnControlListener != null) {
                    mOnControlListener.onCloseClick();
                }
            } else if (view.getId() == R.id.gift) {
                // 显示礼物选择九宫格
                if (mOnControlListener != null) {
                    mOnControlListener.onGiftClick();
                }
            } else if (view.getId() == R.id.option) {
                if (mOnControlListener != null) {
                    mOnControlListener.onOptionClick(view);
                }
            }

        }
    };

    private OnControlListener mOnControlListener;

    public void setOnControlListener(OnControlListener l) {
        mOnControlListener = l;
    }

    public interface OnControlListener {
        //显示聊天操作栏
        public void onChatClick();
        // 关闭直播
        public void onCloseClick();
        // 显示礼物选择九宫格
        public void onGiftClick();

        public void onOptionClick(View view);
    }

}

    还有一个是我们的消息导航栏

public class ChatView extends LinearLayout {

    private CheckBox mSwitchChatType;
    private EditText mChatContent;
    private TextView mSend;

    public ChatView(Context context) {
        super(context);
        init();
    }

    public ChatView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ChatView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        int paddingPx = (int) (getResources().getDisplayMetrics().density * 10 + 0.5f);
        setPadding(paddingPx, paddingPx, paddingPx, paddingPx);
        setBackgroundColor(Color.parseColor("#ccffffff"));
        LayoutInflater.from(getContext()).inflate(R.layout.view_chat, this, true);

        findAllViews();
    }

    private void findAllViews() {
        mSwitchChatType = (CheckBox) findViewById(R.id.switch_chat_type);
        mChatContent = (EditText) findViewById(R.id.chat_content_edit);
        mSend = (TextView) findViewById(R.id.chat_send);

        mSwitchChatType.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    mChatContent.setHint("发送弹幕聊天消息");
                } else {
                    mChatContent.setHint("和大家聊点什么吧");

                }
            }
        });
        mSwitchChatType.setChecked(false);
        mChatContent.setHint("和大家聊点什么吧");
        mSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO 发送聊天消息。
                Toast.makeText(getContext(), mChatContent.getText().toString(), Toast.LENGTH_SHORT).show();
                //sendChatMsg();
            }
        });
    }

    这个就是我们的消息状态栏

        当我们进行点击消息的时候隐藏BottomControlVIew,显示我们的ChatView,里面都有各种事件的监听,我这里就不详细说了,这些都是最基础的

        这里还涉及到一个就是当我们退出键盘的时候我们得回到BottomCOntrolView的界面,但是我们怎么监听键盘的关闭呢,

        这里有一个小窍门,就是我们可以监听这个界面显示了多少,通过他的界面宽度和高度的大小比较来判断键盘是否关闭

    所以我们重写了Reletivelayout来实现的,请看代码

    

public class SizeChangeRelativeLayout extends RelativeLayout {

    public SizeChangeRelativeLayout(Context context) {
        super(context);
    }

    public SizeChangeRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public SizeChangeRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mOnSizeChangeListener == null) {
            return;
        }
        if (h > oldh) {
            //画面变长,键盘隐藏
            mOnSizeChangeListener.onLarge();
        } else {
            //画面变短,键盘显示
            mOnSizeChangeListener.onSmall();
        }
    }

    private OnSizeChangeListener mOnSizeChangeListener;

    public void setOnSizeChangeListener(OnSizeChangeListener l) {
        mOnSizeChangeListener = l;
    }

    public interface OnSizeChangeListener {
        public void onLarge();

        public void onSmall();
    }
}

    很简单的几句代码,里面有两个接口,键盘隐藏,出现的接口。

    通过View的显示和隐藏来进行处理,但是这样是不会实现效果的,童鞋们呢可以自己进行试验一下,这样做并不能够检测到窗口的大小变换,

    因为我们还没有在清单文件里面给我们的activity添加监听的处理在我们的activity里面添加这么一行代码

<activity android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize">

这样我们才能监听到页面改变

    好了这些基本的界面我们已经实现了,接下来开始我们的核心,也就是我们的通信流程了

    1.消息列表展示界面

        消息列表我们是用了一个listview来展示的,

    2.发送消息

          普通文本消息

            

ILVText iliveText = new ILVText("ss", "", ILVLiveConstants.GROUP_TYPE);
            iliveText.setText("" + textInput.getText());
            //发送消息
            ILVLiveManager.getInstance().sendText(iliveText, new ILiveCallBack() {
                @Override
                public void onSuccess(Object data) {
                    Toast.makeText(LiveActivity.this, "send succ!", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(String module, int errCode, String errMsg) {

                }

            });

        以上就是发送一个我们最基本的消息了,

发送完消息后我们需要对消息类型进行一个处理这里我们先不做讨论,之后的弹幕消息再进行处理

    那么我们发送完消息后在哪里接受呢?

       3.解析消息

        LVLiveConfig 是全局直播设置,里面可以配置一个消息回调ILVLiveConfig.ILVLiveMsgListener。通过这个消息回调可以拿到对应的消息类型,然后塞回ILVLiveManager进行配置。

        

参数类型说明
onNewCmdMsgiLiveSDK预定义信令回调 例如上麦 下麦
onNewCustomMsg用户自定义信令回调
onNewTextMsg普通文本接收回调

我们是通过这个接口来实现



//对消息的接受
        ILVLiveConfig liveConfig = BearApplication.getApplication().getLiveConfig();
        liveConfig.setLiveMsgListener(new ILVLiveConfig.ILVLiveMsgListener() {
            @Override
            public void onNewTextMsg(ILVText text, String SenderId, TIMUserProfile userProfile) {
                //接收到文本消息
            }

            @Override
            public void onNewCustomMsg(ILVCustomCmd cmd, String id, TIMUserProfile userProfile) {
                //接收到自定义消息
                if (cmd.getCmd() == Constants.CMD_CHAT_MSG_LIST) {
                    String content = cmd.getParam();
                    ChatMsgInfo info = ChatMsgInfo.createListInfo(content, id, userProfile.getFaceUrl());
                    mChatListView.addMsgInfo(info);
                } else if (cmd.getCmd() == Constants.CMD_CHAT_MSG_DANMU) {
                    String content = cmd.getParam();
                    ChatMsgInfo info = ChatMsgInfo.createListInfo(content, id, userProfile.getFaceUrl());
                    mChatListView.addMsgInfo(info);

                    String name = userProfile.getNickName();
                    if (TextUtils.isEmpty(name)) {
                        name = userProfile.getIdentifier();
                    }
                    ChatMsgInfo danmuInfo = ChatMsgInfo.createDanmuInfo(content, id, userProfile.getFaceUrl(), name);
                    mDanmuView.addMsgInfo(danmuInfo);
                } else if (cmd.getCmd() == Constants.CMD_CHAT_GIFT) {
                    //界面显示礼物动画。
                    GiftCmdInfo giftCmdInfo = new Gson().fromJson(cmd.getParam(), GiftCmdInfo.class);
                    int giftId = giftCmdInfo.giftId;
                    String repeatId = giftCmdInfo.repeatId;
                    GiftInfo giftInfo = GiftInfo.getGiftById(giftId);
                    if (giftInfo == null) {
                        return;
                    }
                    if (giftInfo.type == GiftInfo.Type.ContinueGift) {
                        giftRepeatView.showGift(giftInfo, repeatId, userProfile);
                    } else if (giftInfo.type == GiftInfo.Type.FullScreenGift) {
                        //全屏礼物
                        giftFullView.showGift(giftInfo, userProfile);
                    }
                } else if (cmd.getCmd() == ILVLiveConstants.ILVLIVE_CMD_ENTER) {
                    //用户进入直播
                    mTitleView.addWatcher(userProfile);
                    mVipEnterView.showVipEnter(userProfile);
                } else if (cmd.getCmd() == ILVLiveConstants.ILVLIVE_CMD_LEAVE) {
                    //用户离开消息
                    mTitleView.removeWatcher(userProfile);
                }

            }

            @Override
            public void onNewOtherMsg(TIMMessage message) {
                //接收到其他消息
            }
        });
上面就是我们的信息处理

        那么我们就来处理怎么发送自定义的消息

            

 //发送消息        
                customCmd.setDestId(ILiveRoomManager.getInstance().getIMGroupId());

                ILVLiveManager.getInstance().sendCustomCmd(customCmd, new ILiveCallBack<TIMMessage>() {
                    @Override
                    public void onSuccess(TIMMessage data) {
                        if (customCmd.getCmd() == Constants.CMD_CHAT_MSG_LIST) {
                            //如果是列表类型的消息,发送给列表显示
                            String chatContent = customCmd.getParam();
                            String userId = BearApplication.getApplication().getSelfProfile().getIdentifier();
                            String avatar = BearApplication.getApplication().getSelfProfile().getFaceUrl();
                            ChatMsgInfo info = ChatMsgInfo.createListInfo(chatContent, userId, avatar);
                            mChatListView.addMsgInfo(info);
                        } else if (customCmd.getCmd() == Constants.CMD_CHAT_MSG_DANMU) {
                            String chatContent = customCmd.getParam();
                            String userId = BearApplication.getApplication().getSelfProfile().getIdentifier();
                            String avatar = BearApplication.getApplication().getSelfProfile().getFaceUrl();
                            ChatMsgInfo info = ChatMsgInfo.createListInfo(chatContent, userId, avatar);
                            mChatListView.addMsgInfo(info);

                            String name = BearApplication.getApplication().getSelfProfile().getNickName();
                            if (TextUtils.isEmpty(name)) {
                                name = userId;
                            }
                            ChatMsgInfo danmuInfo = ChatMsgInfo.createDanmuInfo(chatContent, userId, avatar, name);
                            mDanmuView.addMsgInfo(danmuInfo);
                        }
                    }

                    @Override
                    public void onError(String module, int errCode, String errMsg) {
                    }

                });
ILVCustomCmd 和以前的发送消息不同,我们这个发送消息的是用的ILVCustomCmd来实现的,并且又sendText()改变为sendCustomCmd()来进行发送

    发送的时候有个cmd来进行我们发送的一个标记,用来辨别我们的消息到底是哪个消息,弹幕还是列表消息,

    都是通过ILVCustomCmd来实现的,这个我们需要设置他的type;是群组消息,还是一对一的消息,

    

            ILVCustomCmd customCmd = new ILVCustomCmd();
            customCmd.setType(ILVText.ILVTextType.eGroupMsg);
            boolean isDanmu = mSwitchChatType.isChecked();
            if (isDanmu) {
                customCmd.setCmd(Constants.CMD_CHAT_MSG_DANMU);
            } else {
                customCmd.setCmd(Constants.CMD_CHAT_MSG_LIST);
            }
            customCmd.setParam(mChatContent.getText().toString());
            mOnChatSendListener.onChatSend(customCmd);//设置消息内容
            customCmd.setDestId(ILiveRoomManager.getInstance().getIMGroupId());

这段代码就是我们发送的消息承载体

    发送成功后我们就对信息进行处理,弹幕显示或者列表显示

    那么我们的cmd是在哪里生成的呢?

        请看这个类

/**
 * 直播通话配置
 */
public class ILVLiveConstants {

    public static final int ILVLIVE_CMD_NONE = 0x700;         //无效消息

    public static final int ILVLIVE_CMD_ENTER = ILVLIVE_CMD_NONE + 1;                                 //用户加入直播, Group消息
    public static final int ILVLIVE_CMD_LEAVE = ILVLIVE_CMD_ENTER + 1;                           //用户退出直播, Group消息
    public static final int ILVLIVE_CMD_INVITE = ILVLIVE_CMD_LEAVE + 1;                           //邀请上麦,C2C消息
    public static final int ILVLIVE_CMD_INVITE_CANCEL = ILVLIVE_CMD_INVITE + 1;                       //取消邀请上麦,C2C消息
    public static final int ILVLIVE_CMD_INVITE_CLOSE = ILVLIVE_CMD_INVITE_CANCEL + 1;                    //关闭上麦,C2C消息
    public static final int ILVLIVE_CMD_INTERACT_AGREE = ILVLIVE_CMD_INVITE_CLOSE + 1;                  //同意上麦,C2C消息
    public static final int ILVLIVE_CMD_INTERACT_REJECT = ILVLIVE_CMD_INTERACT_AGREE + 1;                       //拒绝上麦,C2C消息

    /**
     * 用户自定义消息段
     */
    public static final int ILVLIVE_CMD_CUSTOM_LOW_LIMIT = 0x800;          //自定义消息段下限
    public static final int ILVLIVE_CMD_CUSTOM_UP_LIMIT = 0x900;          //自定义消息段上线

    /** 信令专用标识 */
    //public static final String TCEXT_MAGIC          = "LiveNotification";

    /**
     * 命令字
     */
    public static final String CMD_KEY = "userAction";
    /**
     * 命令参数
     */
    public static final String CMD_PARAM = "actionParam";




}

这个类里面就有我们的所有cmd,我们也可以进行自定义只不过要在人家给的范围内进行使用(16进制的100)

    

public class Constants {

    //自定义发送列表聊天
    public static final int CMD_CHAT_MSG_LIST = ILVLiveConstants.ILVLIVE_CMD_CUSTOM_LOW_LIMIT + 1;
    //自定义发送弹幕聊天
    public static final int CMD_CHAT_MSG_DANMU = ILVLiveConstants.ILVLIVE_CMD_CUSTOM_LOW_LIMIT + 2;

    //自定义发送礼物
    public static final int CMD_CHAT_GIFT = ILVLiveConstants.ILVLIVE_CMD_CUSTOM_LOW_LIMIT + 3;


}

    这个是我们自定义的cmd

    这样我们就实现了自定义消息的功能

    自定义完我们的消息后我们就要进行弹幕消息的播放:

            1>. 这里我们选用了一个github上面的一个开源库

compile 'com.anbetter:danmukulight:1.0.1'

            2.我们自己封装了一个弹幕播放类

            

/**
 * 弹幕库使用帮助类
 *
 * 建议凡是弹幕中涉及到的图片,大小控制在50kb以内,尺寸控制在100x100以内(单位像素)
 *
 * Created by android_ls on 2016/12/18.
 */
public final class DanMuHelper {

    private ArrayList<WeakReference<IDanMuParent>> mDanMuViewParents;
    private Context mContext;

    public DanMuHelper(Context context) {
        this.mContext = context.getApplicationContext();
        this.mDanMuViewParents = new ArrayList<>();
    }

    public void release() {
        if (mDanMuViewParents != null) {
            for (WeakReference<IDanMuParent> danMuViewParentsRef : mDanMuViewParents) {
                if (danMuViewParentsRef != null) {
                    IDanMuParent danMuParent = danMuViewParentsRef.get();
                    if (danMuParent != null)
                        danMuParent.release();
                }
            }
            mDanMuViewParents.clear();
            mDanMuViewParents = null;
        }

        mContext = null;
    }

    public void add(final IDanMuParent danMuViewParent) {
        if (danMuViewParent != null) {
            danMuViewParent.clear();
        }

        if (mDanMuViewParents != null) {
            mDanMuViewParents.add(new WeakReference<>(danMuViewParent));
        }
    }
    //添加一个广播 true是全局的广播
    public void addDanMu(DanmakuEntity danmakuEntity, boolean broadcast) {
        if (mDanMuViewParents != null) {
            //从集合中取出一个
            WeakReference<IDanMuParent> danMuViewParent = mDanMuViewParents.get(0);
            //如果是全局广播
            if (!broadcast) {
                danMuViewParent = mDanMuViewParents.get(1);
            }
            //获取一个DanmUModel
            DanMuModel danMuView = createDanMuView(danmakuEntity);
            if (danMuViewParent != null && danMuView != null && danMuViewParent.get() != null) {
                danMuViewParent.get().add(danMuView);
            }
        }
    }

    private DanMuModel createDanMuView(final DanmakuEntity entity) {
        final DanMuModel danMuView = new DanMuModel();
        //动画从右到左
        danMuView.setDisplayType(DanMuModel.RIGHT_TO_LEFT);
        //弹幕的 等级
        danMuView.setPriority(DanMuModel.NORMAL);
        //设置左编剧
        danMuView.marginLeft = DimensionUtil.dpToPx(mContext, 30);
        //public static final int DANMAKU_TYPE_SYSTEM = 0;// 系统弹幕消息
       // public static final int DANMAKU_TYPE_USERCHAT = 1;// 用户聊天弹幕消息
        if (entity.getType() == DanmakuEntity.DANMAKU_TYPE_USERCHAT) {
            // 用户的头像大小
            int avatarSize = DimensionUtil.dpToPx(mContext, 30);
            danMuView.avatarWidth = avatarSize;
            danMuView.avatarHeight = avatarSize;

            String avatarImageUrl = entity.getAvatar();
            //加载头像
            Phoenix.with(mContext)
                    .setUrl(avatarImageUrl)
                    .setWidth(avatarSize)
                    .setHeight(avatarSize)
                    .setResult(new IResult<Bitmap>() {
                        @Override
                        public void onResult(Bitmap bitmap) {
                            danMuView.avatar = CircleBitmapTransform.transform(bitmap);
                        }
                    })
                    .load();

            // 等级
            int level = entity.getLevel();
            int levelResId = getLevelResId(level);
            Drawable drawable = ContextCompat.getDrawable(mContext, levelResId);
            danMuView.levelBitmap = drawable2Bitmap(drawable);
            danMuView.levelBitmapWidth = DimensionUtil.dpToPx(mContext, 33);
            danMuView.levelBitmapHeight = DimensionUtil.dpToPx(mContext, 16);
            danMuView.levelMarginLeft = DimensionUtil.dpToPx(mContext, 5);

            if (level > 0 && level < 100) {
                danMuView.levelText = String.valueOf(level);
                danMuView.levelTextColor = ContextCompat.getColor(mContext, R.color.white);
                danMuView.levelTextSize = DimensionUtil.spToPx(mContext, 14);
            }

            // 显示的文本内容
            String name = entity.getName() + ":";
            String content = entity.getText();
            SpannableString spannableString = new SpannableString(name + content);
            spannableString.setSpan(
                    new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.white)),
                    0,
                    name.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            MLog.i("spannableString = " + spannableString);

            danMuView.textSize = DimensionUtil.spToPx(mContext, 14);
            danMuView.textColor = ContextCompat.getColor(mContext, R.color.light_green);
            danMuView.textMarginLeft = DimensionUtil.dpToPx(mContext, 5);
            danMuView.text = spannableString;

            // 弹幕文本背景
            danMuView.textBackground = ContextCompat.getDrawable(mContext, R.drawable.corners_danmu);
            danMuView.textBackgroundMarginLeft = DimensionUtil.dpToPx(mContext, 15);
            danMuView.textBackgroundPaddingTop = DimensionUtil.dpToPx(mContext, 3);
            danMuView.textBackgroundPaddingBottom = DimensionUtil.dpToPx(mContext, 3);
            danMuView.textBackgroundPaddingRight = DimensionUtil.dpToPx(mContext, 15);

            danMuView.enableTouch(true);
            danMuView.setOnTouchCallBackListener(new OnDanMuTouchCallBackListener() {

                @Override
                public void callBack(DanMuModel danMuView) {
                    Toast.makeText(mContext, "点击了", Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            // 显示的文本内容
            danMuView.textSize = DimensionUtil.spToPx(mContext, 14);
            danMuView.textColor = ContextCompat.getColor(mContext, R.color.light_green);
            danMuView.textMarginLeft = DimensionUtil.dpToPx(mContext, 5);

            if (entity.getRichText() != null) {
                danMuView.text = RichTextParse.parse(mContext, entity.getRichText(), DimensionUtil.spToPx(mContext, 18), false);
            } else {
                danMuView.text = entity.getText();
            }

            // 弹幕文本背景
            danMuView.textBackground = ContextCompat.getDrawable(mContext, R.drawable.corners_danmu);
            danMuView.textBackgroundMarginLeft = DimensionUtil.dpToPx(mContext, 15);
            danMuView.textBackgroundPaddingTop = DimensionUtil.dpToPx(mContext, 3);
            danMuView.textBackgroundPaddingBottom = DimensionUtil.dpToPx(mContext, 3);
            danMuView.textBackgroundPaddingRight = DimensionUtil.dpToPx(mContext, 15);

            danMuView.enableTouch(false);
        }

        return danMuView;
    }

    /**
     * Drawable转换成Bitmap
     *
     * @param drawable
     * @return
     */
    public Bitmap drawable2Bitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            // 转换成Bitmap
            return ((BitmapDrawable) drawable).getBitmap();
        } else if (drawable instanceof NinePatchDrawable) {
            // .9图片转换成Bitmap
            Bitmap bitmap = Bitmap.createBitmap(
                    drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(),
                    drawable.getOpacity() != PixelFormat.OPAQUE ?
                            Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            drawable.draw(canvas);
            return bitmap;
        } else {
            return null;
        }
    }

    /**
     * 设置等级
     *
     * @param level level=100表示主播
     */
    public int getLevelResId(int level) {
        int resId = R.drawable.icon_level_stage_zero;
        switch (level) {
            case 100:
//                resId = R.mipmap.lv_1000;
                break;
            case 0:
                resId = R.drawable.icon_level_stage_zero;
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                resId = R.drawable.icon_level_stage_one;
                break;
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                resId = R.drawable.icon_level_stage_two;
                break;
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
                resId = R.drawable.icon_level_stage_three;
                break;
            case 16:
            case 17:
            case 18:
            case 19:
            case 20:
                resId = R.drawable.icon_level_stage_four;
                break;
            case 21:
            case 22:
            case 23:
            case 24:
            case 25:
                resId = R.drawable.icon_level_stage_five;
                break;
            case 26:
            case 27:
            case 28:
            case 29:
            case 30:
            default:
                resId = R.drawable.icon_level_stage_six;
                break;
        }

        return resId;
    }

        当我们需要添加一个房间内弹幕时候只需
//发送一条房间内弹幕消息
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DanmakuEntity danmakuEntity = new DanmakuEntity();
                danmakuEntity.setType(DanmakuEntity.DANMAKU_TYPE_USERCHAT);
                danmakuEntity.setName("小A");
                danmakuEntity.setAvatar("http://q.qlogo.cn/qqapp/100229475/E573B01150734A02F25D8E9C76AFD138/100");
                danmakuEntity.setLevel(23);
                danmakuEntity.setText("滚滚长江东逝水,浪花淘尽英雄~~");

                addRoomDanmaku(danmakuEntity);
            }
        });

        addDanmu()就是我们封装好的进行弹幕发送的api

        

 /**
     * 发送一条房间内的弹幕
     */
    private void addRoomDanmaku(DanmakuEntity danmakuEntity) {
        if (mDanMuHelper != null) {
            mDanMuHelper.addDanMu(danmakuEntity, false);
        }
    }

        当我们需要发送一个全局的弹幕时:

findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String jsonStr = "{\"type\":306,\"name\":\"\",\"text\":\"恭喜小A在小马过河的房间12200031赠送幸运礼物-300棒棒糖,中奖500倍,获得5000钻石。\",\"richText\":[{\"type\":\"text\",\"content\":\"恭喜\",\"color\":\"89F9DF\"},{\"type\":\"text\",\"content\":\"小A\"},{\"type\":\"text\",\"content\":\"在\",\"color\":\"89F9DF\"},{\"type\":\"text\",\"content\":\"小马过河\"},{\"type\":\"text\",\"content\":\"的房间\",\"color\":\"89F9DF\"},{\"type\":\"text\",\"content\":12200031},{\"type\":\"text\",\"content\":\"赠送\",\"color\":\"89F9DF\"},{\"type\":\"icon_gift\",\"extend\":\"text\",\"gift_id\":3816,\"content\":\"300棒棒糖\"},{\"type\":\"text\",\"content\":\",中奖\",\"color\":\"89F9DF\"},{\"type\":\"text\",\"content\":\"500倍\",\"color\":\"FFED0A\"},{\"type\":\"text\",\"content\":\",获得\",\"color\":\"89F9DF\"},{\"type\":\"text\",\"content\":\"5000钻石。\",\"color\":\"FFED0A\"}],\"live_id\":\"1220003114804106040\"}";

                Gson json = new Gson();
                DanmakuEntity danmakuEntity = json.fromJson(jsonStr, DanmakuEntity.class);
                danmakuEntity.setType(DanmakuEntity.DANMAKU_TYPE_SYSTEM);

                addDanmaku(danmakuEntity);
            }
        });

    addDanmuku()

/**
     * 发送一条全站弹幕
     */
    private void addDanmaku(DanmakuEntity danmakuEntity) {
        if (mDanMuHelper != null) {
            mDanMuHelper.addDanMu(danmakuEntity, true);
        }
    }

    发送当弹幕时全局的时候就是true,否则为false

这样我们就可以进行弹幕的发送,虽然看起来代码有点多,但是逻辑很简单

    当我们需要隐藏所有的弹幕时我们可以调用

DanMuView的hideALLDanMuView(boolean falg)进行隐藏

    

    

到此我们的聊天就暂时告一段落了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值