EditText is too slow when there are many ImageSpan in it

1k views Asked by At

I want to customize EditText's behavior like HandRite.

So I tried examing EditText's editing performance when there are many ImageSpan, but it was too slow when adding or deleting character on middle using touch screen and IME after below code.

How can I speed up this performance?

StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 1000; i++) {
        sb.append(' ');
    }

    SpannableString ss = new SpannableString(sb.toString());

    Random random = new Random();

    for (int i = 0; i < 1000; i++) {
        Drawable d = new ColorDrawable(random.nextInt());
        d.setBounds(0, 0, mEditText.getLineHeight(), mEditText.getLineHeight());
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        ss.setSpan(span, i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    mEditText.setText(ss);

HandRite's behavior

1

There are 1 answers

5
strike On

Since the number of ImageSpan objects are very large almost 1000 in your SpannableString which is causing it getting slow.

Try to use HTML teext along with ImageGetter and check peformance :

ImageGetter imageGetter = new ImageGetter() {
            public Drawable getDrawable(String source) {    
                StringTokenizer st = new StringTokenizer(index, ".");
                Drawable d = new BitmapDrawable(getResources(),emoticons[Integer.parseInt(st.nextToken()) - 1]);
                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                return d;
            }
        };

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