How to set paint.setColor(R.color.white)

耗尽温柔 提交于 2019-11-29 05:56:08

问题


I have a custom View that uses Paint and Canvas to draw objects. My question is how to set:

int color = R.color.white;
paint.setColor(color);

from my /res/valuse/color.xml which includes resources like

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    ...
</resources>

回答1:


int color = ContextCompat.getColor(context, R.color.white);
paint.setColor(color);

The setColor() method takes a color number as int value, but not a resource id which is an int as well.




回答2:


Try using color.white:

paint.setColor(Color.white)



回答3:


first get your color from xml file

int color = context.getResources().getColor(R.color.colorPrimary); // old

is deprecated now, use this instead

int color = ContextCompat.getColor(context, R.color.colorPrimary); // new

set color

paint.setColor(color);

xml file preview: res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>


来源:https://stackoverflow.com/questions/12899428/how-to-set-paint-setcolorr-color-white

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!