閱讀744 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android 新浪微博客戶端的表情功能的實現

最近在搞android 新浪微博客戶端,有一些心得分享
弄android客戶端表情功能可以用以下思路
1.首頁把新浪的表情下載到本地一文件夾種,表情圖片的命名要用新浪微博表情原來的命名
比如 新浪的害羞表情是shame.gif 那麼你在本地也得命名為shame.gif,命名相同主要是為了能夠匹配表情對應的code.
2.把本地的表情都放進android的資源文件裏----drawable下麵
3.訪問新浪的表情接口(新浪返回的數據類型有json和xml兩種,本人用xml),把返回的信息,利用xml解析器解析出來的信息儲存在一個Emotion.java的bean裏,這樣就可以根據Emotion.java的code找到一一對應的資源表情圖片了
4.實現一個可以讓用戶選擇的表情界麵,本人用GridView實現
5.實現點擊GridView的每一個item,處理根據item的index查找對應的表情code,然後再把code利用正則把code轉換為相對應的表情圖片,最後表情插入EditText進行發送。

下麵是具體的實現過程
1.把新浪表情圖片下載到本地的實現如下:(這個可以建一個java工程進行下載)

 

Java代碼  
  1. public void getFriendList() throws Exception {   
  2.         BlogReleaseServiceImpl service = new BlogReleaseServiceImpl();   
  3.         List<Emotions> list = service.getEmotion();   
  4.         for (Emotions emotions : list) {   
  5.             String path = emotions.getUrl();   
  6.             String filename = path.substring(path.lastIndexOf("/") + 1,path.length());   
  7.             URL url =  new URL(path);   
  8.             HttpURLConnection conn = (HttpURLConnection)url.openConnection();   
  9.             conn.setRequestMethod("GET");   
  10.             conn.setReadTimeout(5 * 1000);   
  11.             if(conn.getResponseCode() == 200){   
  12.                 InputStream is = conn.getInputStream();   
  13.                 byte[] data = readStream(is);   
  14.                 File file = new File("f: \\sina_images\\" + filename);   
  15.                 FileOutputStream fs = new FileOutputStream(file);   
  16.                 fs.write(data);   
  17.                 fs.close();   
  18.             }else{   
  19.                 System.out.println("請求失敗");   
  20.             }   
  21.         }   
  22.     }   
  23.        
  24.     public byte[] readStream(InputStream is) throws Exception {   
  25.         ByteArrayOutputStream os = new ByteArrayOutputStream();   
  26.         byte[] buffer = new byte[2048];   
  27.         int len = 0;   
  28.         while((len = is.read(buffer)) != -1){   
  29.             os.write(buffer,0,len);   
  30.         }   
  31.         is.close();   
  32.         return os.toByteArray();   
  33.     }  
  1. public void getFriendList() throws Exception {  
  2.         BlogReleaseServiceImpl service = new BlogReleaseServiceImpl();  
  3.         List<Emotions> list = service.getEmotion();  
  4.         for (Emotions emotions : list) {  
  5.             String path = emotions.getUrl();  
  6.             String filename = path.substring(path.lastIndexOf("/") + 1,path.length());  
  7.             URL url =  new URL(path);  
  8.             HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  9.             conn.setRequestMethod("GET");  
  10.             conn.setReadTimeout(5 * 1000);  
  11.             if(conn.getResponseCode() == 200){  
  12.                 InputStream is = conn.getInputStream();  
  13.                 byte[] data = readStream(is);  
  14.                 File file = new File("f: \\sina_images\\" + filename);  
  15.                 FileOutputStream fs = new FileOutputStream(file);  
  16.                 fs.write(data);  
  17.                 fs.close();  
  18.             }else{  
  19.                 System.out.println("請求失敗");  
  20.             }  
  21.         }  
  22.     }  
  23.       
  24.     public byte[] readStream(InputStream is) throws Exception {  
  25.         ByteArrayOutputStream os = new ByteArrayOutputStream();  
  26.         byte[] buffer = new byte[2048];  
  27.         int len = 0;  
  28.         while((len = is.read(buffer)) != -1){  
  29.             os.write(buffer,0,len);  
  30.         }  
  31.         is.close();  
  32.         return os.toByteArray();  
  33.     }  

 

 2:把本地的表情都放進android的資源文件裏----drawable下麵(這個就不用多說了,直接選取所有文件複製就行了)

 

3:

3.1訪問新浪的表情接口,把返回的信息如下:

 

 

