加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

color.xml 中定义drawable和color的区别

发布时间:2020-12-16 06:28:25 所属栏目:百科 来源:网络整理
导读:参考stackoverflow, Basically nothing. They both create a resource that is a "color" value holding a 32 bit color. One is just a drawable subtype,one is a color subtype. When you retrieve a Drawable from resources,if the resource is actual

参考stackoverflow,

Basically nothing. They both create a resource that is a "color" value holding a 32 bit color. One is just a drawable subtype,one is a color subtype. When you retrieve a Drawable from resources,if the resource is actually a color constant it knows how to create a Drawable object for that.

两者只是资源类型不同,一个是drawable类型,一个是color类型,但是都是包含一个32位的颜色。根据不同的参数需求,选择不同的值。可能会使用到以下的方法:

getResources().getDrawable(R.drawable.progress_background_draw);
getResources().getColor(R.drawable.progress_background_draw);


一个返回的是Drawable类型,一个返回的是int类型的0xAARRGGBB颜色值。

所以当遇到函数参数为int 型,可以用上面的getresources().getColor()类型,也可以直接写 0xAARRGGBB的颜色值。


PS:补充一个由于这个参数种类导致的问题,调用的一个设置颜色的函数的参数是int类型,我在color.xml中定义了 drawable类型的颜色,在函数参数中传入这个R.drawable.progress_back_ground_draw ,但是显示的颜色不是自己想要的颜色,尝试换成其他drawable颜色,显示的仍然不是正确的,但是发现无论换成哪种drawable,显示的都是同一个颜色。

查看了android中这个方法的代码:

    /**
     * Set the paint's color. Note that the color is an int containing alpha
     * as well as r,g,b. This 32bit value is not premultiplied,meaning that
     * its alpha can be any value,regardless of the values of r,b.
     * See the Color class for more details.
     *
     * @param color The new color (including alpha) to set in the paint.
     */
    public native void setColor(int color);
原来需要传入的参数是十六进制的颜色值,我给传入的都是#RRGGBB的颜色值,导致不能正常显示。那个最后显示的颜色,应该就是函数设置的一个默认颜色。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读