简体   繁体   中英

Change Drawable Color depending on SeekBar in Android

I have an ImageView which shows a Drawable of my Resource Folder. Second i have a seekbar below the ImageView. The color of each pixel should be changed depending on the value of the seekbar. The colors of each pixel should be changed in the way, that when the seekbar has max value, the 'g' and 'b' value of each pixel should be 0, so that only the red is shown. If you lower the seekbar it should be converted back.

I think this is done by converting the drawable to a bitmap, iterating over the pixel and change their color values depending on the current seekbar progress and their current color values.

But this is just an idea. So far i was not able to implement this in a correct way. Maybe some of you can give a code example, or even have a much better solution for this. Would be glad if someone could help.

EDIT: With the help of @pskink (see comment below) i was able to implement the thing:

float colorValue = 1 - (float) seekBar.getProgress() / 100; // SeekBar has 100 as maxVal

float[] colorMatrix = {
     1, 0, 0, 0, 0,  //red
     0, colorValue, 0, 0, 0, //green
     0, 0, colorValue, 0, 0,  //blue
     0, 0, 0, 1, 0    //alpha
};

ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
backgroungImageView.setColorFilter(colorFilter);

The problem now is (obviously) that not only the original red parts stand out. Everything is red and this is also not what i want. The goal i am trying to achieve is that only the red parts are shown when the seekbar reaches max level. Would be cool if this is also possible with a ColorMatrix but i am not sure. Any suggestions? Could playing around with the Alpha Channel help?

If anyone is interested, here is how i solved the problem with pskinks help:

(SeekBar still has maxValue 100)

float pr = seekBar.getProgress() / (float) 50;

float[] colorMatrix = {
         1, 0, 0, 0, 0,  //red
         0, 1, 0, 0, 0, //green
         0, 0, 1, 0, 0,  //blue
         pr / 2, -pr, -pr, 1, 0    //alpha
};

ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
backgroungImageView.setColorFilter(colorFilter);

Just play around with the values to get some other results. So far these are the values with which i got the best results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM