文章标题

最近在整理项目中Emoji表情的问题,在此整理下代码:

1、EmoticonsView.java

public class EmoticonsView extends LinearLayout implements OnPageChangeListener{
private final String TAG = “EmotionsView”;
private Context mContext;

private LinearLayout mParentView;// 存放表情控件的父view
private EditText mInputEditText;// 输出表情显示的EditText
private ViewPager mPager;// 存放多页表情
private LinearLayout mPageSelectPointLayout;// 选中的pager的小圆点
private ImageView[] mPointViews;
private ArrayList<Fragment> mFragmentList = new ArrayList<Fragment>();
private FragmentManager mFragmentManager;
private int width;
//是否定义最大长度
private boolean maxLangthFlag = false;
//最大长度
private int maxLangth;

/**
 * @param context
 * @param parentView      存放表情控件的父view
 * @param editText        输出表情显示的EditText
 * @param fragmentManager
 */
public EmoticonsView(Context context, LinearLayout parentView,
                     EditText editText, FragmentManager fragmentManager) {
    super(context);
    Log.v(TAG, "----------------- EmotionsView ================");
    mContext = context;
    mParentView = parentView;
    mInputEditText = editText;
    mFragmentManager = fragmentManager;
    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    width = wm.getDefaultDisplay().getWidth();
    init(context);
}

/**
 * @param context
 * @param parentView
 * @param editText
 * @param fragmentManager
 * @param maxLangthFlag   是否定义最大长度
 * @param maxLangth       最大长度是多少
 */
public EmoticonsView(Context context, LinearLayout parentView,
                     EditText editText, FragmentManager fragmentManager, boolean maxLangthFlag, int maxLangth) {
    super(context);
    Log.v(TAG, "----------------- EmotionsView ================");
    mContext = context;
    mParentView = parentView;
    mInputEditText = editText;
    mFragmentManager = fragmentManager;
    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    width = wm.getDefaultDisplay().getWidth();
    this.maxLangthFlag = maxLangthFlag;
    this.maxLangth = maxLangth;
    init(context);
}

/**
 * 表情view的初始化   
 *  @param context
 */
private void init(Context context) {
    Log.v(TAG, "----------------- init ================");
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rootView = inflater.inflate(R.layout.emotions_and_layout, null);
    mPager = (ViewPager) rootView.findViewById(R.id.vPager_2);
    mPageSelectPointLayout = (LinearLayout) rootView
            .findViewById(R.id.page_select_point_layout);
    rootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    initEmoticonsPager();
    addView(rootView);
}

/**
 * 初始化表情pager
 */
private void initEmoticonsPager() {
    int m = EmoticonsConfig.TOTAL_EMOTICONS_NUMBER
            % EmoticonsConfig.ONE_PAGE_EMOTICONS_NUMBER;
    int n = EmoticonsConfig.TOTAL_EMOTICONS_NUMBER
            / EmoticonsConfig.ONE_PAGE_EMOTICONS_NUMBER;
    int pagerNumber = m == 0 ? n : n + 1;
    for (int i = 0; i < pagerNumber; i++) {
        EmoticonsFragment emoticonsFragment = new EmoticonsFragment(i, mInputEditText, maxLangthFlag, maxLangth);
        mFragmentList.add(emoticonsFragment);
    }

    // 给ViewPager设置适配器
    if (mFragmentList.size() > 0) {
        mPager.setAdapter(new MyFragmentPagerAdapter(mFragmentManager,
                mFragmentList));
        mPager.setCurrentItem(0);// 设置当前显示标签页为第一页
        mPager.setOnPageChangeListener(this);
        addPagePoint();// 添加viewpager选中显示提示
        setSelectPagePoint(0);
    }
}

/**
 * 设置viewpager选中的第几页
 *
 * @param index 选中的页数
 */
private void setShowPageIndex(int index) {
    mPager.setCurrentItem(index);
}

/**
 * 添加pager的小圆点
 */
private void addPagePoint() {
    int count = mFragmentList.size();
    if (count > 0) {
        int imgWidth = width / count;
        mPointViews = new ImageView[count];
        for (int i = 0; i < count; i++) {
            ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new LayoutParams(
                    imgWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
            imageView.setBackgroundResource(R.drawable.bg_news_voice);
            mPointViews[i] = imageView;
            mPageSelectPointLayout.addView(imageView);
        }
    }
}

private void setSelectPagePoint(int index) {
    for (int i = 0; i < mPointViews.length; i++) {
        if (i == index)
            mPointViews[i].setBackgroundResource(R.drawable.point_normal);
        else {
            mPointViews[i].setBackground(null);
        }
    }
}

/**
 * 字符转化成图片
 */
public CharSequence formatMessage(Context context, String contact,
                                  String body, String contentType) {

    return EmoticonsUtil.formatMessage(context, body,
            contentType);
}

/**
* 字符转化成图片,可设置表情的宽高
* w
* h
*/
public CharSequence formatMessage(Context context, String body,
String contentType, int w, int h) {

    return EmoticonsUtil.formatMessage(context, body, contentType, w, h);
}

public CharSequence formatMessage(Context context, String body,
                                  String contentType) {
    return EmoticonsUtil.formatMessage(context, body, contentType);
}

public CharSequence formatMessage(EditText editText) {
    Editable editable = editText.getText();
    return editable;

}

/**
 * 隐藏软键盘
 *
 * @param mContext
 */
public void hideInputMethod(Context mContext) {
    EmoticonsUtil.hideInputMethod(mContext);

}

/**
 * 显示表情控件
 */
public void showEmotionsView() {
    // Utils.hideInputMethod((Activity) mContext);
    hideInputMethod(mContext);
    mParentView.setVisibility(View.VISIBLE);
    setShowPageIndex(0);
}

/**
 * 隐藏表情控件
 */
public void hideEmotionsView() {
    mParentView.setVisibility(View.GONE);
}

public boolean isShow() {
    return mParentView.getVisibility() == View.VISIBLE;
}

@Override
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageSelected(int arg0) {
    setSelectPagePoint(arg0);
}

class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> list;
    private int size;

    public MyFragmentPagerAdapter(FragmentManager fm,
                                  ArrayList<Fragment> list) {
        super(fm);
        this.list = list;
    }

    public MyFragmentPagerAdapter(FragmentManager fm, integer a, string sd) {
        super(fm);
        this.list = list;
    }

    @Override
    public int getCount() {
        size = list.size();
        return size;
    }

    @Override
    public Fragment getItem(int arg0) {
        return list.get(arg0);
    }
}

}

