Android使用Emoji探索

1. Emoji介绍

在android4.4以前,无法使用像苹果那样的emoji字体样式,最简单的解决方案是使用ImageSpan配合SpannableString,而在4.4及之后的版本,则可以直接采用类似iOS/OSX的方案进行快速渲染。

2. 直接使用

TextView.setText("")

3. 兼容性使用

https://github.com/googlesamples/android-EmojiCompat

public class EmojiHelper {

    private static boolean sEmojiReady = false;

    public static void initCompat() {

        EmojiCompat.Config config = new BundledEmojiCompatConfig(AppContext.get());
//        EmojiCompat.Config config = new XLEmojiCompatConfig();

        config.setReplaceAll(true);
        config.registerInitCallback(new EmojiCompat.InitCallback() {
            @Override
            public void onInitialized() {
                super.onInitialized();
                XLog.d("onInitialized");
            }

            @Override
            public void onFailed(@Nullable Throwable throwable) {
                super.onFailed(throwable);
                XLog.printStackTrace(throwable);
            }
        });


        EmojiCompat.init(config);
    }

    public static CharSequence process(CharSequence text) {
        if (sEmojiReady) {
            return EmojiCompat.get().process(text);
        }
        return text;
    }

    private static class XLEmojiCompatConfig extends EmojiCompat.Config {
        XLEmojiCompatConfig() {
            super(new XLMetadataRepoLoader());
        }
    }

    private static class XLMetadataRepoLoader implements EmojiCompat.MetadataRepoLoader {

        @Override
        public void load(@NonNull EmojiCompat.MetadataRepoLoaderCallback loaderCallback) {
            File file = new File("/sdcard/NotoColorEmoji.ttf");
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
                loaderCallback.onLoaded(MetadataRepo.create(Typeface.createFromFile(file.getAbsolutePath()), fileInputStream));
            } catch (IOException e) {
                XLog.printStackTrace(e);
            } finally {
                Util.safeClose(fileInputStream);
            }
        }
    }
}

3. 定制思路探索

https://github.com/googlefonts/nototools
https://github.com/fonttools/fonttools
https://github.com/MitchTalmadge/Emoji-Tools

参考:
https://ragnraok.github.io/android-emoji-font-method.html

https://juejin.im/post/5b7bd7e6f265da43741e10da

Emoji Unicode Tables

http://www.typophile.com/node/103268
http://stevehanov.ca/blog/?id=143

hexdump

你可能感兴趣的:(Android使用Emoji探索)