android IM即时通信之聊天界面UI框架

写在最前面

现在很多软件都要求加入即时通信的功能,当然很多都用了三方(环信、融信。。。)。最近,项目也有此需求,我们选择的是环信。环信也提供了UI框架,但是说实在的一般的应用用不了那么多功能,可能就简单的语音、表情、图片。我们就需要根据我们自己的需求去更改他的UI框架,花了很多时间去看他的UI框架源码。看完我真是要吐了,写的太复杂了,所以一气之下就自己写了界面。

效果展示

效果展示

简单介绍

  • 如何使用
EmotionInputDetector mDetector = EmotionInputDetector.with(this)
        //表情、更多页面布局Layout
        .setEmotionView(emotionLayout)
        //表情和更多功能是一个Frament,因此放在了ViewPager里面
        .setViewPager(viewpager)
        //聊天信息列表
        .bindToContent(chatList)
        //输入框
        .bindToEditText(editText)
        //表情按钮绑定
        .bindToEmotionButton(emotionButton)
        //add按钮绑定
        .bindToAddButton(emotionAdd) 
        //发送按钮绑定
        .bindToSendButton(emotionSend)
        //录音按钮绑定
        .bindToVoiceButton(emotionVoice)
        //显示“按下说话,放开发送”的textview绑定
        .bindToVoiceText(voiceText)
        .build();

之所以没有自定义view做,是为了将布局样式交给开发者自己定义,EmotionInputDetector只起到了一个管理工具类的作用。

Paste_Image.png
输入框下面这部分是一个不可左右滑动的viewpager,表情、相册等都是一个fragment。如果你想扩展其它功能时只需要在EmotionInputDetector的viewpager里面加一个fragment即可,其它逻辑操作全在你这个fragment里面。
* 表情功能简介
表情主要在ChatEmotionFragment里面,里面嵌套了一个viewpager,viewpager的每一页是一个GridView,每一页的GridView的最后一个item是一个删除按钮。具体可以看demo。
再说说表情的加载EmotionUtils,表情加载类,可自己添加多种表情,分别建立不同的map存放和不同的标志符即可

EMOTION_CLASSIC_MAP.put("[呵呵]", R.drawable.d_hehe);
EMOTION_CLASSIC_MAP.put("[嘻嘻]", R.drawable.d_xixi);
EMOTION_CLASSIC_MAP.put("[哈哈]", R.drawable.d_haha);
EMOTION_CLASSIC_MAP.put("[爱你]", R.drawable.d_aini);

这是放在本地的图片,对于在线表情。你只需要将你的imageurl和标识符一一对应即可。然后通过你的标识符取对应的imageurl。
* 语音功能简介
录音主要通过AudioRecoderUtils这个工具类,播放通过MediaManager。具体代码就不上了。
录音事件

mVoiceText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // 获得x轴坐标
        int x = (int) event.getX();
        // 获得y轴坐标
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mVoicePop.showAtLocation(v, Gravity.CENTER, 0, 0);
                mVoiceText.setText("松开结束");
                mPopVoiceText.setText("手指上滑,取消发送");
                mVoiceText.setTag("1");
                mAudioRecoderUtils.startRecord(mActivity);
                break;
            case MotionEvent.ACTION_MOVE:
                if (wantToCancle(x, y)) {
                    mVoiceText.setText("松开结束");
                    mPopVoiceText.setText("松开手指,取消发送");
                    mVoiceText.setTag("2");
                } else {
                    mVoiceText.setText("松开结束");
                    mPopVoiceText.setText("手指上滑,取消发送");
                    mVoiceText.setTag("1");
                }
                break;
            case MotionEvent.ACTION_UP:
                mVoicePop.dismiss();
                if (mVoiceText.getTag().equals("2")) {
                    //取消录音(删除录音文件)
                    mAudioRecoderUtils.cancelRecord();
                } else {
                    //结束录音(保存录音文件)
                    mAudioRecoderUtils.stopRecord();
                }
                mVoiceText.setText("按住说话");
                mVoiceText.setTag("3");
                mVoiceText.setVisibility(View.GONE);
                mEditText.setVisibility(View.VISIBLE);
                break;
        }
        return true;
    }});

录音话筒动画主要是通过mImageView.getDrawable().setLevel()进行一个上下波动的效果展示
* 聊天气泡自定义
聊天气泡没有用.9图片,因为.9图片背景,不能满足图片气泡功能。所以笔者采用的是自定义view:BubbleDrawable、BubbleImageView、BubbleLinearLayout。如此也可以满足用户自定义气泡的需求。本文没有做这个功能,有需求可以根据以上三个自定义view自己加。

三方开源框架

为尊重开源作者,再次将所用的三方开源框架一一罗列:
EasyRecyclerView——支持下拉上拉刷新等功能全面的RecyclerView
butterknife——View注解库和配套插件android-butterknife-zelezny
EventBus——事件总线
Glide——图片加载
BubbleView——聊天气泡自定义view
StateButton——一个可以用代码设置selector背景(按下去背景颜色更改,样式变化等等)的button, 再也不用写selector了