2、EmoticonsFragment.java

/**
* 表情显示fragment
*/
@SuppressLint(“ValidFragment”)
public class EmoticonsFragment extends Fragment {
private int mPageIndex = 0;// 当前是第几页

private EditText mInputEditText;
private ArrayList<EmoticonsModel> mEmoticonsData = new ArrayList<EmoticonsModel>();
private String tempDelteStr;
//是否定义最大长度
private boolean maxLangthFlag = false;
//最大长度
private int maxLangth;

public EmoticonsFragment() {
}

/**
 * @param pageIndex
 * @param editText
 * @param maxLangthFlag 是否有字数限制
 * @param maxLangth     字数限制多少
 */
public EmoticonsFragment(int pageIndex, EditText editText, boolean maxLangthFlag, int maxLangth) {
    mPageIndex = pageIndex;
    mInputEditText = editText;
    this.maxLangthFlag = maxLangthFlag;
    this.maxLangth = maxLangth;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.v(EmoticonsConfig.TAG, "========== onCreateView ===========");
    mEmoticonsData = new ArrayList<EmoticonsModel>();
    View view = inflater.inflate(R.layout.emoticons_fragment, container,
            false);
    GridView mGridView = (GridView) view.findViewById(R.id.gv_one);

    // 根据传进来 的页数选择需要 加载 的表情
    addEmoticons();

    EmotionsAdapter adapter = new EmotionsAdapter(this.getActivity());
    mGridView.setAdapter(adapter);
    mGridView.setNumColumns(EmoticonsConfig.ONE_LINE_EMOTICONS_NUMBER);
    mGridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            TextView emoticonsIdView = (TextView) arg1
                    .findViewById(R.id.imoticons_id);
            TextView emoticonsTypeView = (TextView) arg1
                    .findViewById(R.id.imoticons_type);
            TextView emoticonsNameView = (TextView) arg1
                    .findViewById(R.id.imoticons_ename_2_2);

            int type = (Integer) emoticonsTypeView.getTag();
            int id = (Integer) emoticonsIdView.getTag();
            String name = (String) emoticonsNameView.getTag();

            // 如果是普通表情则响应表情选中,否则 响应删除
            if (type == EmoticonsModel.TYPE_EMOTICONS_NORMAL) {
                selectedEmoticons(id, name);
            } else {
                deleteEmoticons();
            }

        }
    });
    return view;
}

