setColorFilter(relativeFactor, PorterDuff.Mode.MULTIPLY);相对系数0-100%

发布于 2024-12-11 02:53:15 字数 438 浏览 0 评论 0原文

我知道如何执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2024-12-18 02:53:15

您的原始代码执行混合模式不正确,并且出现溢出;你需要转换并使用一些long

float x2 = x1 * (float)0x0FFFFFFFFL;
long relativeFactor = (long) x2 & 0X0FFFFFFFFL;

这是一个不诉诸浮点的解决方案:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    final long onepct = 0x0FFFFFFFFL/100L;
    final long error = 0x0FFFFFFFFL-(100*onepct);
    System.out.println("onepct=" + Long.toString(onepct,16) + ",error=" + Long.toString(error,16));
    for (int x = 0; x <= 100; x++) {
        final long x2 = 0x0FFFFFFFFL - onepct*x;
        final long relativeFactor = (long) x2 & 0xFFFFFFFF;
        System.out.println("x=" + x + ", x2=" + x2 + ", rf=" + Long.toString(relativeFactor, 16));
    }
}

它在低端偏离了 95;如果你需要零,你可以测试一下。随意实际转储这些值......

onepct=28f5c28,error=5f
x=0, x2=4294967295, rf=ffffffff
x=1, x2=4252017623, rf=fd70a3d7
x=2, x2=4209067951, rf=fae147af
x=3, x2=4166118279, rf=f851eb87
x=4, x2=4123168607, rf=f5c28f5f
x=5, x2=4080218935, rf=f3333337
x=6, x2=4037269263, rf=f0a3d70f
x=7, x2=3994319591, rf=ee147ae7
x=8, x2=3951369919, rf=eb851ebf
x=9, x2=3908420247, rf=e8f5c297
x=10, x2=3865470575, rf=e666666f
x=11, x2=3822520903, rf=e3d70a47
x=12, x2=3779571231, rf=e147ae1f
x=13, x2=3736621559, rf=deb851f7
x=14, x2=3693671887, rf=dc28f5cf
x=15, x2=3650722215, rf=d99999a7
x=16, x2=3607772543, rf=d70a3d7f
x=17, x2=3564822871, rf=d47ae157
x=18, x2=3521873199, rf=d1eb852f
x=19, x2=3478923527, rf=cf5c2907
x=20, x2=3435973855, rf=ccccccdf
x=21, x2=3393024183, rf=ca3d70b7
x=22, x2=3350074511, rf=c7ae148f
x=23, x2=3307124839, rf=c51eb867
x=24, x2=3264175167, rf=c28f5c3f
x=25, x2=3221225495, rf=c0000017
x=26, x2=3178275823, rf=bd70a3ef
x=27, x2=3135326151, rf=bae147c7
x=28, x2=3092376479, rf=b851eb9f
x=29, x2=3049426807, rf=b5c28f77
x=30, x2=3006477135, rf=b333334f
x=31, x2=2963527463, rf=b0a3d727
x=32, x2=2920577791, rf=ae147aff
x=33, x2=2877628119, rf=ab851ed7
x=34, x2=2834678447, rf=a8f5c2af
x=35, x2=2791728775, rf=a6666687
x=36, x2=2748779103, rf=a3d70a5f
x=37, x2=2705829431, rf=a147ae37
x=38, x2=2662879759, rf=9eb8520f
x=39, x2=2619930087, rf=9c28f5e7
x=40, x2=2576980415, rf=999999bf
x=41, x2=2534030743, rf=970a3d97
x=42, x2=2491081071, rf=947ae16f
x=43, x2=2448131399, rf=91eb8547
x=44, x2=2405181727, rf=8f5c291f
x=45, x2=2362232055, rf=8cccccf7
x=46, x2=2319282383, rf=8a3d70cf
x=47, x2=2276332711, rf=87ae14a7
x=48, x2=2233383039, rf=851eb87f
x=49, x2=2190433367, rf=828f5c57
x=50, x2=2147483695, rf=8000002f
x=51, x2=2104534023, rf=7d70a407
x=52, x2=2061584351, rf=7ae147df
x=53, x2=2018634679, rf=7851ebb7
x=54, x2=1975685007, rf=75c28f8f
x=55, x2=1932735335, rf=73333367
x=56, x2=1889785663, rf=70a3d73f
x=57, x2=1846835991, rf=6e147b17
x=58, x2=1803886319, rf=6b851eef
x=59, x2=1760936647, rf=68f5c2c7
x=60, x2=1717986975, rf=6666669f
x=61, x2=1675037303, rf=63d70a77
x=62, x2=1632087631, rf=6147ae4f
x=63, x2=1589137959, rf=5eb85227
x=64, x2=1546188287, rf=5c28f5ff
x=65, x2=1503238615, rf=599999d7
x=66, x2=1460288943, rf=570a3daf
x=67, x2=1417339271, rf=547ae187
x=68, x2=1374389599, rf=51eb855f
x=69, x2=1331439927, rf=4f5c2937
x=70, x2=1288490255, rf=4ccccd0f
x=71, x2=1245540583, rf=4a3d70e7
x=72, x2=1202590911, rf=47ae14bf
x=73, x2=1159641239, rf=451eb897
x=74, x2=1116691567, rf=428f5c6f
x=75, x2=1073741895, rf=40000047
x=76, x2=1030792223, rf=3d70a41f
x=77, x2=987842551, rf=3ae147f7
x=78, x2=944892879, rf=3851ebcf
x=79, x2=901943207, rf=35c28fa7
x=80, x2=858993535, rf=3333337f
x=81, x2=816043863, rf=30a3d757
x=82, x2=773094191, rf=2e147b2f
x=83, x2=730144519, rf=2b851f07
x=84, x2=687194847, rf=28f5c2df
x=85, x2=644245175, rf=266666b7
x=86, x2=601295503, rf=23d70a8f
x=87, x2=558345831, rf=2147ae67
x=88, x2=515396159, rf=1eb8523f
x=89, x2=472446487, rf=1c28f617
x=90, x2=429496815, rf=199999ef
x=91, x2=386547143, rf=170a3dc7
x=92, x2=343597471, rf=147ae19f
x=93, x2=300647799, rf=11eb8577
x=94, x2=257698127, rf=f5c294f
x=95, x2=214748455, rf=ccccd27
x=96, x2=171798783, rf=a3d70ff
x=97, x2=128849111, rf=7ae14d7
x=98, x2=85899439, rf=51eb8af
x=99, x2=42949767, rf=28f5c87
x=100, x2=95, rf=5f

