Android programming to achieve QQ expression sent and received a complete example (source code) _android

Source: Internet
Author: User

This article describes the Android programming to achieve QQ expression sent and received. Share to everyone for your reference, specific as follows:

In their own to do a chat application practice, need to use the expression, so want to imitate the QQ expression, picture resources complete copy of the qq.apk, decompression can be obtained, here do not elaborate.

The following will be the application of the expression module function out, so that their review after review.

Let's take a look at the effect chart:

First enter the interface: (completely imitate QQ)

Click on the expression icon above:

Select some expressions and enter some text mix:

Click to send:

You can see both the text and the facial images showing up together.

Some key codes are listed below:

Expression Tool Class Expressionutil:

The public class Expressionutil {/** * makes a regular judgment of spanablestring and, if it meets the requirements, replaces the @param context * @param with an expression picture spannables Tring * @param patten * @param start * @throws SecurityException * @throws nosuchfieldexception * @throws N Umberformatexception * @throws illegalargumentexception * @throws illegalaccessexception/public static void Dealexpression (Context context,spannablestring spannablestring, Pattern patten, int start) throws SecurityException, Nosuchfieldexception, NumberFormatException, IllegalArgumentException, illegalaccessexception {Matcher Matcher = Patt 
  En.matcher (spannablestring); 
   while (Matcher.find ()) {String key = Matcher.group (); 
   if (Matcher.start () < start) {continue; 
   Field field = R.drawable.class.getdeclaredfield (key); int resid = Integer.parseint (Field.get (null). ToString ()); Generates the picture resource ID if (resid!= 0) {Bitmap Bitmap = Bitmapfactory.decoderesource (Context.getresources (), RE SID); Imagespan Imagespan = new Imagespan (bitmap); The image resource ID is used to get bitmap, with a imagespan to wrap int end = Matcher.start () + key.length (); Calculates the length of the name of the picture, which is the length of the string to be replaced Spannablestring.setspan (Imagespan, Matcher.start (), End, spannable.span_inclusive_ EXCLUSIVE); 
     Replaces the picture in the specified position in the string if (End < Spannablestring.length ()) {//If the entire string has not been validated, continue ... 
    Dealexpression (Context,spannablestring, Patten, end); 
   } break; }}/** * Gets a Spanablestring object, through the passed-in string, and makes a regular judgment * @param context * @param str * @return/Publi C Static spannablestring getexpressionstring (context context,string str,string Zhengze) {spannablestring SpannableStri 
  ng = new spannablestring (str);  Pattern Sinapatten = Pattern.compile (Zhengze, pattern.case_insensitive); 
  Generates a pattern try {dealexpression (context,spannablestring, Sinapatten, 0) by passing in a regular expression; 
  catch (Exception e) {log.e ("Dealexpression", E.getmessage ()); 
 return spannablestring;

 } 
}

In the adapter that displays the list of chat pages, we need to do the following, that is, the method of invoking the above tool class:

Internal class Viewholder in Simplechatadapter:

Private class viewholder{relativelayout chat_layout; 
  ImageView image; 
  TextView text; Public Viewholder (View convertview) {chat_layout= (relativelayout) Convertview.findviewbyid (r.id.team_singlechat_id 
   _listiteam); 
   Image= (ImageView) Convertview.findviewbyid (R.id.team_singlechat_id_listiteam_headicon); 
  text= (TextView) Convertview.findviewbyid (r.id.team_singlechat_id_listiteam_message); public void SetData (Messageinfo msg) {relativelayout.layoutparams rl_chat_left= (relativelayout.layoutparams) Chat 
   _layout.getlayoutparams ()); 
   Relativelayout.layoutparams rl_tv_msg_left= ((relativelayout.layoutparams) text.getlayoutparams ()); 
   Relativelayout.layoutparams rl_iv_headicon_left= ((relativelayout.layoutparams) image.getlayoutparams ()); if (! DicqConstant.DEFAULTMAC.equalsIgnoreCase (Msg.getusermac ()) {//According to the local MAC address to determine whether the information belongs to me or the other party said//If it is said by himself, then show on the right 
    If it is said by the other side, it is displayed on the left Rl_chat_left.addrule (relativelayout.align_parent_left,-1); Rl_chat_left.addrulE (relativelayout.align_parent_right,0); 
    Rl_iv_headicon_left.addrule (relativelayout.align_parent_left,-1); 
    Rl_iv_headicon_left.addrule (relativelayout.align_parent_right,0); 
    Rl_tv_msg_left.addrule (Relativelayout.right_of,r.id.team_singlechat_id_listiteam_headicon); 
    Rl_tv_msg_left.addrule (relativelayout.left_of,0); 
   Text.setbackgroundresource (R.drawable.balloon_l_selector); 
    }else{Rl_chat_left.addrule (relativelayout.align_parent_left,0); 
    Rl_chat_left.addrule (relativelayout.align_parent_right,-1); 
    Rl_iv_headicon_left.addrule (relativelayout.align_parent_left,0); 
    Rl_iv_headicon_left.addrule (relativelayout.align_parent_right,-1); 
    Rl_tv_msg_left.addrule (relativelayout.right_of,0); 
    Rl_tv_msg_left.addrule (Relativelayout.left_of,r.id.team_singlechat_id_listiteam_headicon); 
   Text.setbackgroundresource (R.drawable.balloon_r_selector);  } image.setimageresource (Prortaitutils.conversionidtores (msg.getprotrait ())); Set Avatar Stringstr = msg.getmsg (); Message specific content String Zhengze = "f0[0-9]{2}|f10[0-7]"; A regular expression that is used to determine whether there are expressions in the message try {spannablestring spannablestring = expressionutil.getexpressionstring (context, str, ZHE 
    Ngze); 
   Text.settext (spannablestring); 
   catch (NumberFormatException e) {e.printstacktrace (); 
   catch (SecurityException e) {e.printstacktrace (); 
   catch (IllegalArgumentException e) {e.printstacktrace ();

 } 
  } 
}

The implementation of the expression pop-up box is as follows:

Mainactivity:

/** * Create an Expression selection dialog box */private void Createexpressiondialog () {builder = new Dialog (mainactivity.this); 
  GridView GridView = Creategridview (); 
  Builder.setcontentview (GridView); 
  Builder.settitle ("Default expression"); 
  Builder.show ();  Gridview.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> 
    arg0, View arg1, int arg2, long arg3) {Bitmap Bitmap = null; 
    Bitmap = Bitmapfactory.decoderesource (Getresources (), imageids[arg2% imageids.length]); 
    Imagespan Imagespan = new Imagespan (mainactivity.this, bitmap); 
    String str = NULL; 
    if (arg2<10) {str = "f00" +ARG2; 
    }else if (arg2<100) {str = "F0" +ARG2; 
    }else{str = "F" +ARG2; 
    } spannablestring spannablestring = new spannablestring (str); 
    Spannablestring.setspan (Imagespan, 0, 4, spannable.span_exclusive_exclusive); 
    Edit.append (spannablestring); 
   Builder.dismiss (); 
} 
  }); /** * Generates an expression in the dialog boxGridView * @return/Private GridView Creategridview () {final GridView view = new GridView (this); 
  list<map<string,object>> ListItems = new arraylist<map<string,object>> (); Generates 107 expression IDs, encapsulates for (int i = 0; i < i++) {try {if (i<10) {Field field = r.drawable.class.getd 
     Eclaredfield ("f00" + i); 
     int ResourceID = Integer.parseint (Field.get (null). ToString ()); 
    Imageids[i] = ResourceID; 
     }else if (i<100) {Field field = R.drawable.class.getdeclaredfield ("F0" + i); 
     int ResourceID = Integer.parseint (Field.get (null). ToString ()); 
    Imageids[i] = ResourceID; 
     }else{Field field = R.drawable.class.getdeclaredfield ("f" + i); 
     int ResourceID = Integer.parseint (Field.get (null). ToString ()); 
    Imageids[i] = ResourceID; 
   } catch (NumberFormatException e) {e.printstacktrace (); 
   catch (SecurityException e) {e.printstacktrace (); catch (IllegalArgumentException E) {e.printstacktrace (); 
   catch (Nosuchfieldexception e) {e.printstacktrace (); 
   catch (Illegalaccessexception e) {e.printstacktrace (); 
   } map<string,object> ListItem = new hashmap<string,object> (); 
   Listitem.put ("image", imageids[i]); 
  Listitems.add (ListItem); } simpleadapter Simpleadapter = new Simpleadapter (this, ListItems, R.layout.team_layout_single_expression_cell, new STR 
  ing[]{"Image"}, new Int[]{r.id.image}); 
  View.setadapter (Simpleadapter); 
  View.setnumcolumns (6); 
  View.setbackgroundcolor (Color.rgb (214, 211, 214)); 
  View.sethorizontalspacing (1); 
  View.setverticalspacing (1); 
  View.setlayoutparams (New Layoutparams (layoutparams.fill_parent,layoutparams.wrap_content)); 
  View.setgravity (Gravity.center); 
return view; 

 }

Full instance code code click here to download the site.

I hope this article will help you with the Android program.

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.