Xml代碼  
  1.      <emotion>  
  2. <phrase>[嘻嘻]</phrase>  
  3. <type>face</type>  
  4. <url>https://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c2/tooth.gif   
  5. </url>  
  6. <is_hot>false</is_hot>  
  7. <is_common>true</is_common>  
  8. <order_number>0</order_number>  
  9. <category></category>  
  10. lt;/emotion>  
  1.      <emotion>  
  2. <phrase>[嘻嘻]</phrase>  
  3. <type>face</type>  
  4. <url>https://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c2/tooth.gif  
  5. </url>  
  6. <is_hot>false</is_hot>  
  7. <is_common>true</is_common>  
  8. <order_number>0</order_number>  
  9. <category></category>  
  10. lt;/emotion>  

 

  3.2 儲存在一個Emotion.java裏。Emotion.java代碼如下:

 

 

Java代碼  
  1. package com.uim.microblog.model;   
  2.   
  3. import java.io.Serializable;   
  4.   
  5. public class Emotions implements Serializable {   
  6.   
  7.     /**  
  8.      *   
  9.      */  
  10.     private static final long serialVersionUID = 1L;   
  11.        
  12.     private String phrase;//表情使用的替代文字   
  13.     private String type;   
  14.     private String url;//表情圖片存放的位置   
  15.     private String isHot;//是否為熱門表情   
  16.     private String isCommon;//是否屬於通用   
  17.     private String orderNumber;//該表情在係統中的排序號碼   
  18.     private String category;//表情分類   
  19.     private String imageName;//表情名稱   
  20.        
  21.     public String getImageName() {   
  22.         return imageName;   
  23.     }   
  24.     public void setImageName(String imageName) {   
  25.         this.imageName = imageName;   
  26.     }   
  27.     public String getPhrase() {   
  28.         return phrase;   
  29.     }   
  30.     public void setPhrase(String phrase) {   
  31.         this.phrase = phrase;   
  32.     }   
  33.     public String getType() {   
  34.         return type;   
  35.     }   
  36.     public void setType(String type) {   
  37.         this.type = type;   
  38.     }   
  39.     public String getUrl() {   
  40.         return url;   
  41.     }   
  42.     public void setUrl(String url) {   
  43.         this.url = url;   
  44.     }   
  45.     public String getIsHot() {   
  46.         return isHot;   
  47.     }   
  48.     public void setIsHot(String isHot) {   
  49.         this.isHot = isHot;   
  50.     }   
  51.     public String getIsCommon() {   
  52.         return isCommon;   
  53.     }   
  54.     public void setIsCommon(String isCommon) {   
  55.         this.isCommon = isCommon;   
  56.     }   
  57.     public String getOrderNumber() {   
  58.         return orderNumber;   
  59.     }   
  60.     public void setOrderNumber(String orderNumber) {   
  61.         this.orderNumber = orderNumber;   
  62.     }   
  63.     public String getCategory() {   
  64.         return category;   
  65.     }   
  66.     public void setCategory(String category) {   
  67.         this.category = category;   
  68.     }   
  69.        
  70.        
  71.        
  72.        
  73. }  
  1. package com.uim.microblog.model;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Emotions implements Serializable {  
  6.   
  7.     /** 
  8.      *  
  9.      */  
  10.     private static final long serialVersionUID = 1L;  
  11.       
  12.     private String phrase;//表情使用的替代文字  
  13.     private String type;  
  14.     private String url;//表情圖片存放的位置  
  15.     private String isHot;//是否為熱門表情  
  16.     private String isCommon;//是否屬於通用  
  17.     private String orderNumber;//該表情在係統中的排序號碼  
  18.     private String category;//表情分類  
  19.     private String imageName;//表情名稱  
  20.       
  21.     public String getImageName() {  
  22.         return imageName;  
  23.     }  
  24.     public void setImageName(String imageName) {  
  25.         this.imageName = imageName;  
  26.     }  
  27.     public String getPhrase() {  
  28.         return phrase;  
  29.     }  
  30.     public void setPhrase(String phrase) {  
  31.         this.phrase = phrase;  
  32.     }  
  33.     public String getType() {  
  34.         return type;  
  35.     }  
  36.     public void setType(String type) {  
  37.         this.type = type;  
  38.     }  
  39.     public String getUrl() {  
  40.         return url;  
  41.     }  
  42.     public void setUrl(String url) {  
  43.         this.url = url;  
  44.     }  
  45.     public String getIsHot() {  
  46.         return isHot;  
  47.     }  
  48.     public void setIsHot(String isHot) {  
  49.         this.isHot = isHot;  
  50.     }  
  51.     public String getIsCommon() {  
  52.         return isCommon;  
  53.     }  
  54.     public void setIsCommon(String isCommon) {  
  55.         this.isCommon = isCommon;  
  56.     }  
  57.     public String getOrderNumber() {  
  58.         return orderNumber;  
  59.     }  
  60.     public void setOrderNumber(String orderNumber) {  
  61.         this.orderNumber = orderNumber;  
  62.     }  
  63.     public String getCategory() {  
  64.         return category;  
  65.     }  
  66.     public void setCategory(String category) {  
  67.         this.category = category;  
  68.     }  
  69.       
  70.       
  71.       
  72.       
  73. }  
 

 

  3.3xm sax解析l的handler如下:

 

