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工程進行下載)
- public void getFriendList() throws Exception {
- BlogReleaseServiceImpl service = new BlogReleaseServiceImpl();
- List<Emotions> list = service.getEmotion();
- for (Emotions emotions : list) {
- String path = emotions.getUrl();
- String filename = path.substring(path.lastIndexOf("/") + 1,path.length());
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setReadTimeout(5 * 1000);
- if(conn.getResponseCode() == 200){
- InputStream is = conn.getInputStream();
- byte[] data = readStream(is);
- File file = new File("f: \\sina_images\\" + filename);
- FileOutputStream fs = new FileOutputStream(file);
- fs.write(data);
- fs.close();
- }else{
- System.out.println("請求失敗");
- }
- }
- }
- public byte[] readStream(InputStream is) throws Exception {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- byte[] buffer = new byte[2048];
- int len = 0;
- while((len = is.read(buffer)) != -1){
- os.write(buffer,0,len);
- }
- is.close();
- return os.toByteArray();
- }
- public void getFriendList() throws Exception {
- BlogReleaseServiceImpl service = new BlogReleaseServiceImpl();
- List<Emotions> list = service.getEmotion();
- for (Emotions emotions : list) {
- String path = emotions.getUrl();
- String filename = path.substring(path.lastIndexOf("/") + 1,path.length());
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setReadTimeout(5 * 1000);
- if(conn.getResponseCode() == 200){
- InputStream is = conn.getInputStream();
- byte[] data = readStream(is);
- File file = new File("f: \\sina_images\\" + filename);
- FileOutputStream fs = new FileOutputStream(file);
- fs.write(data);
- fs.close();
- }else{
- System.out.println("請求失敗");
- }
- }
- }
- public byte[] readStream(InputStream is) throws Exception {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- byte[] buffer = new byte[2048];
- int len = 0;
- while((len = is.read(buffer)) != -1){
- os.write(buffer,0,len);
- }
- is.close();
- return os.toByteArray();
- }
2:把本地的表情都放進android的資源文件裏----drawable下麵(這個就不用多說了,直接選取所有文件複製就行了)
3:
3.1訪問新浪的表情接口,把返回的信息如下:
- <emotion>
- <phrase>[嘻嘻]</phrase>
- <type>face</type>
- <url>https://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c2/tooth.gif
- </url>
- <is_hot>false</is_hot>
- <is_common>true</is_common>
- <order_number>0</order_number>
- <category></category>
- lt;/emotion>
- <emotion>
- <phrase>[嘻嘻]</phrase>
- <type>face</type>
- <url>https://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c2/tooth.gif
- </url>
- <is_hot>false</is_hot>
- <is_common>true</is_common>
- <order_number>0</order_number>
- <category></category>
- lt;/emotion>
3.2 儲存在一個Emotion.java裏。Emotion.java代碼如下:
- package com.uim.microblog.model;
- import java.io.Serializable;
- public class Emotions implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String phrase;//表情使用的替代文字
- private String type;
- private String url;//表情圖片存放的位置
- private String isHot;//是否為熱門表情
- private String isCommon;//是否屬於通用
- private String orderNumber;//該表情在係統中的排序號碼
- private String category;//表情分類
- private String imageName;//表情名稱
- public String getImageName() {
- return imageName;
- }
- public void setImageName(String imageName) {
- this.imageName = imageName;
- }
- public String getPhrase() {
- return phrase;
- }
- public void setPhrase(String phrase) {
- this.phrase = phrase;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getIsHot() {
- return isHot;
- }
- public void setIsHot(String isHot) {
- this.isHot = isHot;
- }
- public String getIsCommon() {
- return isCommon;
- }
- public void setIsCommon(String isCommon) {
- this.isCommon = isCommon;
- }
- public String getOrderNumber() {
- return orderNumber;
- }
- public void setOrderNumber(String orderNumber) {
- this.orderNumber = orderNumber;
- }
- public String getCategory() {
- return category;
- }
- public void setCategory(String category) {
- this.category = category;
- }
- }
- package com.uim.microblog.model;
- import java.io.Serializable;
- public class Emotions implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String phrase;//表情使用的替代文字
- private String type;
- private String url;//表情圖片存放的位置
- private String isHot;//是否為熱門表情
- private String isCommon;//是否屬於通用
- private String orderNumber;//該表情在係統中的排序號碼
- private String category;//表情分類
- private String imageName;//表情名稱
- public String getImageName() {
- return imageName;
- }
- public void setImageName(String imageName) {
- this.imageName = imageName;
- }
- public String getPhrase() {
- return phrase;
- }
- public void setPhrase(String phrase) {
- this.phrase = phrase;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getIsHot() {
- return isHot;
- }
- public void setIsHot(String isHot) {
- this.isHot = isHot;
- }
- public String getIsCommon() {
- return isCommon;
- }
- public void setIsCommon(String isCommon) {
- this.isCommon = isCommon;
- }
- public String getOrderNumber() {
- return orderNumber;
- }
- public void setOrderNumber(String orderNumber) {
- this.orderNumber = orderNumber;
- }
- public String getCategory() {
- return category;
- }
- public void setCategory(String category) {
- this.category = category;
- }
- }
3.3xm sax解析l的handler如下:
- package com.uim.microblog.net.handler;
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import com.uim.microblog.model.Emotions;
- import com.uim.microblog.model.ResponseResult;
- public class BlogEmotionsHandler extends DefaultHandler {
- private List<Emotions> list;
- private Emotions emotions;
- private ResponseResult responseresult;
- private String tag = null;//正在解析的元素
- public List<Emotions> getEmotionsList(){
- return list;
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if (tag != null) {
- String textArea = new String(ch,start,length);
- /**開始解析表情數據*/
- if ("phrase".equals(tag)) {
- emotions.setPhrase(textArea);
- } else if ("type".equals(tag)) {
- emotions.setType(textArea);
- } else if ("url".equals(tag)) {
- try {
- emotions.setUrl(textArea);
- String imageName = textArea.substring(textArea.lastIndexOf("/") + 1,textArea.length() - 4);
- emotions.setImageName(imageName);
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else if ("is_hot".equals(tag)) {
- emotions.setIsHot(textArea);
- } else if ("is_common".equals(tag)) {
- emotions.setIsCommon(textArea);
- } else if ("order_number".equals(tag)) {
- emotions.setOrderNumber(textArea);
- } else if ("category".equals(tag)) {
- emotions.setCategory(textArea);
- } else if ("retn".equals(tag)) {
- responseresult.setRetn(textArea);
- } else if ("desc".equals(tag)) {
- responseresult.setDesc(textArea);
- }
- }
- }
- @Override
- public void endDocument() throws SAXException {
- super.endDocument();
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- tag = null;
- if ("mb".equals(localName)) {
- } else if ("emotions".equals(localName)) {
- responseresult =null;
- } else if ("emotion".equals(localName)) {
- list.add(emotions);
- emotions = null;
- }
- }
- @Override
- public void startDocument() throws SAXException {
- list = new ArrayList<Emotions>();
- }
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if ("mb".equals(localName)) {
- responseresult = new ResponseResult();
- } else if ("emotions".equals(localName)) {
- } else if ("emotion".equals(localName)) {
- emotions = new Emotions();
- }
- tag = localName;
- }
- }
- package com.uim.microblog.net.handler;
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import com.uim.microblog.model.Emotions;
- import com.uim.microblog.model.ResponseResult;
- public class BlogEmotionsHandler extends DefaultHandler {
- private List<Emotions> list;
- private Emotions emotions;
- private ResponseResult responseresult;
- private String tag = null;//正在解析的元素
- public List<Emotions> getEmotionsList(){
- return list;
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if (tag != null) {
- String textArea = new String(ch,start,length);
- /**開始解析表情數據*/
- if ("phrase".equals(tag)) {
- emotions.setPhrase(textArea);
- } else if ("type".equals(tag)) {
- emotions.setType(textArea);
- } else if ("url".equals(tag)) {
- try {
- emotions.setUrl(textArea);
- String imageName = textArea.substring(textArea.lastIndexOf("/") + 1,textArea.length() - 4);
- emotions.setImageName(imageName);
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else if ("is_hot".equals(tag)) {
- emotions.setIsHot(textArea);
- } else if ("is_common".equals(tag)) {
- emotions.setIsCommon(textArea);
- } else if ("order_number".equals(tag)) {
- emotions.setOrderNumber(textArea);
- } else if ("category".equals(tag)) {
- emotions.setCategory(textArea);
- } else if ("retn".equals(tag)) {
- responseresult.setRetn(textArea);
- } else if ("desc".equals(tag)) {
- responseresult.setDesc(textArea);
- }
- }
- }
- @Override
- public void endDocument() throws SAXException {
- super.endDocument();
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- tag = null;
- if ("mb".equals(localName)) {
- } else if ("emotions".equals(localName)) {
- responseresult =null;
- } else if ("emotion".equals(localName)) {
- list.add(emotions);
- emotions = null;
- }
- }
- @Override
- public void startDocument() throws SAXException {
- list = new ArrayList<Emotions>();
- }
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if ("mb".equals(localName)) {
- responseresult = new ResponseResult();
- } else if ("emotions".equals(localName)) {
- } else if ("emotion".equals(localName)) {
- emotions = new Emotions();
- }
- tag = localName;
- }
- }
3.4sax解析
- public List<Emotions> getEmotion(){
- BlogGetData getdata = new BlogGetData();
- String result = getdata.blogEmotionsServlet();
- try {
- //生成SAX解析對象
- parser = SAXParserFactory.newInstance().newSAXParser();
- //生成xml讀取器
- reader = parser.getXMLReader();
- BlogEmotionsHandler handler = new BlogEmotionsHandler();
- //設置Handler
- reader.setContentHandler(handler);
- //指定文件,進行解析
- reader.parse(new InputSource(new StringReader(result)));
- //獲取 List<Emotions>
- emotionList = handler.getEmotionsList();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return emotionList;
- }
- public List<Emotions> getEmotion(){
- BlogGetData getdata = new BlogGetData();
- String result = getdata.blogEmotionsServlet();
- try {
- //生成SAX解析對象
- parser = SAXParserFactory.newInstance().newSAXParser();
- //生成xml讀取器
- reader = parser.getXMLReader();
- BlogEmotionsHandler handler = new BlogEmotionsHandler();
- //設置Handler
- reader.setContentHandler(handler);
- //指定文件,進行解析
- reader.parse(new InputSource(new StringReader(result)));
- //獲取 List<Emotions>
- emotionList = handler.getEmotionsList();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return emotionList;
- }
4:
4.1實現表情選擇器---GridView
- <GridView
- android:id="@+id/blog_sendmsg_gvemotion"
- android:layout_width="fill_parent"
- android:layout_height="150sp"
- android:scrollbars="vertical"
- android:numColumns="auto_fit"
- android:verticalSpacing="15dp"
- android:background="@color/blog_list_back"
- android:stretchMode="columnWidth"
- android:gravity="center"
- android:visibility="gone"
- android:columnWidth="40dp">
- </GridView>
- <GridView
- android:id="@+id/blog_sendmsg_gvemotion"
- android:layout_width="fill_parent"
- android:layout_height="150sp"
- android:scrollbars="vertical"
- android:numColumns="auto_fit"
- android:verticalSpacing="15dp"
- android:background="@color/blog_list_back"
- android:stretchMode="columnWidth"
- android:gravity="center"
- android:visibility="gone"
- android:columnWidth="40dp">
- </GridView>
4.2 GridView的item-----gridview_emotion_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:id="@+id/blog_sendmsg_emotion"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="50"
- android:layout_gravity="center">
- </ImageView>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:id="@+id/blog_sendmsg_emotion"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="50"
- android:layout_gravity="center">
- </ImageView>
- </LinearLayout>
4.3代碼加載表情圖片到GridView進行顯示
- public void addexpression(View view){
- if (expressionGriView.getVisibility() == View.GONE) {
- expressionGriView.setVisibility(View.VISIBLE);
- emotionList = BlogHomeActivity.emotions;
- ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
- for(int i=0;i<70;i++)
- {
- emtions = emotionList.get(i);
- if (emtions != null) {
- HashMap<String, Object> map = new HashMap<String, Object>();
-
Field f;
最後更新:2017-04-02 17:09:28
上一篇:
Android的GridView和Gallery結合Demo
下一篇:
linux下配置android sdk