ANDROID仿QQ聊天界面及发送动态表情(修改版)

来源:互联网 发布:上海长期租车价格 知乎 编辑:程序博客网 时间:2024/06/05 08:11

参考地址:http://dwtedx.com/itshare_240.html

本博客只是用来学习和自己方便阅读,我在网上找了很多类似的觉得很多都不符合条件,无意间看到了一个很不错的例子,就是上面的那个参考地址。我下下来了感觉还是有些地方需要改,就先看了下他的源码。用了一下午的时间把它做成了我自己需求的东西。

修改了他原来的正则格式 :

private SpannableStringBuilder getFace(String png) {SpannableStringBuilder sb = new SpannableStringBuilder();try {/** * 经过测试,虽然这里tempText被替换为png显示,但是但我单击发送按钮时,获取到輸入框的内容是tempText的值而不是png * 所以这里对这个tempText值做特殊处理 * 格式:#[face/png/f_static_000.png]#,以方便判斷當前圖片是哪一個 * */String tempText = "[" + png + "]";sb.append(tempText);sb.setSpan(new ImageSpan(MainActivity.this, BitmapFactory.decodeStream(getAssets().open("face/png/"+png+".png"))), sb.length()- tempText.length(), sb.length(),//<span style="font-family: Arial, Helvetica, sans-serif;">("face/png/"+png+".png")图片资源地址,前面去掉了这里的补上去</span>Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);} catch (Exception e) {e.printStackTrace();}return sb;}/** * 向输入框里添加表情 * */private void insert(CharSequence text) {int iCursorStart = Selection.getSelectionStart((input.getText()));int iCursorEnd = Selection.getSelectionEnd((input.getText()));if (iCursorStart != iCursorEnd) {((Editable) input.getText()).replace(iCursorStart, iCursorEnd, "");}int iCursor = Selection.getSelectionEnd((input.getText()));((Editable) input.getText()).insert(iCursor, text);}/** * 删除图标执行事件 * 注:如果删除的是表情,在删除时实际删除的是tempText即图片占位的字符串,所以必需一次性删除掉tempText,才能将图片删除 * */private void delete() {if (input.getText().length() != 0) {int iCursorEnd = Selection.getSelectionEnd(input.getText());int iCursorStart = Selection.getSelectionStart(input.getText());if (iCursorEnd > 0) {if (iCursorEnd == iCursorStart) {if (isDeletePng(iCursorEnd)) {String st = "[f_static_000]";((Editable) input.getText()).delete(iCursorEnd - st.length(), iCursorEnd);} else {((Editable) input.getText()).delete(iCursorEnd - 1,iCursorEnd);}} else {((Editable) input.getText()).delete(iCursorStart,iCursorEnd);}}}}/** * 判断即将删除的字符串是否是图片占位字符串tempText 如果是:则讲删除整个tempText * **/private boolean isDeletePng(int cursor) {String st = "[f_static_000]";String content = input.getText().toString().substring(0, cursor);if (content.length() >= st.length()) {String checkStr = content.substring(content.length() - st.length(),content.length());String regex = "(\\[f_static_)\\d{1,3}(\\])";Pattern p = Pattern.compile(regex);Matcher m = p.matcher(checkStr);return m.matches();}return false;}
这是两个adapter里我改的东西 我这里是为了方便服务器传过来的代码让他直接显示。

聊天记录里的adapter:

private SpannableStringBuilder handler(final TextView gifTextView,String content) {SpannableStringBuilder sb = new SpannableStringBuilder(content);String regex = "(\\[f_static_)\\d{1,3}(\\])";Pattern p = Pattern.compile(regex);Matcher m = p.matcher(content);while (m.find()) {String tempText = m.group();try {String num = tempText.substring("[f_static_".length(), tempText.length()- "]".length());String gif = "face/gif/f" + num + ".gif";//前面可以替换成自己定义的名字/** * 如果open这里不抛异常说明存在gif,则显示对应的gif * 否则说明gif找不到,则显示png * */InputStream is = mContext.getAssets().open(gif);sb.setSpan(new AnimatedImageSpan(new AnimatedGifDrawable(is,new AnimatedGifDrawable.UpdateListener() {@Overridepublic void update() {gifTextView.postInvalidate();}})), m.start(), m.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);is.close();} catch (Exception e) {Log.i("amp", "haha");String png = tempText.substring("[".length(),tempText.length() - "]".length());try {sb.setSpan(new ImageSpan(mContext, BitmapFactory.decodeStream(mContext.getAssets().open(png))), m.start(), m.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}e.printStackTrace();}}return sb;}

@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {ViewHodler hodler;if (convertView == null) {hodler = new ViewHodler();convertView = LayoutInflater.from(mContext).inflate(R.layout.face_image, null);hodler.iv = (ImageView) convertView.findViewById(R.id.face_img);hodler.tv = (TextView) convertView.findViewById(R.id.face_text);convertView.setTag(hodler);} else {hodler = (ViewHodler) convertView.getTag();}try {Bitmap mBitmap = BitmapFactory.decodeStream(mContext.getAssets().open("face/png/" + list.get(position)));hodler.iv.setImageBitmap(mBitmap);} catch (IOException e) {e.printStackTrace();}hodler.tv.setText(list.get(position).replace(".png", ""));//我把他后面的后缀名去掉只显示名字return convertView;}


下载源码请点击

1 0
原创粉丝点击