// 点击删除图片时,删除editText里面的表情
private void deleteEmoticons() {
    int indexCursor = mInputEditText.getSelectionStart();
    Editable editable = mInputEditText.getText();
    if (indexCursor == 0 || indexCursor < 0) {
        return;
    }
    if (indexCursor >= 6) {
        tempDelteStr = mInputEditText.getText().toString()
                .substring(indexCursor - 6, indexCursor);
        SpannableString spannableString = new SpannableString(tempDelteStr);
        Pattern sinaPatten = Pattern.compile(
                EmoticonsConfig.REGULAR_EXPRESSION,
                Pattern.CASE_INSENSITIVE);
        Matcher matcher = sinaPatten.matcher(spannableString);
        if (matcher.find()) {
            if (matcher.group() != null) {
                editable.delete(indexCursor - 6, indexCursor);
            }
        } else {
            editable.delete(indexCursor - 1, indexCursor);
        }
    } else {
        editable.delete(indexCursor - 1, indexCursor);
    }

}

// 是普通表情,则选中
private void selectedEmoticons(int id, String name) {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            EmoticonsConfig.emoticonsImageIds[id]);
    ImageSpan imageSpan = new ImageSpan(getActivity(), bitmap);
    String imageName = name;
    SpannableString spannableString = new SpannableString(imageName);
    spannableString.setSpan(imageSpan, 0, 6,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    int index = mInputEditText.getSelectionStart();
    int textH = mInputEditText.getText().length();

    if (maxLangthFlag) {
        if (maxLangth - textH < 6) {
            Log.d("EmoticonsFragment ", "EmoticonsFragment -- textH大于editText长度");
        } else {
            mInputEditText.getText().insert(index, spannableString);
        }
    } else {
        mInputEditText.getText().insert(index, spannableString);
    }


}

// 添加表情
private void addEmoticons() {

    for (int i = mPageIndex * EmoticonsConfig.ONE_PAGE_EMOTICONS_NUMBER; i < (mPageIndex + 1)
            * EmoticonsConfig.ONE_PAGE_EMOTICONS_NUMBER; i++) {
        if (i >= EmoticonsConfig.TOTAL_EMOTICONS_NUMBER) {
            break;
        }

        try {
            // 确定表情文件的名称
            String imageName = "";
            if (i < 9) {
                imageName = "f00" + (i + 1);
            } else if (i < 99) {
                imageName = "f0" + (i + 1);
            } else {
                imageName = "f" + (i + 1);
            }

            Field field = R.drawable.class.getDeclaredField(imageName);
            int resourceId = Integer.parseInt(field.get(null).toString());
            EmoticonsConfig.emoticonsImageIds[i] = resourceId;

            EmoticonsModel item = new EmoticonsModel();
            item.setId(i);
            item.setEmoticonsResId(EmoticonsConfig.emoticonsImageIds[i]);
            item.setType(EmoticonsModel.TYPE_EMOTICONS_NORMAL);
            item.setEmoticonName("[" + imageName + "]");
            mEmoticonsData.add(item);

        } 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();
        }
    }

    // 添加删除图标
    EmoticonsModel item = new EmoticonsModel();
    item.setId(-1);// 删除图标不需要id
    item.setEmoticonsResId(R.drawable.ic_search_eliminate);
    item.setType(EmoticonsModel.TYPE_EMOTICONS_DELETE);
    item.setEmoticonName("[f_delete]");
    mEmoticonsData.add(item);

}