Your original code is doing mixed-mode incorrectly, and you are getting overflow; you need to cast and use some long:

float x2 = x1 * (float)0x0FFFFFFFFL;
long relativeFactor = (long) x2 & 0X0FFFFFFFFL;

Here's a solution that doesn't resort to floating-point:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    final long onepct = 0x0FFFFFFFFL/100L;
    final long error = 0x0FFFFFFFFL-(100*onepct);
    System.out.println("onepct=" + Long.toString(onepct,16) + ",error=" + Long.toString(error,16));
    for (int x = 0; x <= 100; x++) {
        final long x2 = 0x0FFFFFFFFL - onepct*x;
        final long relativeFactor = (long) x2 & 0xFFFFFFFF;
        System.out.println("x=" + x + ", x2=" + x2 + ", rf=" + Long.toString(relativeFactor, 16));
    }
}

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....

onepct=28f5c28,error=5f
x=0, x2=4294967295, rf=ffffffff
x=1, x2=4252017623, rf=fd70a3d7
x=2, x2=4209067951, rf=fae147af
x=3, x2=4166118279, rf=f851eb87
x=4, x2=4123168607, rf=f5c28f5f
x=5, x2=4080218935, rf=f3333337
x=6, x2=4037269263, rf=f0a3d70f
x=7, x2=3994319591, rf=ee147ae7
x=8, x2=3951369919, rf=eb851ebf
x=9, x2=3908420247, rf=e8f5c297
x=10, x2=3865470575, rf=e666666f
x=11, x2=3822520903, rf=e3d70a47
x=12, x2=3779571231, rf=e147ae1f
x=13, x2=3736621559, rf=deb851f7
x=14, x2=3693671887, rf=dc28f5cf
x=15, x2=3650722215, rf=d99999a7
x=16, x2=3607772543, rf=d70a3d7f
x=17, x2=3564822871, rf=d47ae157
x=18, x2=3521873199, rf=d1eb852f
x=19, x2=3478923527, rf=cf5c2907
x=20, x2=3435973855, rf=ccccccdf
x=21, x2=3393024183, rf=ca3d70b7
x=22, x2=3350074511, rf=c7ae148f
x=23, x2=3307124839, rf=c51eb867
x=24, x2=3264175167, rf=c28f5c3f
x=25, x2=3221225495, rf=c0000017
x=26, x2=3178275823, rf=bd70a3ef
x=27, x2=3135326151, rf=bae147c7
x=28, x2=3092376479, rf=b851eb9f
x=29, x2=3049426807, rf=b5c28f77
x=30, x2=3006477135, rf=b333334f
x=31, x2=2963527463, rf=b0a3d727
x=32, x2=2920577791, rf=ae147aff
x=33, x2=2877628119, rf=ab851ed7
x=34, x2=2834678447, rf=a8f5c2af
x=35, x2=2791728775, rf=a6666687
x=36, x2=2748779103, rf=a3d70a5f
x=37, x2=2705829431, rf=a147ae37
x=38, x2=2662879759, rf=9eb8520f
x=39, x2=2619930087, rf=9c28f5e7
x=40, x2=2576980415, rf=999999bf
x=41, x2=2534030743, rf=970a3d97
x=42, x2=2491081071, rf=947ae16f
x=43, x2=2448131399, rf=91eb8547
x=44, x2=2405181727, rf=8f5c291f
x=45, x2=2362232055, rf=8cccccf7
x=46, x2=2319282383, rf=8a3d70cf
x=47, x2=2276332711, rf=87ae14a7
x=48, x2=2233383039, rf=851eb87f
x=49, x2=2190433367, rf=828f5c57
x=50, x2=2147483695, rf=8000002f
x=51, x2=2104534023, rf=7d70a407
x=52, x2=2061584351, rf=7ae147df
x=53, x2=2018634679, rf=7851ebb7
x=54, x2=1975685007, rf=75c28f8f
x=55, x2=1932735335, rf=73333367
x=56, x2=1889785663, rf=70a3d73f
x=57, x2=1846835991, rf=6e147b17
x=58, x2=1803886319, rf=6b851eef
x=59, x2=1760936647, rf=68f5c2c7
x=60, x2=1717986975, rf=6666669f
x=61, x2=1675037303, rf=63d70a77
x=62, x2=1632087631, rf=6147ae4f
x=63, x2=1589137959, rf=5eb85227
x=64, x2=1546188287, rf=5c28f5ff
x=65, x2=1503238615, rf=599999d7
x=66, x2=1460288943, rf=570a3daf
x=67, x2=1417339271, rf=547ae187
x=68, x2=1374389599, rf=51eb855f
x=69, x2=1331439927, rf=4f5c2937
x=70, x2=1288490255, rf=4ccccd0f
x=71, x2=1245540583, rf=4a3d70e7
x=72, x2=1202590911, rf=47ae14bf
x=73, x2=1159641239, rf=451eb897
x=74, x2=1116691567, rf=428f5c6f
x=75, x2=1073741895, rf=40000047
x=76, x2=1030792223, rf=3d70a41f
x=77, x2=987842551, rf=3ae147f7
x=78, x2=944892879, rf=3851ebcf
x=79, x2=901943207, rf=35c28fa7
x=80, x2=858993535, rf=3333337f
x=81, x2=816043863, rf=30a3d757
x=82, x2=773094191, rf=2e147b2f
x=83, x2=730144519, rf=2b851f07
x=84, x2=687194847, rf=28f5c2df
x=85, x2=644245175, rf=266666b7
x=86, x2=601295503, rf=23d70a8f
x=87, x2=558345831, rf=2147ae67
x=88, x2=515396159, rf=1eb8523f
x=89, x2=472446487, rf=1c28f617
x=90, x2=429496815, rf=199999ef
x=91, x2=386547143, rf=170a3dc7
x=92, x2=343597471, rf=147ae19f
x=93, x2=300647799, rf=11eb8577
x=94, x2=257698127, rf=f5c294f
x=95, x2=214748455, rf=ccccd27
x=96, x2=171798783, rf=a3d70ff
x=97, x2=128849111, rf=7ae14d7
x=98, x2=85899439, rf=51eb8af
x=99, x2=42949767, rf=28f5c87
x=100, x2=95, rf=5f
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文