Android Development, do you encounter Emoji headache?

Source: Internet
Author: User
Tags unsupported

In Android, if you need to use the Emoji expression, you will find that on some devices, some of the Emoji expression will be shown in the form of Tofu block "?", because the current device does not support this Emoji expression.

In Android support, a new emojicompat has been added to solve this problem, Emojicompat to Android 4.4 (Api level 19) and subsequent systems, the Emoji of the extension!

Next we will learn all the details of using Emojicompat!

First, what is Emoji?

Now that we're going to use Emoji, let's start by knowing what Emoji is.

Emoji is a graphic symbol that can be inserted into text. It is a Japanese language, e means "painting", Moji means "text", even together is "emoji".

Emoji was the first time in the 90 's, and Japan Telecom business First support, is to in text messages, insert expression, to enhance the experience of SMS. Apple supported Emoji in the IPhone in 2007 to make it popular around the world.

In the early days, Emoji's implementation was to replace some special combinations of symbols with image expressions, such as replacing them with a. :) , such a solution would make it difficult to standardize the expression of Emoji, and the range of expression was limited.

Beginning in 2010, Unicode began assigning code points to Emoji, that is, the Emoji symbol after that is a font, which is rendered as a picture display.

Emoji is popular because of its characteristic of expressing emotions. Emoji's international standards were introduced in 2015 and are now in the 5.0 version, and in 2018, the Emoji 6.0 (then renamed to Emoji 11, which is actually the Emoji 5.0 upgrade) version, will be released.

As of now, Emoji 5.0, has been included in the Unicode has been 2,623.

Specific details can be queried on this website:

Http://www.unicode.org/emoji/charts/full-emoji-list.html

Here you should be aware that Emoji after standardization, is actually a font, it is Unicode assigned a fixed code point, generally we use standard Unicode to identify a unique Emoji.

Although Emoji has been standardized, different platforms have different effects because of the different fonts used, resulting in the same Unicode representation of the Emoji.

For example, here's an example, the standard Emoji u+1f601, in Apple and Android, while also representing a smiley face, but the effect of rendering is not the same, which we need to understand.

Ii. Emoji status in Android 2.1 How do I use Emoji?

A standard Emoji, in fact, there are many ways to express, for example, first look at the front of the smiley face U+1F601 .

Code, UTF-8, surrogates these forms, can represent the Emoji of this smiley face. Usually this Emoji expression is from the user input data or the service side passes over the data, although these forms can represent this Emoji, but the different format needs the different form to parse.

Normally, we recommend using surrogates to pass Emoji, for example: \uD83D\uDE01 It is a Unicode encoding and is generic and can be displayed directly in TextView.

However, if we don't get surrogates, it's Code, for example 1F601 , so we need to do extra processing. In fact, it is very simple, after a few steps of conversion can be done.

String(Character.toChars(Integer.parseInt("1F601"16)))

The Kotlin code is used in the example, but should not affect reading.

The resulting String object is passed to TextView, which is displayed properly if it is a Emoji supported by the current device.

2.2 Emoji show no situation.

In the way described in the previous section, in fact we did not do any special processing, completely since the device's own font library to perform Emoji rendering. This can cause some Emoji to show up on some devices.

Using this scheme, I use the device I have on hand to see the effect of the display.

Clearly, there are some Emoji that can't be shown and will be shown as a tofu block "?" and that's not what we want.

Let's take a look at how to handle it using EMOJICOMPAT.

Third, use EmojiCompat3.1 what is Emojicompat

According to the official document description, the Emojicompat support library is primarily designed to allow Android devices to display the latest Emoji symbols, which prevents the application from appearing in the form of a tofu block "?", although it is only because your current device does not have this font. With Emojicompat, your device can get the latest Emoji emoticons without waiting for an Android update.

3.2 How to use Emojicompat

Emojicompat Support Library with minimum support for system devices to Android 4.4 (Api level 19).

Emojicompat provides support for two types of fonts, namely:

    1. A downloadable font configuration.
    2. The locally bundled font configuration.

These two uses, in addition to the reference library is different, the most fundamental reason is that the way to download the font, the first time you start to check if there is a local font, not to download the latest Emoji fonts from the Internet, and the local bundled way, in the process of packaging the APP, will be implanted in the latest Emoji the font file, and then encounters an unsupported Emoji, the resource is loaded from the font file and rendered.

At present, the official use of NotoColorEmojiCompat.ttf font files, will be explained in detail later.

I think you should find that the local bundling of the way will embed a font file, virtually increased the size of the APK installation package, but the way to download fonts, and completely rely on Google services, so in the country is basically crippled, in this environment, we can only choose the local bundle of ways to use Emojicompat.

Regardless of the way you configure the font, for Emojicompat, it is not concerned about, it only needs to determine whether the current device supports this Emoji, support the use of the system built-in, not supported, use Emojispans to replace the charsequence, To achieve the effect of replacing the rendering.

The operating principle of the Emojicompat is as shown.

3.3 How to configure a local bundle font

Since downloadable Emoji fonts need to work with Google services, there is no longer much to be described here.

This article mainly explains how to use the local bundling method, using Emojicompat.

The first step is to add Gradle dependencies.

dependencies {    ...    compile "com.android.support:support-emoji-bundled:27.0.2"}

The second step is to initialize the Emojicompat.

The initialization of EMOJICOMPAT requires two steps.

    1. First, you need to generate a Bundledemojicompatconfig object, and its construction method receives a Context.
    2. The method is called again EmojiCompat.init() , and the previously generated config is passed to it for initialization.

