Android动态设置纯色图标的颜色

以前做了一个流量悬浮窗,悬浮窗里有当前网络状态的图标和网速的文字,想实现改变文字颜色的同时改变状态图标的颜色
流量悬浮窗
实现后分享出来让大家参考下

先上图

效果图

以下是关键代码
<ImageView
  android:id="@+id/iv_icon"
  android:layout_width="150dp"
  android:layout_height="150dp"
  android:src="@drawable/icon"/>
//设置图标的颜色
private void setIconColor(ImageView icon, int r, int g, int b, int a) {
    float[] colorMatrix = new float[]{
            0, 0, 0, 0, r,
            0, 0, 0, 0, g,
            0, 0, 0, 0, b,
            0, 0, 0, (float) a / 255, 0
    };
    icon.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
}
想知道具体原理的可以看一下这篇文章

Android学习笔记22:图像颜色处理(ColorMatrix)

如果图标是不透明的并且不需要透明度的变化,还有更简单的实现方法
icon.setColorFilter(Color.argb(255, r, g, b));
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值