setColorFilter(relativeFactor, PorterDuff.Mode.MULTIPLY);相对系数0-100%
我知道如何执行 setColorFilter(colorX, PorterDuff.Mode.MULTIPLY);,其中 colorX 的定义类似于 0x00000000 到 0xFFFFFFFF
。
但如果我有 0-100 之间的值并尝试转换它们,我就无法让它工作。 (100 表示 100% 透明度)
这是我尝试过的 - 一步一步 - 但不起作用:
float x1 = (float) ((100 - x) / 100.0);
float x2 = x1 * 0xFFFFFFFF;
int relativeFactor = (int) x2;
Btn1.getBackground().setColorFilter(relativeFactor, PorterDuff.Mode.MULTIPLY);
这里出了什么问题?非常感谢!
I know how to do the setColorFilter(colorX, PorterDuff.Mode.MULTIPLY);
where colorX is defined like 0x00000000 to 0xFFFFFFFF
.
But I can't get this to work if I have values from 0-100 and try to convert these. (100 means 100% transparency)
This is what I tried - step by step - but doesn't work:
float x1 = (float) ((100 - x) / 100.0);
float x2 = x1 * 0xFFFFFFFF;
int relativeFactor = (int) x2;
Btn1.getBackground().setColorFilter(relativeFactor, PorterDuff.Mode.MULTIPLY);
What is wrong here? Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的原始代码执行混合模式不正确,并且出现溢出;你需要转换并使用一些
long
:这是一个不诉诸浮点的解决方案:
它在低端偏离了 95;如果你需要零,你可以测试一下。随意实际转储这些值......
Your original code is doing mixed-mode incorrectly, and you are getting overflow; you need to cast and use some
long
:Here's a solution that doesn't resort to floating-point:
It is off by 95 at the low end; you can test for that if you need zero. Feel free to actually dump the values....