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

2016-02-19 10:01 9 1 收藏

图老师小编精心整理的基于Android开发支持表情的实现详解希望大家喜欢,觉得好的亲们记得收藏起来哦!您的支持就是小编更新的动力~

【 tulaoshi.com - 编程语言 】

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

public class SmileyParser {
private static SmileyParser sInstance = null;

private Context mContext = null;
private Pattern mPattern = null;
private HashMapString, Integer 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 HashMapString, Integer buildSmileyRes(){

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

HashMapString, Integer smileyTextToId = new HashMapString, Integer(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

android:id="@+id/comment_item_content"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

android:layout_marginTop="10dp"

android:layout_marginBottom="10dp"

android:textSize="16sp"

android:textColor="#333333"

/

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

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

TextView

android:id="@+id/comment_item_content"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

android:layout_marginTop="10dp"

android:layout_marginBottom="10dp"

android:textSize="16sp"

android:textColor="#333333"

android:minHeight="25dp"

android:gravity="bottom"

/

来源:http://www.tulaoshi.com/n/20160219/1593057.html

延伸阅读
文件存储: 代码如下: public class MainActivity extends Activity { EditText mname, mage; TextView mtv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mname = (EditText) findViewById(R.id.editText1); mage = (EditText) f...
AppWidgetProvider 用来在HOME页面显示插件 实现步骤: 1、为AppWidget提供一个元布局文件AppWigdetProvider_Provider.xml,用来显示Widget的界面。 2、创建一个类继承自AppWidgetProvider,并覆写里面的相关的方法。 3、为WidgetProvider创建一个引用的布局文件,或者直接用main.xml。 4、在程序中注册Manifest.xml。 代码如下: 1、在res/x...
提到串口编程,就不得不提到JNI,不得不提到JavaAPI中的文件描述符类:FileDescriptor。下面我分别对JNI、FileDescriptor以及串口的一些知识点和实现的源码进行分析说明。这里主要是参考了开源项目android-serialport-api。 串口编程需要了解的基本知识点:对于串口编程,我们只需对串口进行一系列的设置,然后打开串口,这些操作我们可以参考...
在android中,LayoutInflater有点类似于Activity的findViewById(id),不同的是LayoutInflater是用来找layout下的xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。 下面通过一个例子进行详细说明: 1、在res/layout文件夹下,添加一个xml文件dialog.xml 代码如下: LinearLayout xmlns:an...
在Android平台中,集成了一个嵌入式关系型数据库-- SQLite ,它支持NULL、INTEGER、REAL(浮点数字)、TEXT(字符串文本)和BLOB(二进制对象)数据类型,虽然只支持五种数据类型,实际上可以接受varchar(n),char(n),decimal(p,s)等数据类型,在进行运算或保存的时候会转换成对应的五种数据类型。 ex: 可以在Integer类型的字段中存放字符串,或者在布...

经验教程

433

收藏

57
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部