class EmotionsAdapter extends BaseAdapter {
    private LayoutInflater inflater;

    public EmotionsAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mEmoticonsData.size();
    }

    @Override
    public EmoticonsModel getItem(int arg0) {
        return mEmoticonsData.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.emoticons_item, parent,
                    false);

            viewHolder.emoticonsItemId = (TextView) convertView
                    .findViewById(R.id.imoticons_id);

            viewHolder.emoticonsItemName = (TextView) convertView
                    .findViewById(R.id.imoticons_ename_2_2);

            viewHolder.emoticonsItemType = (TextView) convertView
                    .findViewById(R.id.imoticons_type);

            viewHolder.emoticonsItemView = (ImageView) convertView
                    .findViewById(R.id.imoticons_image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        EmoticonsModel itemData = getItem(position);
        viewHolder.emoticonsItemView.setImageResource(itemData
                .getEmoticonsResId());
        viewHolder.emoticonsItemId.setTag(itemData.getId());
        viewHolder.emoticonsItemType.setTag(itemData.getType());
        viewHolder.emoticonsItemName.setTag(itemData.getEmoticonName());
        return convertView;
    }

}

private class ViewHolder {
    ImageView emoticonsItemView;
    TextView emoticonsItemId;
    TextView emoticonsItemType;
    TextView emoticonsItemName;
}

}

3、EmoticonsUtil.java

