Android 解析微信基础表情并显示

原创 2016年08月31日 11:22:12

先贴一个链接http://blog.csdn.net/u011002668/article/details/50896586

自己根据需求去更改.

先不看代码,先说一下原理,把原理弄明白了,再看代码就很清晰。

在工程中,使用的就是系统的TextView,所以可以看出来显示表情并不是我自定义的功能,而是本身就支持的。只不过通常TextView在显示文本时,我们输入的都是普通的String或者Charsequence,要显示表情,就不能输入普通的String了,而是输入一个特殊的东西,叫SpannableString。
SpannableString,从名字也能看出来,它就是String字符串,只不过它在显示的时候,会把字符串中的一部分替换成一张图片,而当你调用editText.getText()方法时,返回的还是String字符串。所以它就是一段字符串和一张图片的对应。在显示的时候是图片,getText()取出时是String。

这个原理解释清楚后,示例图所展示的功能就不难了。 

 

(1).由于需求的原因,此文只解析微信表情并显示上去,不发送表情

(2).微信表情获取的地址,微信安装路径D:\Program Files (x86)\91 Wireless\91Mobile V5\Dlls\WeChat\Resource\html\expression

(3).表情对应的String,最后面5个微信没有,无法测试

    public static final String ee_1 = "/::)";
    public static final String ee_2 = "/::~";
    public static final String ee_3 = "/::B";
    public static final String ee_4 = "/::|";
    public static final String ee_5 = "/:8-)";
    public static final String ee_6 = "/::<";
    public static final String ee_7 = "/::$";
    public static final String ee_8 = "/::X";
    public static final String ee_9 = "/::Z";
    public static final String ee_10 = "/::'(";
    public static final String ee_11 = "/::-|";
    public static final String ee_12 = "/::@";
    public static final String ee_13 = "/::P";
    public static final String ee_14 = "/::D";
    public static final String ee_15 = "/::O";
    public static final String ee_16 = "/::(";
    public static final String ee_17 = "/::+";
    public static final String ee_18 = "/:--b";
    public static final String ee_19 = "/::Q";
    public static final String ee_20 = "/::T";
    public static final String ee_21 = "/:,@P";
    public static final String ee_22 = "/:,@-D";
    public static final String ee_23 = "/::d";
    public static final String ee_24 = "/:,@o";
    public static final String ee_25 = "/::g";
    public static final String ee_26 = "/:|-)";
    public static final String ee_27 = "/::!";
    public static final String ee_28 = "/::L";
    public static final String ee_29 = "/::>";
    public static final String ee_30 = "/::,@";
    public static final String ee_31 = "/:,@f";
    public static final String ee_32 = "/::-S";
    public static final String ee_33 = "/:?";
    public static final String ee_34 = "/:,@x";
    public static final String ee_35 = "/:,@@";
    public static final String ee_36 = "/::8";
    public static final String ee_37 = "/:,@!";
    public static final String ee_38 = "/:!!!";
    public static final String ee_39 = "/:xx";
    public static final String ee_40 = "/:bye";
    public static final String ee_41 = "/:wipe";
    public static final String ee_42 = "/:dig";
    public static final String ee_43 = "/:handclap";
    public static final String ee_44 = "/:&-(";
    public static final String ee_45 = "/:B-)";
    public static final String ee_46 = "/:<@";
    public static final String ee_47 = "/:@>";
    public static final String ee_48 = "/::-O";
    public static final String ee_49 = "/:>-|";
    public static final String ee_50 = "/:P-(";
    public static final String ee_51 = "/::'|";
    public static final String ee_52 = "/:X-)";
    public static final String ee_53 = "/::*";
    public static final String ee_54 = "/:@x";
    public static final String ee_55 = "/:8*";
    public static final String ee_56 = "/:pd";
    public static final String ee_57 = "/:<W>";
    public static final String ee_58 = "/:beer";
    public static final String ee_59 = "/:basketb";
    public static final String ee_60 = "/:oo";
    public static final String ee_61 = "/:coffee";
    public static final String ee_62 = "/:eat";
    public static final String ee_63 = "/:pig";
    public static final String ee_64 = "/:rose";
    public static final String ee_65 = "/:fade";
    public static final String ee_66 = "/:showlove";
    public static final String ee_67 = "/:heart";
    public static final String ee_68 = "/:break";
    public static final String ee_69 = "/:cake";
    public static final String ee_70 = "/:li";
    public static final String ee_71 = "/:bome";
    public static final String ee_72 = "/:kn";
    public static final String ee_73 = "/:footb";
    public static final String ee_74 = "/:ladybug";
    public static final String ee_75 = "/:shit";
    public static final String ee_76 = "/:moon";
    public static final String ee_77 = "/:sun";
    public static final String ee_78 = "/:gift";
    public static final String ee_79 = "/:hug";
    public static final String ee_80 = "/:strong";
    public static final String ee_81 = "/:weak";
    public static final String ee_82 = "/:share";
    public static final String ee_83 = "/:v";
    public static final String ee_84 = "/:@)";
    public static final String ee_85 = "/:jj";
    public static final String ee_86 = "/:@@";
    public static final String ee_87 = "/:bad";
    public static final String ee_88 = "/:lvu";
    public static final String ee_89 = "/:no";
    public static final String ee_90 = "/:ok";
    public static final String ee_91 = "/:love";
    public static final String ee_92 = "/:<L>";
    public static final String ee_93 = "/:jump";
    public static final String ee_94 = "/:shake";
    public static final String ee_95 = "/:<O>";
    public static final String ee_96 = "/:circle";
    public static final String ee_97 = "/:kotow";
    public static final String ee_98 = "/:turn";
    public static final String ee_99 = "/:skip";
    public static final String ee_100 = "/:oY";
    public static final String ee_101 = "/:#-0";
    public static final String ee_102 = "[街舞]";
    public static final String ee_103 = "/:kiss";
    public static final String ee_104 = "/:<&";
    public static final String ee_105 = "/:&>";

 

(4)解析的代码

   

public static SpannableString getExpressionString(Context context,
            String str, int textSize) {
        Log.d(TAG, str.length() + "str");
        SpannableString spannableString = new SpannableString(str);

        for (int i = 0; i < EaseDefaultEmojiconDatas.emojis.length; i++) {
            String PATTEN_STR = EaseDefaultEmojiconDatas.emojis[i];

            int indexOf = str.indexOf(PATTEN_STR);

            if (indexOf != -1) {
                try {
                    dealExpression(context, str, spannableString, textSize,
                            PATTEN_STR, EaseDefaultEmojiconDatas.icons[i],
                            indexOf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return spannableString;
    }

    String substring = "";

    public static void dealExpression(Context context, String str,
            SpannableString spannableString, int textSize, String key,
            int icons, int start) throws Exception {
        Logger.d(TAG, spannableString + " == spannableString");

        Drawable d = context.getResources().getDrawable(icons);
        d.setBounds(0, 0, textSize, textSize);
        ImageSpan imageSpan = new ImageSpan(d);
        int end = start + key.length();
        Logger.d(TAG, "start =" + start);
        spannableString.setSpan(imageSpan, start, end,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        int indexOf = spannableString.toString().substring(end).indexOf(key);
        if (indexOf != -1) {
            Logger.d(TAG, indexOf + "indexOf");
            dealExpression(context, substring, spannableString, textSize, key,
                    icons, end + indexOf);
        }
    }

 

(5)核心代码

str用来创建SpannableString。
SpannableString ss = new SpannableString(str);
drawable用来创建ImageSpan。
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
那这两个是怎么关联的呢?就是下一行代码:
ss.setSpan(span, 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
用span来对应ss中从0到str.length()这段字符串。
最后调用显示
editText.getText().insert(cursor, ss);

 

(6)感谢第一篇文章的链接的博主Maybe_ren

 


 

相关文章推荐

android高仿微信表情输入与键盘输入(详细实现分析)

转载请注明出处: http://blog.csdn.net/javazejian/article/details/52126391  最近公司在项目上要使用到表情与键盘的切换输入,自己实现了一个,还...

Android 表情功能的完整处理方案

概述 1.原理和实现思路 2.表情图片显示 3.表情面板 4.表情的输入框插入和删除 5.表情添加脚本 Android中表情功能,一般都不是用ImageView...

安卓开发-最简单快速的仿微信聊天实现-附赠微信原生表情,QQ原生表情

前言;正常实现聊天功能想必大家都使用三方的Sdk比如环信融云集成的,但是聊天记录的保存只能有三天,想增加保存时长就需要花钱,so 我只好自己想办法实现了,这个demo是类似于留言板,并非即时通讯!只实...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Java微信二次开发之9-微信表情发送、接收、同步

想大家对QQ表情一定不会陌生,一个个小头像极大丰富了聊天的乐趣,使得聊天不再是简单的文字叙述,还能够配上喜、怒、哀、乐等表达人物心情的小图片。本文重点要介绍的内容就是如何在微信公众平台使用QQ表情,即...
  • LanBuff
  • LanBuff
  • 2016年09月06日 15:33
  • 916

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Android表情文字EmotionText解析

现在几乎所有的聊天类App都支持输入表情混合文字,但是Android的EditText通常只能输入文字。 今天就来实现一个可以显示表情混合文字的EditText和TextView。老规矩,在节目开始...

android 过滤字符串中的Emoji表情

在网上找了一部分,然后根据自己的项目进行了改动。 1.没有采用自定义的表情发送模式,而是直接套用的android系统里面的Emoji表情。 2.在表情发送的时候,也没有采用Unicode编码方式。...

AndroidEmojiDemo 支持emoji的表情解析

androidemojidemo,支持iphone的emoji表情显示,支持Android端的表情选择,翻页选择,以及发送。...
  • hwl1314
  • hwl1314
  • 2013年12月17日 12:17
  • 20538
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android 解析微信基础表情并显示
举报原因:
原因补充:

(最多只允许输入30个字)