Android:TextView与EditText中插入表情图片
2016-11-18 09:29
351 查看
表情工具类,用于在TextView与EditText中插入表情图片:
给TextView插入表情图片:
给EditText插入表情图片:
public class FaceUtil { public int[] faceIds; public String[] faceCodes; public Map<String, Integer> mFaceMap; private String key0; private String key; private static FaceUtil mFaceUtil = new FaceUtil(); private FaceUtil(){ faceIds = new int[] { R.drawable.face0, R.drawable.face1, R.drawable.face2, R.drawable.face3, R.drawable.face4, R.drawable.face5, R.drawable.face6, R.drawable.face7, R.drawable.face8, R.drawable.face9, R.drawable.face10, R.drawable.face11, R.drawable.face12, R.drawable.face13, R.drawable.face14, R.drawable.face15, R.drawable.face16, R.drawable.face17, R.drawable.face18, R.drawable.face19, R.drawable.face20, R.drawable.face21, R.drawable.face22, R.drawable.face23, R.drawable.face24, R.drawable.face25, R.drawable.face26, R.drawable.face27, R.drawable.face28, R.drawable.face29, R.drawable.face30, R.drawable.face31, R.drawable.face32, R.drawable.face33, R.drawable.face34, R.drawable.face35, R.drawable.face36, R.drawable.face37, R.drawable.face38, R.drawable.face39, R.drawable.face40, R.drawable.face41, R.drawable.face42, R.drawable.face43, R.drawable.face44, R.drawable.face45, R.drawable.face46, R.drawable.face47, R.drawable.face48, R.drawable.face49, R.color.white, R.color.white, R.color.white, R.color.white, R.color.white, R.color.white }; faceCodes = new String[] { "[face:0]", "[face:1]", "[face:2]", "[face:3]", "[face:4]", "[face:5]", "[face:6]", "[face:7]", "[face:8]", "[face:9]", "[face:10]", "[face:11]", "[face:12]", "[face:13]", "[face:14]", "[face:15]", "[face:16]", "[face:17]", "[face:18]", "[face:19]", "[face:20]", "[face:21]", "[face:22]", "[face:23]", "[face:24]", "[face:25]", "[face:26]", "[face:27]", "[face:28]", "[face:29]", "[face:30]", "[face:31]", "[face:32]", "[face:33]", "[face:34]", "[face:35]", "[face:36]", "[face:37]", "[face:38]", "[face:39]", "[face:40]", "[face:41]", "[face:42]", "[face:43]", "[face:44]", "[face:45]", "[face:46]", "[face:47]", "[face:48]", "[face:49]", "/0050", "/0051", "/0052", "/0053", "/0054", "/0055" }; mFaceMap = new HashMap<String, Integer>(); mFaceMap.put("[face:0]", R.drawable.face0); mFaceMap.put("[face:1]", R.drawable.face1); mFaceMap.put("[face:2]", R.drawable.face2); mFaceMap.put("[face:3]", R.drawable.face3); mFaceMap.put("[face:4]", R.drawable.face4); mFaceMap.put("[face:5]", R.drawable.face5); mFaceMap.put("[face:6]", R.drawable.face6); mFaceMap.put("[face:7]", R.drawable.face7); mFaceMap.put("[face:8]", R.drawable.face8); mFaceMap.put("[face:9]", R.drawable.face9); mFaceMap.put("[face:10]", R.drawable.face10); mFaceMap.put("[face:11]", R.drawable.face11); mFaceMap.put("[face:12]", R.drawable.face12); mFaceMap.put("[face:13]", R.drawable.face13); mFaceMap.put("[face:14]", R.drawable.face14); mFaceMap.put("[face:15]", R.drawable.face15); mFaceMap.put("[face:16]", R.drawable.face16); mFaceMap.put("[face:17]", R.drawable.face17); mFaceMap.put("[face:18]", R.drawable.face18); mFaceMap.put("[face:19]", R.drawable.face19); mFaceMap.put("[face:20]", R.drawable.face20); mFaceMap.put("[face:21]", R.drawable.face21); mFaceMap.put("[face:22]", R.drawable.face22); mFaceMap.put("[face:23]", R.drawable.face23); mFaceMap.put("[face:24]", R.drawable.face24); mFaceMap.put("[face:25]", R.drawable.face25); mFaceMap.put("[face:26]", R.drawable.face26); mFaceMap.put("[face:27]", R.drawable.face27); mFaceMap.put("[face:28]", R.drawable.face28); mFaceMap.put("[face:29]", R.drawable.face29); mFaceMap.put("[face:30]", R.drawable.face30); mFaceMap.put("[face:31]", R.drawable.face31); mFaceMap.put("[face:32]", R.drawable.face32); mFaceMap.put("[face:33]", R.drawable.face33); mFaceMap.put("[face:34]", R.drawable.face34); mFaceMap.put("[face:35]", R.drawable.face35); mFaceMap.put("[face:36]", R.drawable.face36); mFaceMap.put("[face:37]", R.drawable.face37); mFaceMap.put("[face:38]", R.drawable.face38); mFaceMap.put("[face:39]", R.drawable.face39); mFaceMap.put("[face:40]", R.drawable.face40); mFaceMap.put("[face:41]", R.drawable.face41); mFaceMap.put("[face:42]", R.drawable.face42); mFaceMap.put("[face:43]", R.drawable.face43); mFaceMap.put("[face:44]", R.drawable.face44); mFaceMap.put("[face:45]", R.drawable.face45); mFaceMap.put("[face:46]", R.drawable.face46); mFaceMap.put("[face:47]", R.drawable.face47); mFaceMap.put("[face:48]", R.drawable.face48); mFaceMap.put("[face:49]", R.drawable.face49); } public static FaceUtil getFaceUtil(){ if(mFaceUtil == null){ mFaceUtil = new FaceUtil(); } return mFaceUtil; } /** * 将表情图片及文本加入到EditText中 * @param context * @param editText * @param imgId */ public void addFaceToEditText(Context context, EditText editText, String faceCode, int faceId) { int start = editText.getSelectionStart(); Spannable spannable = editText.getText().insert(start, faceCode); Drawable drawable = context.getResources().getDrawable(faceId); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); ImageSpan imageSpan = new ImageSpan(drawable, faceCode, ImageSpan.ALIGN_BASELINE); spannable.setSpan(imageSpan, start, start + faceCode.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } /** * 将表情图片及文本加入到TextView中 * @param context * @param textView */ public void addFacesToTextView(final Context context, TextView textView, String str){ boolean one = true; boolean isFace = false; boolean isFaceFirst = false; List<Spanned> faceList = new ArrayList<Spanned>(); String text = str; Pattern p = Pattern.compile("face:[0-9]+"); final Matcher m = p.matcher(text); while(m.find()){ key0 = m.group(); key = "["+m.group()+"]"; if(mFaceMap.get(key) == null){ textView.setText(str); return; } isFace = true; ImageGetter imageGetter = new ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable = context.getResources().getDrawable(mFaceMap.get(key)); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); return drawable; } }; if(text.length() != key.length() && text.indexOf(key) != 0){ one = false; isFaceFirst = false; }else if(text.length() != key.length() && text.indexOf(key) == 0){ one = false; isFaceFirst = true; }else if(text.length() == key.length() && one){ textView.setText(""); textView.append(Html.fromHtml("<img src='" + mFaceMap.get(key) + "'/>", imageGetter, null)); return; } Spanned spanned = Html.fromHtml("<img src='" + mFaceMap.get(key) + "'/>", imageGetter, null); faceList.add(spanned); text = text.replaceAll("\\[" + key0 + "\\]", "="); } if(isFace){ textView.setText(""); String[] textArray = text.split("="); /**哪个数组大就以此数组个数为循环次数**/ int len = 0; if(textArray.length > faceList.size()){ len = textArray.length; }else{ len = faceList.size(); } for (int i = 0; i < len; i++) { if(textArray.length > faceList.size()){ if(i < faceList.size()){ if(isFaceFirst){ textView.append(faceList.get(i)); textView.append(textArray[i]); }else { textView.append(textArray[i]); textView.append(faceList.get(i)); } }else{ textView.append(textArray[i]); } }else{ if(i < textArray.length){ if(isFaceFirst){ textView.append(faceList.get(i)); textView.append(textArray[i]); }else { textView.append(textArray[i]); textView.append(faceList.get(i)); } }else{ textView.append(faceList.get(i)); } } } } } public void onDestroy(){ key0 = null; key = null; faceIds = null; faceCodes = null; if(mFaceMap != null){ mFaceMap.clear(); mFaceMap = null; } mFaceUtil = null; } }
给TextView插入表情图片:
TextView tv = ...; String msg = "高兴[face:2],无奈[face:10][face:11]"; FaceUtil.getFaceUtil().addFacesToTextView(mContext, tv, msg);
给EditText插入表情图片:
EditText et = ...; FaceUtil.getFaceUtil().addFaceToEditText(mContext, et, "[face:10]", R.drawable.face10);
相关文章推荐
- android在EditText中插入表情图片
- android在EditText中插入表情图片
- android textview和edittext中插入表情
- Android EditText中插入图片,比如做表情发送
- 在EditText中插入表情图片 (CharacterStyle&SpannableString),TextView中显示HTML和图片
- 在EditText/TextView中插入表情图片、样式、下划线等
- android TextView,EditText插入表情
- android textview和edittext中插入表情
- 在EditText/TextView中插入表情图片、样式、下划线等(转)
- android在EditText中插入表情图片
- 在EditText中插入表情图片 (CharacterStyle&SpannableString),TextView中显示HTML和图片
- 在EditText/TextView中插入表情图片、样式、下划线等
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- android TextView、EditText对部分内容设置颜色、字体、超链接、图片、点击事件
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- Android学习札记51:在TextView显示插入的图片
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- 在EditText中插入表情图片 (CharacterStyle&SpannableString)
- android TextView和EditText中显示图片