The sooner the better, because the initialization is time-consuming, it will load the packaging when the embedded Emoji font file, so it takes about 150ms of time, and occupy about 200kb of memory.

The third step is to use Emojicompat.

After the initialization is complete, there is nothing left to do with it.

The processing logic of Emojicompat is clearly described in the previous picture. It loads a Emoji font and then determines whether the current device supports the Emoji that needs to be displayed, and if not, replaces it with Emojispans and eventually sets the processed charsequence to TextView.

And this process, Emojicompat provides a very simple method process() .

From its signature it can be seen that it accepts a charsequence and processes it, and then returns a charsequence.

Here's an example: Convert a smiley face here.

EmojiCompat.get().process("笑脸: \uD83D\uDE01")

process()You need to accept a Unicode character, so if the resulting data is the Emoji Code mentioned earlier, a separate conversion is required. The process() internal has helped us to complete the conversion, these details do not need our concern, we just need to set the return of the charsequence to TextView on it.

3.4 Emoji AppCompat Widgets

In real-world projects, if you need EmojiCompat.get().process() to deal with strings every time, it's really cumbersome. For this reason, Google also provides support for the corresponding controls for developers.

If you need to use it, you need to introduce a new dependent library.

dependencies {      compile "com.android.support:support-emoji-appcompat:27.0.2"}

Once introduced, the controls provided by Emojiappcompat can be used directly in XML.

Use support-emoji-appcompat just saves our process() steps, but still needs init() .

3.5 Custom Controls Support Emoji

You can always use progress() or use EmojiAppCompatXxx controls, but if you want to customize a control to display Emoji, you need to use the other two helper classes provided by Emojicompat.

    • Emojitextviewhelper
    • Emojieditviewhelper

These two are very simple to use, a control to handle pure presentation, a control for handling the state of the input, very concise and straightforward.

Even if you do not remember, see Emojiappcompattextview and Emojiappcompateditview in the implementation of the way.

Take Emojiappcompattextview here for example, only need to be in a few key positions, the corresponding method of Emojitextviewhelper can be used.

Problems faced by 3.6 Emojicompat

Overall emojicompat is still very useful, no matter which way to load it, in fact, we do not need to do too much intervention.

Refer to the official documentation here for some of the most common questions.

1. What is the download strategy for download fonts?

Emoji font is used for the first time, it detects if it exists on the current device and downloads it in a child thread if it does not exist.

2. How long does it take to initialize?

When a font is already available locally, it takes approximately 150 milliseconds to initialize the Emojicompat.

3, Emojicompat Support library, how much memory will be used?

Currently, after the Emoji font is fully loaded, approximately 200kb of memory is used.

4. What happens to using the Emojiappcompatxxx control on devices below Android 4.4?

Emojicompat has been processed internally, just as in the case of a normal appcompatxxx control on a lower version.

5, the local bundle of Emoji font files, about how big?

The local bundle of Emoji font files NotoColorEmojiCompat.ttf , will be embedded in the directory when packaged assets , now the actual size of 7.4MB, which will directly cause the APK to increase.

For more details, it is recommended that you read the official documentation.

Https://developer.android.google.cn/guide/topics/ui/look-and-feel/emoji-compat.html

Iv. The defects of Emojicompat?

In the actual use of emojicompat in the process, but also encountered a defect can not be counted.

Let's recall what we mentioned earlier, Emojicompat's handling mechanism.

It will only load the font from the support font if the current device encounters an unsupported Emoji, and if so, it will use the System font.

This is not to blame Emojicompat designers, it's starting point, is to solve the Emoji in some devices, the display of Tofu block "?" problem, and do not care whether it shows the latest Emoji, is to solve the problem.

This is very embarrassing, in fact, sometimes the Android device built-in support of the font, the effect is not good, we first look at the use of Emojicompat before and after the contrast effect.

The left side is not using the Emojicompat effect, and the right side is the used effect.

It is clear that Emojicompat helped me with the Emoji expressions supported by my current equipment department, but did not replace the Android jelly expression with the standard Emoji expression.

So, what do we need to do if we want it to show the latest Emoji?

As mentioned earlier, since Emoji began to be standardized, is actually a font, and Emojicompat is also to help us bundle embedded a font package in the assets directory, then we only need to let us display the TextView load this Emoji font, we can solve the problem.

With the idea, let's try this solution.

In this way, we can call the loadEmoji() method, let TextView display Emoji, to see the contrast effect.

From left to right, they are: default Emoji, Emojicompat, Emoji Font display effect.

Dense expression a little more, intensive phobia please leave me,??!

Of course, this is only to provide a solution, in the case of this scenario, basically all of the more than 4.4 of the models, can display the latest Emoji, if the Emoji display performance requirements, this is a solution.

V. Summary

This is what I know about Android Emoji.

What do you have to gain from reading this article? Or do you have any better suggestions about Emoji, welcome to discuss in the message!

Today, in the background of incense Ink Shadow Public, reply to " growth ". I will send you some of my study materials, including: Android Anti-compilation, algorithm, design mode, virtual machine, Linux, Kotlin, Python, crawler, Web project source code.

Recommended reading:

    • From the perspective of Android development, talk about Airbnb's Lottie
    • Kotlin a useful new feature: Parcelize! Kill the serialized template code
    • Looking for a day without a Bug? Try Git's dichotomy!!!
    • How to search the Open Source Library on Github more precisely? You need these tricks!
    • Handwriting your first Dalvik version of HelloWorld!

I hear that people who like to leave a message are not bad luck.

Click on " read the original " to see more exciting content

https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIxNjc0ODExMA== #wechat_redirect

Android Development, do you encounter Emoji headache?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.