Implementation of emojis supported by Android Development

Source: Internet
Author: User

Recently, the project needs to support emoticon. The addition and resolution of emoticon are basically based on the SmileyParser of Android,Directly paste the Code:

Copy codeThe Code is as follows: public class SmileyParser {
Private static SmileyParser sInstance = null;

Private Context mContext = null;
Private Pattern mPattern = null;
Private HashMap <String, Integer> mSmileyTextToId = null;
Private final String [] mSmileyArrays =
{"/Watermelon", "89", "/poop", "59", "/taixiao", "74", "/sneer", "20 ", "/arrogant", "23", "/again", "39", "/withered", "64", "/Daze", "3 ", "/angry", "11", "/zhuyun", "54", "/cute", "21", "/zhutou", "46 ", "/coffee", "60", "/yawning", "104", "/cool", "105", "/grievance", "106 ", "/cry", "107", "/108", "109", "/kiss", "110 ", "/tolerance", "111", "/chopper", "112", "/beer", "113", "/tolerance ball", "114 ", "/ping-pong", "115", "/", "116", "/", "117", "/", "118 ", "/seduce", "119", "/fist", "120", "/poor", "121", "/love you", "122 ", "/NO", "123", "/OK", "124", "/incircle", "125", "/inhead", "126 ", "/", "127", "/", "128", "/shoushou", "129", "/exciting", "130 ", "/street dance", "131", "/half kiss", "132", "/left Taibei", "133", "/right Taibei ", "134", "/vomit", "19", "/cake", "53", "/fangya", "13", "/curse ", "31", "/football", "57", "/hush", "33", "/sleepy", "25", "/soldier", "29 ", "/cry", "9", "/strong", "76", "/strong", "30", "/strong hug", "49 ", "/shy", "6", "/simhei", "10", "/right hum", "103", "/shihuo", "86 ", "/", "79", "/smite", "4", "/", "14", "/heartbroken", "67 ", "/terrorism", "26", "/smile", "0", "/Xiao", "28", "/crazy", "18 ", "/torture", "35", "/Shake", "41", "/handshake", "78", "/kiss", "85 ", "/applaud", "99", "/grin", "1", "/tap", "38", "/seek", "34 ", "/moon", "75", "/sweating", "27", "/streaming", "5", "/moon is too big", "100 ", "/love heart", "66", "/left hum", "102", "/Rose", "63", "/suspect", "32 ", "/white eyes", "22", "/sleep", "8", "/cold sweat", "96", "/show love", "65 ", "/weak", "77", "/jump", "43", "/color", "2", "/blow up", "55 ", "/Xiao", "101", "/Fu", "36", "/Dao", "56", "/Xiao Pi", "12 ", "/snore", "98", "/cool", "16", "/TSL", "69", "/slogan", "7 ", "/modified", "15", "/zhuyun", "24", "/zhuyun", "61", "/zhuyun", "37 ", "/Love", "42 "};
Private int [] mSmileyIds = null;
Private String [] mSmileyTexts = null;
Public static SmileyParser getInstance (){
If (sInstance = null ){
SInstance = new SmileyParser (GameDataMgr. getInstance (). getActivity ());

}

Return sInstance;
}
Private SmileyParser (Context context ){
// TODO Auto-generated constructor stub
MContext = context;
InitSmileyIds ();
MPattern = buildPattern ();
MSmileyTextToId = buildSmileyRes ();
}

Private void initSmileyIds (){
MSmileyIds = new int [mSmileyArrays. length/2];
MSmileyTexts = new String [mSmileyArrays. length/2];
For (int I = 0; I <mSmileyArrays. length/2; I ++ ){
MSmileyTexts [I] = mSmileyArrays [I * 2];
MSmileyIds [I] = Integer. parseInt (mSmileyArrays [I * 2 + 1]);
}
}

Public int [] getSmileyIDs (){
Return mSmileyIds;
}

Public int getSmileyResourceId (int smileyId ){
String idString = "face _" + Integer. toString (smileyId );

Int id = getResId (idString, mContext, R. drawable. class );

Return id;
}

Public static int getResId (String variableName, Context context, Class <?> C ){

Try {
Field idField = c. getDeclaredField (variableName );
Return idField. getInt (idField );
} Catch (Exception e ){
E. printStackTrace ();
Return-1;
}
}

Public String [] getSmileyTexts (){
Return mSmileyTexts;
}

Drawable getSmileyDrawable (int id ){
Drawable drawable = null;
Drawable = mContext. getResources (). getDrawable (getSmileyResourceId (id ));

Return drawable;

}

/**
* Create a String-Id correspondence
*/
Private HashMap <String, Integer> buildSmileyRes (){

HashMap <String, Integer> smileyTextToId = new HashMap <String, Integer> (mSmileyIds. length );
For (int I = 0; I <mSmileyIds. length; ++ I ){
SmileyTextToId. put (mSmileyTexts [I], mSmileyIds [I]);
}

Return smileyTextToId;
}

/**
* Create a regular expression for matching
* @ Return
*/
Private Pattern buildPattern (){
StringBuilder builder = new StringBuilder (mSmileyTexts. length * 3 );
Builder. append ('(');
For (String s: mSmileyTexts ){
Builder. append (Pattern. quote (s ));
Builder. append ('| ');
}

Builder. replace (builder. length ()-1, builder. length (),")");

Return Pattern. compile (builder. toString ());
}

/**
* Convert text into images
* @ Param text
* @ Return
*/
Public Spannable addSmileySpans (CharSequence text ){
SpannableStringBuilder spBuilder = new SpannableStringBuilder (text );

Matcher matcher = mPattern. matcher (text );

While (matcher. find ()){
Int id = mSmileyTextToId. get (matcher. group ());
Matcher. start (), matcher. end (), Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
SpBuilder. setSpan (new ImageSpan (mContext, getSmileyResourceId (id), ImageSpan. ALIGN_BASELINE), matcher. start (), matcher. end (), Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );

}

Return spBuilder;
}
}

There is a small problem in implementation: when adding an emoticon to TextView, it is normal to display the text when there are both emoticon and text, but when there is only emoticon in the text, the facial expression is displayed on the top of the page, and some of the above are truncated.

The TextView layout is as follows:
Copy codeThe Code is as follows: <TextView

Android: id = "@ + id/comment_item_content"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: layout_marginTop = "10dp"

Android: layout_marginBottom = "10dp"

Android: textSize = "16sp"

Android: textColor = "#333333"

/>

Solution: the problem here is that TextView determines the line spacing based on the font. However, when the text is an emoticons, this determines that the line spacing is too small, therefore, it is truncated when the expression is displayed. The solution is to set the minimum height of the TextView and specify the downward alignment of the text. In addition, if you specify the ImageSpan. ALIGN_BOTTOM Alignment Method when creating an ImagePan, this problem is generally not found, but in this way, the facial expression will show down.

After modification, the TextView layout is as follows:
Copy codeThe Code is as follows: <TextView

Android: id = "@ + id/comment_item_content"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: layout_marginTop = "10dp"

Android: layout_marginBottom = "10dp"

Android: textSize = "16sp"

Android: textColor = "#333333"

Android: minHeight = "25dp"

Android: gravity = "bottom"

/>

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.