Uniied emoji for Android

Source: Internet
Author: User

This is the most annoying thing I 've ever encountered since I made Android. It's a damn emoji expression, and it's so annoying .. I have no choice but to allocate this question to me. I can only do it with my scalp.

0. Spit it out first

First of all, you need to understand what emoji is and what Google does not know. You do not need to support emoji.

There are many different versions of emoji. I hate different versions of TM. Uniied DoCoMo KDDI Softbank Google

Since ios5 is upgraded, the emoji code has changed from Softbank to uniied. Therefore, it can only be changed on the android side. Sad.

The job I want to do is to filter out the unified emoji encoding contained in the message, and map the corresponding emoticon resources to display them.

1. preparations.

The history of emoji on Google. There is a source code on Google code, but it is Java, so you need to change it to Java .. (I remember that when I first parsed GIF expressions, I also changed Java to Android. Sorry, I always did this ..)

Here there is a link, and all the above codes are converted accordingly. Emoji for PHP

Then Google Code contains all the corresponding encoded and converted XML. It is called emoji4unicode. xml. Here is link: emoji4unicode

If you are not interested, I will post it directly later.CodeBut you 'd better understand the principle of resolution first. Emoji has been increasing. If you want to be compatible with it in the future, you have to find a solution on your own.

2. Example

Black Sun with rays uniied: U + 2600 Softbank: U + e04a

If you have previously parsed Softbank, you should be familiar with it. Well, it has a general order. when parsing, you only need to determine whether it is within the Unicode range. So easy

However, unified is completely unordered, so you must build the ing yourself.

Because the original emoji. XML is very large, and all the description information is put in it. I don't need it, so I wrote a Java file to parse the XML again, then a new XML is generated. Paste a picture to see it.

You see, the original XML is too big, with 162 KB, and it is still XML parsing .. You can think about memory and time consumption. So it must be converted again.

So this process is complete, and you can proceed to the next step ..

3. parsing process.

Because emoji has emoji groups, you should give priority to resolve the group.

Hashmap <string, arraylist <string> emomap = new hashmap <string, arraylist <string> ();

This is how I group data.

Parse the XML and convert the string 2600 to Unicode. This is critical, and the ing cannot be parsed. Note that some emoji are composed of two Unicode U + 1f1f0 U + 1f1f7 .. So. There is another link.

Hashmap <list <integer>, string> convertmap = new hashmap <list <integer>, string> ();

Define a map to store the ing between Unicode and string characters.

If (Xmlpull. getname (). Equals ("e ")){
Fromattr = xmlpull. nexttext ();
Emos. Add (fromattr );
List <integer> fromcodepoints = New Arraylist <integer> ();
If (Fromattr. Length ()> 6 ){
String [] froms = fromattr. Split ("\\_");
For (String part: froms ){
Fromcodepoints. Add (integer. parseint (part, 16 ));
}
} Else {
Fromcodepoints. Add (integer. parseint (fromattr, 16 ));
}
Convertmap. Put (fromcodepoints, fromattr );
}

In this way, the entire Parsing is written into the memory. This is a singleton.ProgramInitialize in the application.

4. parsing process

