设置TextView颜色最高效的方法

huangapple go评论84阅读模式
英文:

Most efficient way to set TextView colour

问题

Option 1 (使用 RGB 通道):

myTextView.setTextColor(Color.rgb(154,160,166))

Option 2 (使用颜色解析器):

myTextView.setTextColor(Color.parseColor("#2B3A11"))

Option 3 (使用来自 colors.xml 的颜色资源):

myTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_colour>))
英文:

What's the most efficient way to set the color of a TextView and why? Is there 1 method that is more memory and/or processor efficient? Or is there no difference at all to what happens with my app when it's running? Is it better to refer to a colour resource than declare RGB everytime I set a colour?

Option 1 (using RGB channel)

myTextView.setTextColor(Color.rgb(154,160,166))

Option 2 (using colour parser)

myTextView.setTextColor(Color.parseColor(&quot;#2B3A11&quot;))

Option 3 (using colour resource from colors.xml)

myTextView.setTextColor(ContextCompat.getColor(context, R.color.&lt;name_of_colour&gt;))

答案1

得分: 1

选项1应该是设置TextView颜色的最快方法。选项2是一个非常接近的第二选择。因为RGB值和Hex码的工作方式相似。关键是它们被调用的函数以及这些函数在后台如何执行(参见rgb parseColor以获取这些函数的描述和实现)。

就像myTextView.setTextColor(...)一样。因此,在后台发生的事情保持不变,只是颜色的选择方式不同。

在我设置颜色时,是引用颜色资源好,还是每次都声明RGB颜色好?

答:这取决于您自己的用途,例如,如果您想多次使用某种颜色,并且难以记住RGB代码,则肯定应将颜色保存在颜色资源中,以便以后引用。颜色资源就是为此目的而创建的,不是吗!正如@Fancesc所提到的,它确实可以使您的文档可读性更强,易于维护。因此,这是更专业的做法。

另一方面,在需要的时候直接使用RGB会节省很多麻烦。

  1. 您无需在任何地方保存任何数据。
  2. 在使用时,您无需搜索整个颜色资源文件。
  3. 99%的情况下,使用的颜色模型都是RGB/RGBA,因此您所做的一切都会以此结束。
  4. 而且更新使得在颜色的RGB和Hex值上更容易操作。一个示例在这里展示。
  5. 适用于不擅长颜色名称的人。

我假设您已经了解了所有这些,但还是决定提问。XD。祝您有美好的一天。

英文:

Option 1 Should be the fastest way to set color to your TextView. With option 2 being a very closed Second. Because both RGB values and Hexcodes function in a similar way. It all comes down to the functions they are called from and how these functions are executed in the background (look rgb parseColor for these function's description and implementation ). <br><br>
As
myTextView.setTextColor(...) is the same. So what happens to your textview in the background remains the same only the picking up of color is done differently. <br>

Is it better to refer to a color resource than declare RGB everytime I set a color?
<br>Ans- It depends on the your own usage,for example <br><br>if you want to use a color multiple times and find it harder to remember the rgb code then you should definitely save the color int the color resource and refer to it later. The color resource was created for this purpose only right! And as @Fancesc mentioned it does make your documents readable and maintainable. Hence, the more professional way of doing things.

<br>On the other hand using rgb right whenever needed saves you a lot of trouble.

  1. You don't need to save any data anywhere.
  2. While using you don't need to search the whole color resource file.
  3. 99% times the color model used is rgb/rgba, so everything you do ends up here.
  4. Plus newer updates make it easier to work on rgb and hex values for colors. one such example is shown here.
  5. Good for people who are bad with color names.

<br><br>
I am assuming you knew all of this already but decided to ask it anyways.XD. Have a good Day.

huangapple
  • 本文由 发表于 2020年4月11日 07:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61150095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定