LightingColorFilter笔记

本文介绍了如何使用Android中的ColorFilter类来改变按钮的颜色和点击效果。通过具体代码示例展示了LightingColorFilter和ColorMatrixColorFilter的应用,以及如何通过设置不同的参数实现特定的颜色变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在http://stackoverflow.com网站上看到了别人修改button的颜色,点击效果什么的,

突然看到可以用

button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
以前没用到过LightingColorFilter这个类 ,google了下,
@Override
 protected void onDraw(Canvas canvas) {
  
  int mul = 0xFFFFFF00; //remove BLUE component
  int add = 0x0000FF00; //set GREEN full
  LightingColorFilter lightingColorFilter = new LightingColorFilter(mul, add);
  
  Paint MyPaint_Normal = new Paint();
  Paint MyPaint_Lighting = new Paint();
  MyPaint_Lighting.setColorFilter(lightingColorFilter);
     
     Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
     canvas.drawBitmap(myBitmap, 400, 100, MyPaint_Normal);
     canvas.drawBitmap(myBitmap, 500, 100, MyPaint_Lighting);
     
 };
因为是基于三基色配色方案来改的,
而且
0xFFFFFF00  0x表示16进制数,前两个ff表示的是透明度,00-ff,接下来两个ff表示红R,00-ff,后面两个ff表示绿G,00-ff,最后两个ff表示蓝B,00-ff,
所以呢
int mul = 0xFFFFFF00; //remove BLUE component           移除蓝色B部分,可以根据三基色配色图看
int add = 0x0000FF00; //set GREEN full                  绿色的填满,差不多就这个意思了。。。

具体的还可以去测试下
float[] colorMatrix = { 
    1, 0, 0, 0, 0, //red
    0, 0, 0, 0, 0, //green
    0, 0, 0, 0, 0, //blue
    0, 0, 0, 1, 0 //alpha  
  };
  
  Paint MyPaint = new Paint();
  ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
     MyPaint.setColorFilter(colorFilter);
     
     Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
     canvas.drawBitmap(myBitmap, 100, 100, MyPaint);
 };
这个画出来的效果就是红色和黑色了,因为不透明,这个是color矩阵的用法,不太懂矩阵。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值