Drawable tint 着色

其实在 Android Support V4 的包中提供了 DrawableCompat 类,我们很容易写出如下的辅助方法来实现 Drawable 的着色,如下:

public static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {  
    final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTintList(wrappedDrawable, colors);
    return wrappedDrawable;
}

使用例子:

EditText editText1 = (EditText) findViewById(R.id.edit_1);  
final Drawable originalDrawable = editText1.getBackground();  
final Drawable wrappedDrawable = tintDrawable(originalDrawable, ColorStateList.valueOf(Color.RED));  
editText1.setBackgroundDrawable(wrappedDrawable);

EditText editText2 = (EditText) findViewById(R.id.edit_2);  
editText2.setBackgroundDrawable(tintDrawable(editText2.getBackground(),  
        ColorStateList.valueOf(Color.parseColor("#03A9F4"))));

效果如下

image.png

https://www.race604.com/tint-drawable/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,180评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,663评论 2 45
  • 2017年6月8号下午考完试高三就解放了吧 也许9号早上你会睡到10点 也许你会急急忙忙穿上校服冲到客厅问妈妈怎么...
    徐大月阅读 988评论 0 0
  • 有一盏灯 被寂寞点亮 蓝色的火苗 在星夜燃烧 在每一个角落 在每一分钟 被禁固的目光 深藏着欲望 每一个欲望即是渴...
    江城妖怪阅读 832评论 0 0
  • 真对一个人好,没有时间去思考: 对这个人好有什么用,能有什么回报?! 真好都是傻好,一点也不复杂: 只是他开心了,...
    Mmiao_阅读 1,508评论 0 0