Java代碼  
  1. package com.uim.microblog.net.handler;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import org.xml.sax.Attributes;   
  7. import org.xml.sax.SAXException;   
  8. import org.xml.sax.helpers.DefaultHandler;   
  9.   
  10. import com.uim.microblog.model.Emotions;   
  11. import com.uim.microblog.model.ResponseResult;   
  12.   
  13. public class BlogEmotionsHandler extends DefaultHandler {   
  14.        
  15.     private List<Emotions> list;   
  16.        
  17.     private Emotions emotions;   
  18.     private ResponseResult responseresult;   
  19.        
  20.     private String tag = null;//正在解析的元素   
  21.        
  22.        
  23.     public List<Emotions> getEmotionsList(){   
  24.         return list;   
  25.     }   
  26.   
  27.     @Override  
  28.     public void characters(char[] ch, int start, int length)   
  29.             throws SAXException {   
  30.         if (tag != null) {   
  31.             String textArea = new String(ch,start,length);   
  32.                
  33.             /**開始解析表情數據*/  
  34.             if ("phrase".equals(tag)) {   
  35.                 emotions.setPhrase(textArea);   
  36.             } else if ("type".equals(tag)) {   
  37.                 emotions.setType(textArea);   
  38.             } else if ("url".equals(tag)) {   
  39.                 try {   
  40.                     emotions.setUrl(textArea);   
  41.                     String imageName = textArea.substring(textArea.lastIndexOf("/") + 1,textArea.length() - 4);   
  42.                     emotions.setImageName(imageName);   
  43.                 } catch (Exception e) {   
  44.                     e.printStackTrace();   
  45.                 }   
  46.             } else if ("is_hot".equals(tag)) {   
  47.                 emotions.setIsHot(textArea);   
  48.             } else if ("is_common".equals(tag)) {   
  49.                 emotions.setIsCommon(textArea);   
  50.             } else if ("order_number".equals(tag)) {   
  51.                 emotions.setOrderNumber(textArea);   
  52.             } else if ("category".equals(tag)) {   
  53.                 emotions.setCategory(textArea);   
  54.             } else if ("retn".equals(tag)) {   
  55.                 responseresult.setRetn(textArea);   
  56.             } else if ("desc".equals(tag)) {   
  57.                 responseresult.setDesc(textArea);   
  58.             }   
  59.         }   
  60.            
  61.     }   
  62.   
  63.     @Override  
  64.     public void endDocument() throws SAXException {   
  65.         super.endDocument();   
  66.     }   
  67.   
  68.     @Override  
  69.     public void endElement(String uri, String localName, String qName)   
  70.             throws SAXException {   
  71.         tag = null;   
  72.         if ("mb".equals(localName)) {   
  73.                
  74.         } else if ("emotions".equals(localName)) {   
  75.             responseresult =null;   
  76.         } else if ("emotion".equals(localName)) {   
  77.             list.add(emotions);   
  78.                
  79.             emotions = null;   
  80.         }    
  81.     }   
  82.   
  83.     @Override  
  84.     public void startDocument() throws SAXException {   
  85.         list = new ArrayList<Emotions>();   
  86.     }   
  87.   
  88.     @Override  
  89.     public void startElement(String uri, String localName, String qName,   
  90.             Attributes attributes) throws SAXException {   
  91.         if ("mb".equals(localName)) {   
  92.             responseresult = new ResponseResult();   
  93.         } else if ("emotions".equals(localName)) {   
  94.                
  95.         } else if ("emotion".equals(localName)) {   
  96.             emotions = new Emotions();   
  97.         }    
  98.            
  99.         tag = localName;   
  100.     }   
  101.        
  102.        
  103.   
  104. }  
  1. package com.uim.microblog.net.handler;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.xml.sax.Attributes;  
  7. import org.xml.sax.SAXException;  
  8. import org.xml.sax.helpers.DefaultHandler;  
  9.   
  10. import com.uim.microblog.model.Emotions;  
  11. import com.uim.microblog.model.ResponseResult;  
  12.   
  13. public class BlogEmotionsHandler extends DefaultHandler {  
  14.       
  15.     private List<Emotions> list;  
  16.       
  17.     private Emotions emotions;  
  18.     private ResponseResult responseresult;  
  19.       
  20.     private String tag = null;//正在解析的元素  
  21.       
  22.       
  23.     public List<Emotions> getEmotionsList(){  
  24.         return list;  
  25.     }  
  26.   
  27.     @Override  
  28.     public void characters(char[] ch, int start, int length)  
  29.             throws SAXException {  
  30.         if (tag != null) {  
  31.             String textArea = new String(ch,start,length);  
  32.               
  33.             /**開始解析表情數據*/  
  34.             if ("phrase".equals(tag)) {  
  35.                 emotions.setPhrase(textArea);  
  36.             } else if ("type".equals(tag)) {  
  37.                 emotions.setType(textArea);  
  38.             } else if ("url".equals(tag)) {  
  39.                 try {  
  40.                     emotions.setUrl(textArea);  
  41.                     String imageName = textArea.substring(textArea.lastIndexOf("/") + 1,textArea.length() - 4);  
  42.                     emotions.setImageName(imageName);  
  43.                 } catch (Exception e) {  
  44.                     e.printStackTrace();  
  45.                 }  
  46.             } else if ("is_hot".equals(tag)) {  
  47.                 emotions.setIsHot(textArea);  
  48.             } else if ("is_common".equals(tag)) {  
  49.                 emotions.setIsCommon(textArea);  
  50.             } else if ("order_number".equals(tag)) {  
  51.                 emotions.setOrderNumber(textArea);  
  52.             } else if ("category".equals(tag)) {  
  53.                 emotions.setCategory(textArea);  
  54.             } else if ("retn".equals(tag)) {  
  55.                 responseresult.setRetn(textArea);  
  56.             } else if ("desc".equals(tag)) {  
  57.                 responseresult.setDesc(textArea);  
  58.             }  
  59.         }  
  60.           
  61.     }  
  62.   
  63.     @Override  
  64.     public void endDocument() throws SAXException {  
  65.         super.endDocument();  
  66.     }  
  67.   
  68.     @Override  
  69.     public void endElement(String uri, String localName, String qName)  
  70.             throws SAXException {  
  71.         tag = null;  
  72.         if ("mb".equals(localName)) {  
  73.               
  74.         } else if ("emotions".equals(localName)) {  
  75.             responseresult =null;  
  76.         } else if ("emotion".equals(localName)) {  
  77.             list.add(emotions);  
  78.               
  79.             emotions = null;  
  80.         }   
  81.     }  
  82.   
  83.     @Override  
  84.     public void startDocument() throws SAXException {  
  85.         list = new ArrayList<Emotions>();  
  86.     }  
  87.   
  88.     @Override  
  89.     public void startElement(String uri, String localName, String qName,  
  90.             Attributes attributes) throws SAXException {  
  91.         if ("mb".equals(localName)) {  
  92.             responseresult = new ResponseResult();  
  93.         } else if ("emotions".equals(localName)) {  
  94.               
  95.         } else if ("emotion".equals(localName)) {  
  96.             emotions = new Emotions();  
  97.         }   
  98.           
  99.         tag = localName;  
  100.     }  
  101.       
  102.       
  103.   
  104. }  

 

 3.4sax解析

 

