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

参考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,g,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的颜色值,导致不能正常显示。那个最后显示的颜色,应该就是函数设置的一个默认颜色。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值