public class EmoticonsUtil {

private static String TAG = "EmoticonsUtil";
/**
 * 对spanableString进行正则判断,如果符合要求,则以表情图片代替
 * 
 * @param context
 * @param spannableString
 * @param patten
 * @param start
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws NumberFormatException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static void dealExpression(final Context context,
        SpannableString spannableString, Pattern patten, int start)
        throws SecurityException, NoSuchFieldException,
        NumberFormatException, IllegalArgumentException,
        IllegalAccessException {
    Matcher matcher = patten.matcher(spannableString);
    while (matcher.find()) {
        String key = matcher.group();
          if (matcher.start() < start) {
            continue;
        }
        String key1 = key.substring(1, key.length() - 1);
        Field field = R.drawable.class.getDeclaredField(key1);
        int resId = Integer.parseInt(field.get(null).toString()); // 通过上面匹配得到的字符串来生成图片资源id
        if (resId != 0) {
             Bitmap bitmap = BitmapFactory.decodeResource(
             context.getResources(), resId);
            // bitmap.setWidth(12);
            // bitmap.setHeight(12);

// Drawable drawable = context.getResources().getDrawable(resId);

            // 通过图片资源id来得到bitmap,用一个ImageSpan来包装
             ImageSpan imageSpan = new ImageSpan(context, bitmap);

// ImageSpan imageSpan = new ImageSpan(drawable,
// ImageSpan.ALIGN_BOTTOM);
int end = matcher.start() + key.length(); // 计算该图片名字的长度,也就是要替换的字符串的长度

            spannableString.setSpan(imageSpan, matcher.start(),
                    matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // 将该图片替换字符串中规定的位置中

            if (end < spannableString.length()) { // 如果整个字符串还未验证完,则继续
                start = matcher.end();
            }
            //break;
        }
    }
}

/**
 * 得到一个SpanableString对象,通过传入的字符串,并进行正则判断
 * 
 * @param context
 * @param str
 * @return
 */
public static SpannableString getExpressionString(Context context,
        String str, String regularExpression) {
    SpannableString spannableString = new SpannableString(str);

    Pattern sinaPatten = Pattern.compile(regularExpression,
            Pattern.CASE_INSENSITIVE); // 通过传入的正则表达式来生成一个pattern
    try {
        dealExpression(context, spannableString, sinaPatten, 0);
    } catch (Exception e) {
        Log.e("dealExpression", e.getMessage());
    }
    return spannableString;
}

public static boolean checkExpression(Context context,
        SpannableString spannableString, Pattern patten, int start)
        throws SecurityException, NoSuchFieldException,
        NumberFormatException, IllegalArgumentException,
        IllegalAccessException {
    Matcher matcher = patten.matcher(spannableString);
    while (matcher.find()) {
        String key = matcher.group();
        Log.v(EmoticonsConfig.TAG, "key:" + key);
        if (key != null) {
            return true;
        }
    }
    return false;
}

public static CharSequence formatMessage(Context context, String contact,
        String body, String contentType) {
    SpannableStringBuilder buf = new SpannableStringBuilder();
    if (!TextUtils.isEmpty(body)) {
        if (contentType != null && "textml".equals(contentType)) {
            buf.append(Html.fromHtml(body));
        } else {

            SpannableString spannableString = new SpannableString(
                    body.toString());
            try {
                // dealExpression(context, spannableString, sinaPatten, 0);
                spannableString = emotifySpannable(context, spannableString);

            } catch (Exception e) {
                Log.e("dealExpression", e.getMessage());
            }

            buf.append(spannableString);
            // buf.insert(where, spannableString);

        }
    }
    return buf;
}

public static CharSequence formatMessage(Context context, String body,
        String contentType) {
    SpannableStringBuilder buf = new SpannableStringBuilder();
    if (!TextUtils.isEmpty(body)) {
        if (contentType != null && "textml".equals(contentType)) {
            buf.append(Html.fromHtml(body));
        } else {

            SpannableString spannableString = new SpannableString(
                    body.toString());
            try {
                // dealExpression(context, spannableString, sinaPatten, 0);
                spannableString = emotifySpannable(context, spannableString);

            } catch (Exception e) {
                Log.e("dealExpression", e.getMessage());
            }

            buf.append(spannableString);
            // buf.insert(where, spannableString);

        }
    }
    return buf;
}

public static CharSequence formatMessage(Context context, String body,
        String contentType,int w, int h) {
    SpannableStringBuilder buf = new SpannableStringBuilder();
    if (!TextUtils.isEmpty(body)) {
        if (contentType != null && "textml".equals(contentType)) {
            buf.append(Html.fromHtml(body));
        } else {
            SpannableString spannableString = new SpannableString(
                    body.toString());

            try {
                // dealExpression(context, spannableString, sinaPatten, 0);
                spannableString = emotifySpannable(context, spannableString, w, h);

            } catch (Exception e) {
                Log.e("dealExpression", e.getMessage());
            }

            buf.append(spannableString);
        }
    }

    return buf;

}

/**
 * @description: 隐藏软键盘
 * @param activty
 *            调用此方法的所在activity
 */
public static void hideInputMethod(Context mContext) {
    if (null != mContext) {
        View view = ((Activity) mContext).getCurrentFocus();
        if (view != null) {
            InputMethodManager mInputMethodManager = (InputMethodManager) ((Activity) mContext)
                    .getApplicationContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE);
            if (null != mInputMethodManager) {
                boolean isOpen = mInputMethodManager.isActive();
                if (isOpen) {
                    mInputMethodManager.hideSoftInputFromWindow(
                            view.getWindowToken(), 0);
                }
            }
        }
    }
}

/**
 * Work through the contents of the string, and replace any occurrences of
 * [icon] with the imageSpan
 * 
 * @param spannable
 */

private static String START_CHAR = "[";
private static String END_CHAR = "]";