写到最后

如果觉得本文对你有帮助,感谢给个star
github地址:https://github.com/Rance935/ChatUI

本文作者:Rance935本文出处:android IM即时通信之聊天界面UI框架转载请在开头注明作者详细信息和本文出处
欢迎关注我的微信公众号和QQ群,分享Android 开发和互联网内容
Android技术分享:群号534813930
微信号:androidparks
公众号:AndroidParks

评论

  • Elite proxies回复

    Hi there! This is my first comment here so I just wanted to give
    a quick shout out and say I truly enjoy reading your articles.
    Can you suggest any other blogs/websites/forums that
    cover the same topics? Thank you so much!

  • Swinsbyv回复

    よって、評者が書いたコメントを読んでほしい。 Wedsホイル18インチ、タイヤはルマン4 215/40、エアロは純正。 [url=http://jan.hise.org/mizuno/jpx900_1/index.html]ミズノ ゴルフ ウッド[/url]
    西方陣地にG級やタイガーなど1万両 その他は東部にあった。 恐怖の逆高速道路です!(ンな事、あるのか!!)結局、打った分、すっかり返って来てくれました。
    [url=http://cattowngame.com/mb/718_1/index.html]ゴルフ レディース シューズ 人気[/url] 今、510ページまで来ているけど、ジョーダンさんが、一度目の引退をした直後くらいの話のところまで来ています。 ブラジルにお住まいでないかぎり失礼にはならんでしょう。
    [url=http://cattowngame.com/mb/718_1/index.html]718 MB[/url]
     5時に起きたものの、二度寝して7時まで目覚めませんでしたわ。 土牢を見なかった代わりに、腹切りやぐらに行くことにしました。 [url=http://cattowngame.com/mb/718_1/index.html]718 MB アイアン[/url]
    豊見城~南風原方面にかけては、軍主力の撤退を援護するための残地部隊のみで構成されてたので、敵の侵攻を妨げるほどの兵力はなく、これまでの戦いに比べれば敵はスムーズに南下できたようです。 公式には表明していないメリットは反米感情をかわすのだ。
    [url=http://jan.hise.org/mizuno/jpx900_1/index.html]JPX900 アイアン[/url] 成績上位25位までの選手が、決勝に進みますが、優勝もNET61.0で3人が並び、混戦でした。 小鳥味のソフトクリームのパワーじゃねぇダロ。
    [url=http://abqpoly.com/xxio9/iron_1/index.html]xxio フォージド アイアン 2017[/url]

  • Swinsstb回复

    海沿いの果てにある「松山」…それが「末の松山」だった。 中谷元防衛相も「予定通り堅実に工事を進めたい」と述べた。 [url=http://cattowngame.com/mb/718_1/index.html]タイトリスト 718 ap2 アイアン[/url]
    漢字表記なら変わりないが、旧かな表記はどうも「むずむず」する。 「ドライバーは顔のよさだ」感性が合うのか?最初に見たものを基準とするのか?最初に買ったカスタムドライバーマスダゴルフ V ROD 450 9.5度近くのゴルフ練習場で偶然買ってから400ydも飛ぶんだドラコン競技だと知りました。
    [url=http://futava.com/ping/g30_1/index.html]プリムス バーナー[/url] ちょっとなの?僕は「めちゃくちゃきつかったけど!」。  9月7日、普天間基地の辺野古移設をめぐる沖縄県と政府の第5回集中協議が首相官邸で行われた。
    [url=http://futava.com/ping/g30_1/index.html]ピン g30 評価[/url]
     今季、多くのGT3マシンが登場するスーパーGT。  米軍普天間飛行場の名護市辺野古移設の阻止を公約した翁長氏が知事に就任した2014年12月以降、官房長官が翁長氏と会談したのは初めて。 [url=http://futava.com/ping/g30_1/index.html]ピン g30 アイアン ロフト[/url]
     ツアーステージFW 14度ヘッドに バシレウスFw75シャフトを装着後の画像です。 流行に乗って肩まで沼温泉に浸かってしまいました(笑)それにしても・・・・・。
    [url=http://cattowngame.com/mb/718_1/index.html]タイトリスト 718 MB アイアン[/url] 歩行・走行の速度が測定値に影響を及ぼしていたとみられる。 1964年の例では、アイゼンハワー元大統領は、同年の候補指名を受けたバリー・ゴールドウォーター上院議員に対してあまり敬意を抱いていなかった。
    [url=http://jan.hise.org/mizuno/jpx900_1/index.html]JPX900 アイアン[/url]

  • VladilenGergo回复

    viagra pure europe

    [url=http://viagraomz.com/]buy generic viagra[/url]

    <a href="http://viagraomz.com/">cheap viagra online</a>

    get best results viagra.

  • Garage回复

    I simply couldn't leave your site prior to suggesting that I really loved the standard information an individual supply in your guests? Is going to be back frequently to check out new posts

  • Farmgirl Flowers回复

    I¡¦m not sure the place you're getting your information, but good topic. I needs to spend some time finding out much more or understanding more. Thanks for excellent information I was searching for this information for my mission.

  • Adult Education回复

    Hi there, I found your site by the use of Google whilst looking for a comparable subject, your site got here up, it seems great. I have bookmarked it in my google bookmarks.

  • cinema回复

    I simply could not go away your web site prior to suggesting that I actually enjoyed the usual info an individual provide in your visitors? Is going to be again steadily in order to check up on new posts

  • Busy At Home回复

    Hi, Neat post. There's a problem along with your web site in internet explorer, may check this¡K IE still is the market leader and a big component of people will leave out your fantastic writing because of this problem.

  • Schools回复

    I'm really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it’s rare to see a great blog like this one nowadays..

  • celebrities回复

    I definitely wanted to send a remark in order to appreciate you for all the awesome recommendations you are placing at this website. My incredibly long internet look up has at the end been paid with good quality details to talk about with my classmates and friends. I would declare that most of us readers are definitely blessed to be in a great community with many outstanding professionals with great solutions. I feel very much grateful to have seen your entire web page and look forward to tons of more awesome times reading here. Thank you once again for everything.

  • Living and Gardening回复

    hey there and thank you for your info – I’ve definitely picked up something new from right here. I did however expertise a few technical issues using this site, as I experienced to reload the site a lot of times previous to I could get it to load correctly. I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and could damage your quality score if advertising and marketing with Adwords. Well I am adding this RSS to my e-mail and could look out for a lot more of your respective interesting content. Ensure that you update this again very soon..

  • School Technology回复

    Helpful information. Lucky me I found your site unintentionally, and I am stunned why this twist of fate did not happened in advance! I bookmarked it.

  • Better Homes回复

    I am continuously invstigating online for articles that can aid me. Thank you!

  • Cheap dvd boxset回复

    This blog was... how do I say it? Relevant!! Finally I've found something which helped me.
    Cheers!

  • Public School回复

    Hello there, just became alert to your blog through Google, and found that it is truly informative. I am going to watch out for brussels. I will appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  • Carpeting回复

    Thank you for sharing excellent informations. Your website is so cool. I'm impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles. You, my friend, ROCK! I found just the info I already searched all over the place and just couldn't come across. What a perfect web site.

  • music回复

    Normally I do not read article on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, very nice article.

  • ליגתהעל回复

    Hi there it's me, I am also visiting this site regularly, this web
    page is actually fastidious and the visitors are genuinely sharing pleasant
    thoughts.

  • Continuing Education回复

    Of course, what a fantastic site and illuminating posts, I definitely will bookmark your website.Best Regards!

  • A Way to Garden回复

    I want to show some appreciation to the writer for bailing me out of such a dilemma. Because of browsing through the the web and getting tricks that were not helpful, I figured my entire life was over. Being alive without the approaches to the problems you've fixed by means of your entire website is a critical case, as well as the kind which may have badly affected my career if I had not noticed your blog. That expertise and kindness in controlling a lot of things was valuable. I don't know what I would've done if I had not come upon such a stuff like this. I can also at this time relish my future. Thank you so much for your expert and sensible guide. I won't hesitate to recommend your web page to anyone who would need counselling about this subject matter.

  • painting ideas回复

    This is really interesting, You're a very skilled blogger. I've joined your feed and look forward to seeking more of your great post. Also, I have shared your web site in my social networks!

  • Kitchen Design Ideas回复

    It is actually a nice and helpful piece of information. I¡¦m glad that you just shared this helpful information with us. Please keep us informed like this. Thanks for sharing.

  • Living and Gardening回复

    Useful info. Fortunate me I discovered your site accidentally, and I am stunned why this twist of fate did not took place in advance! I bookmarked it.

  • movie回复

    Thank you, I have recently been looking for information approximately this subject for ages and yours is the greatest I have came upon so far. But, what in regards to the bottom line? Are you positive concerning the supply?

  • Garden Decor回复

    I've been surfing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the net will be a lot more useful than ever before.

  • Growing With Plants回复

    Great ¡V I should certainly pronounce, impressed with your site. I had no trouble navigating through all the tabs as well as related information ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Quite unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your customer to communicate. Nice task..

  • pissstrahl回复

    I need to to thank you for this good read!! I absolutely loved every bit of
    it. I have got you book marked to look at new stuff you post…

  • music回复

    Valuable information. Lucky me I found your site by accident, and I am surprised why this accident did not took place in advance! I bookmarked it.

  • Home Flooring回复

    Well I definitely enjoyed studying it. This post provided by you is very helpful for accurate planning.

  • School Technology回复

    I have been exploring for a bit for any high-quality articles or blog posts on this sort of house . Exploring in Yahoo I ultimately stumbled upon this site. Studying this info So i¡¦m glad to express that I've an incredibly excellent uncanny feeling I came upon exactly what I needed. I most certainly will make certain to don¡¦t overlook this website and provides it a look on a relentless basis.

  • The Handyman回复

    Definitely, what a splendid site and educative posts, I will bookmark your blog.All the Best!