转换到绘制一个特定的字符串 [英] Convert drawable to a specific string

查看:85
本文介绍了转换到绘制一个特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要与表情符号扩展聊天应用。

I have a chat app which I want to extend with emoticons.

这code用来插入文本一个表情符号:

This code is used to insert a smilie in the text:

   Spanned cs = Html.fromHtml("<img src ='"+ index +"'/>", imageGetter, null);     

   int cursorPosition = content.getSelectionStart();        
   content.getText().insert(cursorPosition, cs);

这是伟大的工作。表情符号在正确的地方TextView中显示出来。

This is working great. The smilies show up in the textView at the right place.

现在我想通过HTTP发送文本到我的服务器。
我想保存:),而不是形象作为使用的是旧版本的应用程序无法显示图像的。在新版本中我显示文本之前转换:)的形象。有没有什么办法将图像转换为特定的字符串?

Now I want to send the text to my server via HTTP. I would like to store ":)" instead of the image as for ones using an older app version the image can not be displayed. In the new version I convert ":)" to the image before displaying the text. Is there any way to convert the image to a specific string?

推荐答案

如果您要更换你的表情试试这个:

if you want to replace your emoticons try this:

EditText et = new EditText(this);
et.setTextSize(24);
et.setHint("this view shows \":)\" as an emoticon, try to type \":)\" somewhere");
final Bitmap smile = BitmapFactory.decodeResource(getResources(), R.drawable.emo_im_happy);
final Pattern pattern = Pattern.compile(":\\)");
TextWatcher watcher = new TextWatcher() {
boolean fastReplace = true; 
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //Log.d(TAG, "onTextChanged " + start + " " + before + " " + count);
        if (fastReplace) {
            if (start > 0 && count > 0) {
                String sub = s.subSequence(start - 1, start + 1).toString();
                if (sub.equals(":)")) {
                    Spannable spannable = (Spannable) s;
                    ImageSpan smileSpan = new ImageSpan(smile);
                    spannable.setSpan(smileSpan, start-1, start+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        } else {
            Spannable spannable = (Spannable) s;
            Matcher matcher = pattern.matcher(s);
            while (matcher.find()) {
                int mstart = matcher.start();
                int mend = matcher.end();
                ImageSpan[] spans = spannable.getSpans(mstart, mend, ImageSpan.class);
                Log.d(TAG, "onTextChanged " + mstart + " " + mend + " " + spans.length);
                if (spans.length == 0) {
                    ImageSpan smileSpan = new ImageSpan(smile);
                    spannable.setSpan(smileSpan, mstart, mend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        Log.d(TAG, "onTextChanged " + s);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        Log.d(TAG, "afterTextChanged " + s);
    }
};
et.addTextChangedListener(watcher );

setContentView(et);

在这里如果 fastReplace ==真您不必扫描整个文本,但它只是最小的实现:只有当你键入的作品)右后键入 :如果 fastReplace ==虚假它取代的每次出现:)一个笑脸,但它必须扫描整个文本,这是一个有点慢,当文字是相当大

here if fastReplace == true you don't have to scan the whole text but it's only minimal implementation: works only if you type ")" right after typed ":", if fastReplace == false it replaces every occurrence of ":)" with a smiley but it has to scan the whole text so it's a bit slower when text is quite large

这篇关于转换到绘制一个特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