public static SpannableString emotifySpannable(Context mContext,
        SpannableString spannable) {

    int length = spannable.length();
    int position = 0;
    int tagStartPosition = 0;
    int tagLength = 0;
    StringBuilder buffer = new StringBuilder();
    boolean inTag = false;

    if (length <= 0) {
        return null;
    }

    do {
        String c = spannable.subSequence(position, position + 1).toString();

        if (!inTag && c.equals(START_CHAR)) {
            buffer = new StringBuilder();
            tagStartPosition = position;
            inTag = true;
            tagLength = 0;
        }

        if (inTag) {
            buffer.append(c);
            tagLength++;

            // 到达一个标签的结束位置
            if (c.equals(END_CHAR)) {
                inTag = false;

                String tag = buffer.toString();
                int tagEnd = tagStartPosition + tagLength;

                String hexStr = tag.substring(1, tag.length() - 1);

                try {
                    Log.d(TAG, "hexStr:" + hexStr);

                    Field field = R.drawable.class.getDeclaredField(hexStr);
                    int resId = Integer
                            .parseInt(field.get(null).toString());
                    if (resId != 0) {

                        Bitmap bitmap = BitmapFactory.decodeResource(
                                mContext.getResources(), resId);
                        ImageSpan imageSpan = new ImageSpan(mContext,
                                bitmap);
                        spannable.setSpan(imageSpan, tagStartPosition,
                                tagEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                } catch (Exception e) {
                }
            }
        }
        position++;
    } while (position < length);
    return spannable;
}
public static SpannableString emotifySpannable(Context mContext,
        SpannableString spannable, int w, int h) {
    int length = spannable.length();
    int position = 0;
    int tagStartPosition = 0;
    int tagLength = 0;
    StringBuilder buffer = new StringBuilder();
    boolean inTag = false;
    if (length <= 0) {
        return null;
    }
    do {
        String c = spannable.subSequence(position, position + 1).toString();
        if (!inTag && c.equals(START_CHAR)) {
            buffer = new StringBuilder();
            tagStartPosition = position;
            inTag = true;
            tagLength = 0;
        }
        if (inTag) {
            buffer.append(c);
            tagLength++;
            if (c.equals(END_CHAR)) {
                inTag = false;
                String tag = buffer.toString();
                int tagEnd = tagStartPosition + tagLength;
                String hexStr = tag.substring(1, tag.length() - 1);
                try {
                    Log.d(TAG, "hexStr:" + hexStr);
                    Field field = R.drawable.class.getDeclaredField(hexStr);
                    int resId = Integer
                            .parseInt(field.get(null).toString());
                    if (resId != 0) {
                        Drawable drawable = mContext.getResources()
                                .getDrawable(resId);
                        if (w > 0 && h > 0) {
                            drawable.setBounds(0, 0, w, h);// 这里设置图片的大小                                
                        }
                        // 通过图片资源id来得到bitmap,用一个ImageSpan来包装
                        ImageSpan imageSpan = new ImageSpan(drawable,
                                ImageSpan.ALIGN_BASELINE);

                        spannable.setSpan(imageSpan, tagStartPosition,
                                tagEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }

                } catch (Exception e) {
                }
            }
        }
        position++;
    } while (position < length);

    return spannable;

}

}

4、EmoticonsModel.java

public class EmoticonsModel {
private int id;// 表情编号
private int emoticonsResId;// 表情图标资源id
private int type;// 图标类型 0为普通图标 1为删除图标
private String emoticonName; //名称
public static final int TYPE_EMOTICONS_NORMAL = 0;
public static final int TYPE_EMOTICONS_DELETE = 1;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getEmoticonsResId() {
    return emoticonsResId;
}

public void setEmoticonsResId(int emoticonsResId) {
    this.emoticonsResId = emoticonsResId;
}

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}

public String getEmoticonName() {
    return emoticonName;
}

public void setEmoticonName(String emoticonName) {
    this.emoticonName = emoticonName;
}

}

5、EmoticonsConfig.java

/**
* 表情配置类,主要设置表情相关参数,如表情的总个数、表情的总页数、一页显示的个数、一行显示 的个数
*/
public class EmoticonsConfig {
public static final String TAG = “Expression”;
public static final int TOTAL_EMOTICONS_NUMBER = 57; // 表情的总个数

public static final int ONE_PAGE_EMOTICONS_NUMBER = 31;// 一页显示的个数

public static final int ONE_LINE_EMOTICONS_NUMBER = 8;// 一行显示 的个数

public static int[] emoticonsImageIds = new int[TOTAL_EMOTICONS_NUMBER];// 存放所有表情的resourceId

// public static final String EXPRESSION_PREFIX_NAME = "exp_";//表情文档名的前缀

// 正则表达式,用来判断消息内是否有表情
// 0xf000
// ~
// 0xf466    \\[f0[0-9]{2}|f[1-3][0-9][0-9]|f4[0-6][0-7]\\]
///\[:\w{*}\d{3}:\]/ 

// public static final String REGULAR_EXPRESSION = “

f0[09]2|f[13][09]2|f4[06][07]+//];publicstaticfinalStringREGULAR E XPRESSION=\[f[09]3 
”;

}

6、使用时候

xml:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值