Here I parse the emoji expression and change it to [e] 2600 [/E. Then use the regular expression to parse it again (why .. Because the message cannot only contain emoji... You need to parse another expression .)

The following method is used to filter emoji:

Public String parseemoji (string input ){
If (Input = Null | Input. Length () <= 0 ){
Return "";
}
Stringbuilder result = New Stringbuilder ();
Int [] Codepoints = tocodepointarray (input );
List <integer> key = Null ;
For ( Int I = 0; I <codepoints. length; I ++ ){
Key = New Arraylist <integer> ()
If (I + 1 <codepoints. Length ){
Key. Add (codepoints [I]);
Key. Add (codepoints [I + 1]);
If (Convertmap. containskey (key )){
String value = convertmap. Get (key );
If (Value! = Null ){
Result. append ("[e]" + value + "[/E]");
}
I ++;
Continue ;
}
}
Key. Clear ();
Key. Add (codepoints [I]);
If (Convertmap. containskey (key )){
String value = convertmap. Get (key );
If (Value! = Null ){
Result. append ("[e]" + value + "[/E]");
}
Continue ;
}
Result. append (character. tochars (codepoints [I]);
}
Return Result. tostring ();
}

Here, you may feel disconnected. At that time, I got stuck for two or three days and kept searching for information online to decompile others' code. There are not many codes for the final solution. The most important thing is how to think about it.

4. render the image.

Public Static Spannablestringbuilder convettohtml (string content, context mcontext ){
String RegEx = "\ [E \] (. *?) \ [/E \] ";
Pattern pattern = pattern. Compile (RegEx );
String emo = "";
Resources resources = mcontext. getresources ();
String Unicode = emojiparser. getinstance (mcontext). parseemoji (content );
Matcher = pattern. matcher (UNICODE );
Spannablestringbuilder sbuilder = New Spannablestringbuilder (UNICODE );
Drawable = Null ;
Imagespan span = Null ;
While (Matcher. Find ()){
Emo = matcher. Group ();
Try {
Int Id = resources. getidentifier (
"Emoji _" + emo. substring (EMO. indexof ("]") + 1, emo. lastindexof ("[")),
"Drawable", "package ");
If (ID! = 0 ){
Drawable = resources. getdrawable (ID );
Drawable. setbounds (0, 0, 24, 24 );
SPAN = New Imagespan (drawable );
Sbuilder. setspan (span, matcher. Start (), matcher. End (),
Spannable. span_exclusive_exclusive );
}
} Catch (Exception e ){
Break ;
}
}
Return Sbuilder;
}

Okay, it is resolved successfully. But !! You have to get the picture first. I also need this format.

Post Images

Well, I also wrote a Java program to convert the Softbank name to the uniied one by one, fortunately, my colleague woke up with a dream .. Otherwise, I would probably have vomited blood and died. This is how the program writes more, and the thinking is fixed...

5. Send an emoji message.

Now that it can be displayed, you still have to send it.

Directly paste the code into a restoration process

Public Static String converttomsg (charsequence CS, context mcontext ){
Spannablestringbuilder SSB = New Spannablestringbuilder (CS );
Imagespan [] spans = SSB. getspans (0, CS. Length (), imagespan. Class );
For ( Int I = 0; I <spans. length; I ++ ){
Imagespan span = spans [I];
String c = span. getsource ();
Int A = SSB. getspanstart (SPAN );
Int B = SSB. getspanend (SPAN );
If (C. Contains ("emoji ")){
SSB. Replace (a, B, convertunicode (c ));
}
}
SSB. clearspans ();
Return SSB. tostring ();
}
Private Static String convertunicode (string emo ){
Emo = emo. substring (EMO. indexof ("_") + 1 );
If (EMO. Length () <6 ){
Return New String (character. tochars (integer. parseint (emo, 16 )));
}
String [] emos = emo. Split ("_");
Char [] Char0 = character. tochars (integer. parseint (EMOs [0], 16 ));
Char [] Char1 = character. tochars (integer. parseint (EMOs [1], 16 ));
Char [] Emoji = New Char [Char0.length + char1.length];
For ( Int I = 0; I <char0.length; I ++ ){
Emoji [I] = char0 [I];
}
For ( Int I = char0.length; I <emoji. length; I ++ ){
Emoji [I] = char1 [I-char0.length];
}
Return New String (emoji );
}

The time span is long. I wrote it at the beginning of the year, but I cannot remember it. So try not to ask me, dear friends, I only provide solutions.

I will also upload a copy of the code. If you need it, you can play it.

Source code: Http://www.stay4it.com /? P = 123

income from hard work, do not reprint ~

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.