Java代碼  
  1. public List<Emotions> getEmotion(){   
  2.         BlogGetData getdata = new BlogGetData();   
  3.         String result = getdata.blogEmotionsServlet();   
  4.         try {   
  5.             //生成SAX解析對象   
  6.             parser = SAXParserFactory.newInstance().newSAXParser();   
  7.             //生成xml讀取器   
  8.             reader = parser.getXMLReader();   
  9.             BlogEmotionsHandler handler = new BlogEmotionsHandler();   
  10.             //設置Handler   
  11.             reader.setContentHandler(handler);   
  12.             //指定文件,進行解析   
  13.             reader.parse(new InputSource(new StringReader(result)));   
  14.             //獲取 List<Emotions>   
  15.             emotionList = handler.getEmotionsList();   
  16.         } catch (ParserConfigurationException e) {   
  17.             e.printStackTrace();   
  18.         } catch (SAXException e) {   
  19.             e.printStackTrace();   
  20.         } catch (IOException e) {   
  21.             e.printStackTrace();   
  22.         }   
  23.            
  24.         return emotionList;   
  25.     }  
  1. public List<Emotions> getEmotion(){  
  2.         BlogGetData getdata = new BlogGetData();  
  3.         String result = getdata.blogEmotionsServlet();  
  4.         try {  
  5.             //生成SAX解析對象  
  6.             parser = SAXParserFactory.newInstance().newSAXParser();  
  7.             //生成xml讀取器  
  8.             reader = parser.getXMLReader();  
  9.             BlogEmotionsHandler handler = new BlogEmotionsHandler();  
  10.             //設置Handler  
  11.             reader.setContentHandler(handler);  
  12.             //指定文件,進行解析  
  13.             reader.parse(new InputSource(new StringReader(result)));  
  14.             //獲取 List<Emotions>  
  15.             emotionList = handler.getEmotionsList();  
  16.         } catch (ParserConfigurationException e) {  
  17.             e.printStackTrace();  
  18.         } catch (SAXException e) {  
  19.             e.printStackTrace();  
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.           
  24.         return emotionList;  
  25.     }  

 

 4:

4.1實現表情選擇器---GridView

 

Xml代碼  
  1. <GridView  
  2.             android:id="@+id/blog_sendmsg_gvemotion"  
  3.             android:layout_width="fill_parent"  
  4.             android:layout_height="150sp"  
  5.             android:scrollbars="vertical"  
  6.             android:numColumns="auto_fit"  
  7.             android:verticalSpacing="15dp"  
  8.             android:background="@color/blog_list_back"  
  9.             android:stretchMode="columnWidth"  
  10.             android:gravity="center"  
  11.             android:visibility="gone"  
  12.             android:columnWidth="40dp">  
  13.     </GridView>  
  1. <GridView  
  2.             android:id="@+id/blog_sendmsg_gvemotion"  
  3.             android:layout_width="fill_parent"  
  4.             android:layout_height="150sp"  
  5.             android:scrollbars="vertical"  
  6.             android:numColumns="auto_fit"  
  7.             android:verticalSpacing="15dp"  
  8.             android:background="@color/blog_list_back"  
  9.             android:stretchMode="columnWidth"  
  10.             android:gravity="center"  
  11.             android:visibility="gone"  
  12.             android:columnWidth="40dp">  
  13.     </GridView>  

 

 4.2 GridView的item-----gridview_emotion_item.xml

 

Xml代碼  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="https://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.   <ImageView  
  7.     android:id="@+id/blog_sendmsg_emotion"  
  8.     android:layout_width="wrap_content"  
  9.     android:layout_height="wrap_content"  
  10.     android:layout_weight="50"  
  11.     android:layout_gravity="center">  
  12.   </ImageView>  
  13. </LinearLayout>  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="https://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.   <ImageView  
  7.     android:id="@+id/blog_sendmsg_emotion"  
  8.     android:layout_width="wrap_content"  
  9.     android:layout_height="wrap_content"  
  10.     android:layout_weight="50"  
  11.     android:layout_gravity="center">  
  12.   </ImageView>  
  13. </LinearLayout>  

 

 4.3代碼加載表情圖片到GridView進行顯示

 

Java代碼  
  1. public void addexpression(View view){   
  2.         if (expressionGriView.getVisibility() == View.GONE) {   
  3.             expressionGriView.setVisibility(View.VISIBLE);   
  4.                
  5.             emotionList = BlogHomeActivity.emotions;   
  6.             ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();   
  7.                 
  8.               for(int i=0;i<70;i++)     
  9.               {    
  10.                 emtions = emotionList.get(i);      
  11.                 if (emtions != null) {   
  12.                     HashMap<String, Object> map = new HashMap<String, Object>();   
  13.                    
  14.                     Field f;

    最後更新:2017-04-02 17:09:28

      上一篇:go Android的GridView和Gallery結合Demo
      下一篇:go linux下配置android sdk