Most efficient way for Dynamic Text Color Change in TextView

后端 未结 3 1398
情话喂你
情话喂你 2020-12-17 02:42

I want to change color of parts of a text several times with a timer.

Simplest way is this:

SpannableStringBuilder ssb = new SpannableStringBuilder(m         


        
相关标签:
3条回答
  • 2020-12-17 03:36

    I meet the same issue this afternoon,and here is my solution:

    tv.setText(yourText, TextView.BufferType.SPANNABLE);
    ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
    ((Spannable) article.getText()).setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    I found it's a much more efficient way,in my project I used set-whole-text method took 400~600 milliseconds,by this way only 0 or 1 millisecond.

    0 讨论(0)
  • 2020-12-17 03:40

    Solution for span movement without calling setText method:

        final TextView tv = new TextView(this);
        tv.setTextSize(32);
        setContentView(tv);
    
        SpannableStringBuilder ssb = new SpannableStringBuilder("0123456789012345678901234567890123456789");
        ssb.append(ssb).append(ssb);
        tv.setText(ssb, BufferType.SPANNABLE);
        final Spannable sp = (Spannable) tv.getText();
        final ForegroundColorSpan span = new ForegroundColorSpan(0xffff0000);
        Runnable action = new Runnable() {
            @Override
            public void run() {
                sp.setSpan(span, start, start + 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                start++;
                if (start <= sp.length() - 4) {
                    tv.postDelayed(this, 50);
                }
            }
        };
        tv.postDelayed(action, 1000);
    

    Solution for dynamic color change:

    class HSVSpan extends CharacterStyle {
        int color;
        float[] hsv = {0, 1, 1};
    
        @Override
        public void updateDrawState(TextPaint tp) {
            tp.setColor(color);
        }
    
        public void update() {
            hsv[0] += 5;
            hsv[0] %= 360;
            color = Color.HSVToColor(hsv);
    //        Log.d(TAG, "update " + Integer.toHexString(color));
        }
    }
    

    and testing code:

        final TextView tv = new TextView(this);
        setContentView(tv);
        SpannableStringBuilder ssb = new SpannableStringBuilder("0123456789");
        final HSVSpan span = new HSVSpan();
        ssb.setSpan(span, 2, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(ssb);
        tv.setTextSize(32);
    
        Runnable action = new Runnable() {
            @Override
            public void run() {
                span.update();
                tv.invalidate();
                tv.postDelayed(this, 50);
            }
        };
        action.run();
    
    0 讨论(0)
  • 2020-12-17 03:42

    execute below code once:

    SpannableStringBuilder ssb = new SpannableStringBuilder(mainText);
    ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
    

    and every time you want to change span do this:

    ssb.clearSpans()
    ssb.setSpan(span, start, end, 0);
    tv.setText(ssb);
    
    0 讨论(0)
提交回复
热议问题