TextView 中动态文本颜色更改的最有效方法

问题描述 投票:0回答:4

我想用计时器多次更改文本部分的颜色。

最简单的方法是这样的:

SpannableStringBuilder ssb = new SpannableStringBuilder(mainText);
ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
ssb.setSpan(span, start, end, 0);
tv.setText(ssb);

但是,如果我在一秒钟内多次运行上述代码,我实际上每次都会更改

TextView
的整个(大)文本,因此在低端设备上会出现不需要的内存 CPU 负载。

如何在

Span
上有一个
TextView
并且仅更改
Span
的开始和结束位置?

它会起作用还是会在幕后进行全文替换?

我的文字是固定的,永远不会改变。

android textview spannablestring
4个回答
9
投票

不调用

setText
方法进行跨度移动的解决方案:

    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);

动态变色解决方案:

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));
    }
}

和测试代码:

    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();

1
投票

执行以下代码一次:

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

每次您想要更改跨度时,请执行以下操作:

ssb.clearSpans()
ssb.setSpan(span, start, end, 0);
tv.setText(ssb);

0
投票

我今天下午遇到了同样的问题,这是我的解决方案:

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

我发现这是一种更有效的方法,在我的项目中我使用set-whole-text方法花了400~600毫秒,这样只需要0或1毫秒。


0
投票

主要思想是修改textview持有的spannable,而不需要再次调用setText

阅读这些文章中的此标题:创建自定义跨度此标题:设置文本以获得最佳性能以获取更多信息。

val spannable = SpannableString("Text is\nspantastic")
val foregroundColor = ForegroundColorSpan(Color.RED)
spannable.setSpan(
     foregroundColor, 
     /*start*/ 1, 
     /*end*/ 3, 
     SPAN_INCLUSIVE_INCLUSIVE
)
textView.setText(spannable, BufferType.SPANNABLE)

GlobalScope.launch{
  // later getting the mutable instance of the text object held 
  // by the TextView
  // this can can be cast to Spannable only because we set it as a
  // BufferType.SPANNABLE before
  val spannableText = textView.text as Spannable
  delay(1000)
  spannableText.setSpan(foregroundColor, 2, 4, SPAN_INCLUSIVE_INCLUSIVE)

  delay(1000)
  spannableText.setSpan(foregroundColor, 3, 5, SPAN_INCLUSIVE_INCLUSIVE)
}

注意,我们只更新了

spannableText
,不需要再次调用
setText()
,不同的是,再次设置文本会导致视图重绘和重新计算,而更新
spannableText
会导致某些方法被调用更新跨度而不从零重新绘制视图,这是最有效的方法。

如果改变特定跨度的颜色

您将需要覆盖

updateDrawState
并设置颜色,您还必须根据更改调用
invalidate()
requestLayout()
,请检查我上面提到的第二篇文章中的“奖励性能提示”。这是更新 URLSpan 的链接颜色的示例

    class MutableColorURLSpan(url: String) : URLSpan(url) {
        var color = 0

        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            ds.color = color
        }
    }

        val spannable = buildSpannedString {
            append("Hello")
            setSpan(MutableColorURLSpan("https://www.google.com/"), 0, 4, SPAN_INCLUSIVE_INCLUSIVE)

            append(" World")
            setSpan(MutableColorURLSpan("https://www.google.com/"), 5, 10, SPAN_INCLUSIVE_INCLUSIVE)

            append("!")
            setSpan(MutableColorURLSpan("https://www.google.com/"), 11, 12, SPAN_INCLUSIVE_INCLUSIVE)
        }
        binding.textView.setText(spannable, BufferType.SPANNABLE)
        GlobalScope.launch(Dispatchers.Main) {
            val spannableText = binding.textView.text as Spannable
            delay(1000)
            spannableText.getSpans<MutableColorURLSpan>().forEach { it.color = Color.RED }
            binding.textView.invalidate()

            delay(1000)
            spannableText.getSpans<MutableColorURLSpan>().forEach { it.color = Color.BLUE }
            binding.textView.invalidate()

            delay(1000)
            spannableText.getSpans<MutableColorURLSpan>().forEach { it.color = Color.GREEN }
            binding.textView.invalidate()
        }


同样适用于其他 Span,如果您的要求是频繁更新某些属性,如颜色、大小,那么您将需要继承 Span 并将可变属性存储在类中,并将其设置在

updateDrawState()
或负责的相应方法中设置属性。

© www.soinside.com 2019 - 2024. All rights reserved.