英文:
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("#2B3A11"))
Option 3 (using colour resource from colors.xml
)
myTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_colour>))
答案1
得分: 1
选项1应该是设置TextView颜色的最快方法。选项2是一个非常接近的第二选择。因为RGB值和Hex码的工作方式相似。关键是它们被调用的函数以及这些函数在后台如何执行(参见rgb parseColor以获取这些函数的描述和实现)。
就像myTextView.setTextColor(...)
一样。因此,在后台发生的事情保持不变,只是颜色的选择方式不同。
在我设置颜色时,是引用颜色资源好,还是每次都声明RGB颜色好?
答:这取决于您自己的用途,例如,如果您想多次使用某种颜色,并且难以记住RGB代码,则肯定应将颜色保存在颜色资源中,以便以后引用。颜色资源就是为此目的而创建的,不是吗!正如@Fancesc所提到的,它确实可以使您的文档可读性更强,易于维护。因此,这是更专业的做法。
另一方面,在需要的时候直接使用RGB会节省很多麻烦。
- 您无需在任何地方保存任何数据。
- 在使用时,您无需搜索整个颜色资源文件。
- 99%的情况下,使用的颜色模型都是RGB/RGBA,因此您所做的一切都会以此结束。
- 而且更新使得在颜色的RGB和Hex值上更容易操作。一个示例在这里展示。
- 适用于不擅长颜色名称的人。
我假设您已经了解了所有这些,但还是决定提问。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.
- You don't need to save any data anywhere.
- While using you don't need to search the whole color resource file.
- 99% times the color model used is rgb/rgba, so everything you do ends up here.
- Plus newer updates make it easier to work on rgb and hex values for colors. one such example is shown here.
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论