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

后端 未结 5 395
盖世英雄少女心
盖世英雄少女心 2020-12-29 23:09

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);
相关标签:
5条回答
  • 2020-12-29 23:16

    Set any color

    paint.setColor( Color.rgb(R, G, B) )
    
    0 讨论(0)
  • 2020-12-29 23:17

    Try using color.white:

    paint.setColor(Color.white)
    
    0 讨论(0)
  • 2020-12-29 23:31
    paint.setColor(Color.parseColor("#FFFFFF"))
    
    0 讨论(0)
  • 2020-12-29 23:34

    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>
    
    0 讨论(0)
  • 2020-12-29 23:43
    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.

    0 讨论(0)
提交回复
热议问题