首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在android中动态设置透明颜色?比如10% ~ 20%到100%

如何在android中动态设置透明颜色?比如10% ~ 20%到100%
EN

Stack Overflow用户
提问于 2018-05-28 19:29:47
回答 2查看 68关注 0票数 -2
代码语言:javascript
复制
    100% — FF
    95% — F2
    90% — E6
    85% — D9
    80% — CC
    75% — BF
    70% — B3
    65% — A6
    60% — 99
    55% — 8C
    50% — 80
    45% — 73
    40% — 66
    35% — 59
    30% — 4D
    25% — 40
    20% — 33
    15% — 26
    10% — 1A
    5% — 0D
    0% — 00
EN

回答 2

Stack Overflow用户

发布于 2018-05-28 20:04:05

尝尝这个

代码语言:javascript
复制
//simple way to transparency set dynamicly any View 
TextView tv = findViewById(R.id.tv);
String has = "#";
String PR_transparency = "50";// this text background color 50% transparent;
String og_color = "FF001A";

tv.setBackgroundColor(Color.parseColor(has+PR_transparency+og_color));
票数 1
EN

Stack Overflow用户

发布于 2018-05-28 19:38:40

您可以创建Map,然后:

代码语言:javascript
复制
//Initial color
String color = "#bf6116";

//Create a map which hold percent and its corresponding String
Map<Integer, String> mapPercent = Map.of(100, "FF", 95, "F2", 10, "1A");

//user input, to explain I use a static variable
int userInput = 10;
if(mapPercent.containsKey(userInput)) {
    //Use replaceFirst or subscring to put the corresponding String
    color = color.replaceFirst(
        "#(.*)", //the regex match # as group 1 and the rest as group 2
        //put the corresponding string between the two groups
        String.format("$0%s$1", mapPercent.get(userInput)) 
    );
}


System.out.println(color);//result in this case is #1Abf6116

注意:Map.of在Java10中,您可以使用普通的映射并将输入

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50565394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档