高仿微信表情控件 -- LQREmojiLibrary

LQREmojiLibrary

一个超级牛逼的表情库,可使用表情及贴图功能,方便好用,抽离图片加载接口,让开发者自己选择图片加载工具。

码云:

https://git.oschina.net/CSDNLQR/LQREmojiLibrary

GitHub:

https://github.com/GitLqr/LQREmojiLibrary

一、简述

这个库相当牛逼,好用。这个库相当牛逼,好用。这个库相当牛逼,好用。好了,接下来直接看效果图吧:

DemoApp下载

二、引用初始化

1、在自己项目中添加本项目依赖:

compile 'com.lqr.emoji:library:1.0.2'

2、初始化

使用本库必须在自定义的Application中使用LQREmotionKit对库进行初始化,LQREmotionKit提供了四种初始化方法,请根据自己的需要选择。

*使用前需要注意以下几点:

  1. 本库抽离出了图片加载接口,可让开发者自己选择图片加载工具(如:Glide、UIL等),所以使用本库必须实现IImageLoader接口。
  2. 本库支持设置贴图的存放路径,这意味着开发者可以根据自己项目需求修改贴图的存放位置,并且支持贴图自定义。默认的贴图存放在/data/data/包名/files/stickers 目录下。

1)不带IImageLoader的init()

public static void init(Context context)

public static void init(Context context, String stickerPath)

2)带IImageLoader的init()

public static void init(Context context, IImageLoader imageLoader)

public static void init(Context context, String stickerPath, IImageLoader imageLoader)

3)示例

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        LQREmotionKit.init(this, new IImageLoader() {
            @Override
            public void displayImage(Context context, String path, ImageView imageView) {
                Glide.with(context).load(path).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
            }
        });
    }
}

三、表情功能集成

1、布局中使用EmotionLayout控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <!--内容区-->
    <LinearLayout
        android:id="@+id/llContent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        ...
        这里一般是放消息列表,和内容输入框等控件
        ...

    </LinearLayout>

    <!--表情区-->
    <com.lqr.emoji.EmotionLayout
        android:id="@+id/elEmotion"
        android:layout_width="match_parent"
        android:layout_height="270dp"
        android:visibility="gone"/>

</LinearLayout>

2、实现输入框图文混排

1)将内容输入框交给EmotionLayout管理(强烈建议!!!)

mElEmotion.attachEditText(mEtContent);

2)实现IEmotionSelectedListener接口,手动实现图文混排(有自己的实现方式的,可以采用这种方式)

mElEmotion.setEmotionSelectedListener(new IEmotionSelectedListener() {
    @Override
    public void onEmojiSelected(String key) {
        if (mEtContent == null)
            return;
        Editable editable = mEtContent.getText();
        if (key.equals("/DEL")) {
            mEtContent.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
        } else {
            int start = mEtContent.getSelectionStart();
            int end = mEtContent.getSelectionEnd();
            start = (start < 0 ? 0 : start);
            end = (start < 0 ? 0 : end);
            editable.replace(start, end, key);

            int editEnd = mEtContent.getSelectionEnd();
            MoonUtils.replaceEmoticons(LQREmotionKit.getContext(), editable, 0, editable.toString().length());
            mEtContent.setSelection(editEnd);
        }
    }

    @Override
    public void onStickerSelected(String categoryName, String stickerName, String stickerBitmapPath) {

    }
});

3、实现内容区与表情区仿微信切换效果

private EmotionKeyboard mEmotionKeyboard;

private void initEmotionKeyboard() {
    mEmotionKeyboard = EmotionKeyboard.with(this);
    mEmotionKeyboard.bindToContent(mLlContent);
    mEmotionKeyboard.bindToEmotionButton(mIvEmo);
    mEmotionKeyboard.bindToEditText(mEtContent);
    mEmotionKeyboard.setEmotionLayout(mElEmotion);
}

4、效果

经过上面几步,就可以实现以下效果了:

四、贴图功能集成

1、设置贴图的存放位置

这一步可略过,不设置的话,贴图的默认存放位置是 /data/data/包名/files/stickers ,可通过LQREmotionKit.getStickerPath()获得。

贴图的存放位置只能通过LQREmotionKit的init()来设置:

LQREmotionKit.init(this, Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+"sticker");

LQREmotionKit.init(this, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "sticker", new IImageLoader() {
        @Override
        public void displayImage(Context context, String path, ImageView imageView) {
            Glide.with(context).load(path).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
        }
    });

2、将贴图下载到指定贴图的存放位置

1)自带贴图

本库支持 集成默认贴图,可将贴图按规则放置在assets的sticker目录下,当程序启动时,会自动将assets的sticker目录下所有的贴图复制到贴图的存放位置。

2)网络下载贴图

//得到贴图的存放位置
String stickerPath = LQREmotionKit.getStickerPath();
...
网络下载(这里不同项目实现方式不同,请根据自己的项目实现该部分代码)
...

3、监听用户点击贴图事件

mElEmotion.setEmotionSelectedListener(new IEmotionSelectedListener() {
    @Override
    public void onEmojiSelected(String key) {
        
    }

    @Override
    public void onStickerSelected(String categoryName, String stickerName, String stickerBitmapPath) {
        String stickerPath = stickerBitmapPath;
        ...
        发送图片
        ...
    }
});

4、效果

经过上面几步,就可以实现以下效果了:

五、拓展按钮的控制

1、设置表情控件的拓展按钮

默认表情控件的底部Tab是不显示“添加”按钮和“设置”按钮的,如果需要,可通过以下代码进行控制。

mElEmotion.setEmotionAddVisiable(true);
mElEmotion.setEmotionSettingVisiable(true);
mElEmotion.setEmotionExtClickListener(new IEmotionExtClickListener() {
    @Override
    public void onEmotionAddClick(View view) {
        Toast.makeText(getApplicationContext(), "add", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEmotionSettingClick(View view) {
        Toast.makeText(getApplicationContext(), "setting", Toast.LENGTH_SHORT).show();
    }
});

2、效果

欢迎关注微信公众号:全栈行动
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,577评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,626评论 4 59
  • 函数的创建方式: ① 函数声明 ② 函数表达式 ③ 使用Function类型的构造器创建 代码示例 关键字new的...
    码农的世界你不懂阅读 284评论 0 0
  • 走过青春岁月,慢慢放下脚步,回头一看,来时的路竟渐渐模糊,记忆中的片段零零散散 给老师的恶作剧 因为不满老师的处罚...
    丩丩阅读 102评论 3 2
  • 墙上芦苇,头重脚轻根底浅; 山间竹笋,嘴尖皮厚腹中空 ――(明)解缙 身体透支,健康会亮起红灯;金钱透支,经济会陷...
    裕文书屋阅读 402评论 2 4