当前位置:  编程技术>移动开发

基于Android开发支持表情的实现详解

    来源: 互联网  发布时间:2014-10-15

    本文导语:  最近项目需要支持表情,表情的添加和解析实现基本上是参照Android自身的SmileyParser,具体就不多讲了,直接贴上代码: 代码如下:public class SmileyParser {private static SmileyParser sInstance = null; private Context mContext = null;private Pattern mPatt...

最近项目需要支持表情,表情的添加和解析实现基本上是参照Android自身的SmileyParser,具体就不多讲了,直接贴上代码:

代码如下:

public class SmileyParser {
private static SmileyParser sInstance = null;

private Context mContext = null;
private Pattern mPattern = null;
private HashMap mSmileyTextToId = null;
private final String[] mSmileyArrays =
{"/西瓜","89","/便便","59","/太陽","74","/偷笑","20","/傲慢","23","/再見","39","/凋謝","64","/發呆","3","/發怒","11","/閃電","54","/可愛","21","/豬頭","46","/咖啡","60","/哈欠","104","/鄙視","105","/委屈","106","/快哭了","107","/陰險","108","/親親","109","/嚇","110","/可憐","111","/菜刀","112","/啤酒","113","/籃球","114","/乒乓","115","/示愛","116","/瓢蟲","117","/抱拳","118","/勾引","119","/拳頭","120","/差勁","121","/愛你","122","/NO","123","/OK","124","/轉圈","125","/磕頭","126","/回頭","127","/跳繩","128","/揮手","129","/激動","130","/街舞","131","/獻吻","132","/左太極","133","/右太極","134","/吐","19","/蛋糕","53","/呲牙","13","/咒罵","31","/足球","57","/嘘","33","/困","25","/大兵","29","/大哭","9","/强","76","/奮鬥","30","/擁抱","49","/害羞","6","/尷尬","10","/右哼哼","103","/慪火","86","/勝利","79","/得意","4","/驚訝","14","/心碎","67","/驚恐","26","/微笑","0","/憨笑","28","/抓狂","18","/折磨","35","/發抖","41","/握手","78","/飛吻","85","/鼓掌","99","/撇嘴","1","/敲打","38","/暈","34","/月亮","75","/流汗","27","/流淚","5","/糗大了","100","/愛心","66","/左哼哼","102","/玫瑰","63","/疑問","32","/白眼","22","/睡","8","/冷汗","96","/示愛","65","/弱","77","/跳跳","43","/色","2","/炸彈","55","/壞笑","101","/衰","36","/刀","56","/調皮","12","/摳鼻","98","/酷","16","/禮物","69","/閉嘴","7","/難過","15","/饑餓","24","/飯","61","/骷髏","37","/愛情","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;

}

/**
* 建立String - Id的对应关系
*/
private HashMap buildSmileyRes(){

HashMap smileyTextToId = new HashMap(mSmileyIds.length);
for(int i = 0;i < mSmileyIds.length;++i){
smileyTextToId.put(mSmileyTexts[i], mSmileyIds[i]);
}

return smileyTextToId;
}

/**
* 建立匹配用的正则表达式
* @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());
}

/**
* 把文字转换为图片
* @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;
}
}


实现过程中遇到个小问题:往TextView中添加表情时,当文本既有表情也有文字时,显示是正常的,但是当文本中只有表情时,发现表情显示会偏上,而且上面有一部分被截断。

TextView布局如下:
代码如下:



解决方法:这里的问题应该是TextView在判断行距的时候是根据字体来判断的,但是当文本是表情的时候这个判断有些问题,导致行距过小,所以显示表情的时候就截断了,解决方法是设置一下TextView的最小高度,同时要指定文本向下对齐。另外在创建ImagePan的时候如果指定ImageSpan.ALIGN_BOTTOM对齐方式一般是不会出现这个问题的,但是这种方式下表情显示会偏下。

修改后TextView布局如下:
代码如下:



    
 
 

您可能感兴趣的文章:

  • 深入android Unable to resolve target 'android-XX'详解
  • Android工程:引用另一个Android工程的方法详解
  • Android TextView设置背景色与边框的方法详解
  • Android中的android:layout_weight使用详解
  • Android 实现永久保存数据的方法详解
  • 在android开发中尽量不要使用中文路径的问题详解
  • android开发环境搭建详解(eclipse + android sdk)
  • android双缓冲技术实例详解
  • 深入Android开发FAQ的详解
  • Android开发笔记之:一分钟学会使用Logcat调试程序的详解
  • Android对sdcard扩展卡文件操作实例详解
  • Android笔记之:onConfigurationChanged详解
  • Android 动画之AlphaAnimation应用详解
  • 解析后台进程对Android性能影响的详解
  • android ListView 一些重要属性详解
  • 解决Fedora14下eclipse进行android开发,ibus提示没有输入窗口的方法详解
  • Windows下获取Android 源码方法的详解
  • Android selector背景选择器的使用详解
  • Android 动画之RotateAnimation应用详解
  • Handler与Android多线程详解
  • Android开发技巧之像QQ一样输入文字和表情图像
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Android 按后退键退出Android程序的实现方法
  • android panellistview 圆角实现代码
  • Android实现弹出键盘的方法
  • Android实现信号强度监听的方法
  • Android设置桌面背景图片的实现方法
  • 请教如何在android平台下实现每个进程的流量监控?
  • Android平台的QT实现 Necessitas
  • Android实现关机重启的方法分享
  • android activity设置无标题实现全屏
  • android实现关闭或开启移动网络数据
  • android4.0屏蔽HOME键的简单实现
  • Android手机获取root权限并实现关机重启功能的方法
  • android开发教程之实现toast工具类
  • Android判断屏幕是横屏或是竖屏的简单实现方法
  • Android中button实现onclicklistener事件的两种方式
  • Android实现动态显示或隐藏密码输入框的内容
  • Android实现Flip翻转动画效果
  • android实现背景平铺的三种方法
  • Android创建文件实现对文件监听示例
  • Android实现弹出登陆框的方案
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • Android瀑布流实例 android_waterfall
  • Android开发需要的几点注意事项总结
  • Android系统自带样式 (android:theme)
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android网络共享软件 Android Wifi Tether
  • Android访问与手机通讯相关类的介绍
  • Android 图标库 Android GraphView
  • Android及andriod无线网络Wifi开发的几点注意事项
  • 轻量级Android开发工具 Android Tools
  • Android 2.3 下StrictMode介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3