1

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

rapzzard
  • 11
  • 4
  • you are not using any images in your `ImageSpan` but just solid flat colors, instead you should use `BackgtoundColorSpan` – pskink Jun 09 '15 at 05:48
  • @pskink : It is just examining for performance. I will use image for these ImageSpan later. Thank you! – rapzzard Jun 09 '15 at 05:58
  • so how many ImageSpans will you use? – pskink Jun 09 '15 at 06:24
  • @pskink : I don't know how many ImageSpan will be used. do you know HandRite App? because it is an editor, user's input is infite. – rapzzard Jun 09 '15 at 06:30
  • If ImageSpan or any other span isn't a suitable method for this problem, please let me know solution what you think. – rapzzard Jun 09 '15 at 06:33
  • but they are using BackgtoundColorSpan and most likely BackgtoundColorSpan is much faster than ImageSpan – pskink Jun 09 '15 at 06:33
  • i don't know what effect you would like to have... similar like HandRite app? – pskink Jun 09 '15 at 06:35
  • @pskink who are they? I don't think HandRite use BackgroundColorSpan instead of ImageSpan. – rapzzard Jun 09 '15 at 06:36
  • @pskink : yes. I want to input handwriting characters as ImageSpan and IME inputted characters as normal like HandRite app. – rapzzard Jun 09 '15 at 06:38
  • see https://lh5.ggpht.com/MIqUz-yv0z0c2W2IB_BBPSViRtNhLKecPzHXGRwfohoWrF8mH-OdUF_xb-RorEHh8YA=h900-rw it is a BackgroundColorSpan – pskink Jun 09 '15 at 06:38
  • @pskink : I'm sorry that my explanation was bad, please check http://imgur.com/RUHJPJW – rapzzard Jun 09 '15 at 06:48
  • I want this feature and find easy methods for it. if there is nothing to resolve this problem easily. If I should implement custom edit view entirely for it, I will give up this feature. – rapzzard Jun 09 '15 at 06:52

1 Answers1

0

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);  
strike
  • 1,031
  • 8
  • 24
  • Creating time isn't importance at this time. I examined EditText's edit performance after creating. – rapzzard Jun 09 '15 at 05:14
  • Have you checked it using Imagegetter and in case if you not using any drawables, they are only flat colors then going with the BackgroundColorSpan would be more accurate. – strike Jun 09 '15 at 05:58
  • thank you, but I will use image for ImageSpan later. – rapzzard Jun 09 '15 at 06:02
  • I read [Html source code](https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/Html.java) and I found fromHtml method use same "ImageSpan". do you think there are some black magic for speeding up modifying performance not creating performance? – rapzzard Jun 09 '15 at 06:07
  • I'm replied you on the basis of complete documentation of Spanning you can also chek it here. http://stackoverflow.com/questions/17546955/android-spanned-spannedstring-spannable-spannablestring-and-charsequence – strike Jun 09 '15 at 06:51
  • Thank you! I will check it. – rapzzard Jun 09 '15 at 06:59