как установить прозрачный цвет динамически в Android?Как 10% от 20% до 100% - PullRequest
0 голосов
/ 28 мая 2018

Я знаю, как установить статический цвет, например # 1Abf6116

    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

код цвета = = * # bf6116"Я хочу установить цвет, как если быпользователь установил 10 , чем цвет # 1Abf6116 я хочу получить значение, подобное 1a , если пользователь установил 10% , если пользователь установил 20%, чеммне нужно 33

Ответы [ 2 ]

0 голосов
/ 28 мая 2018

Попробуйте это

//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));
0 голосов
/ 28 мая 2018

Затем вы можете создать карту:

//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 из Java 10, вы можете использовать обычную карту и поместить входные данные

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...