简体   繁体   English

根据Android中的SeekBar更改可绘制颜色

[英]Change Drawable Color depending on SeekBar in Android

I have an ImageView which shows a Drawable of my Resource Folder. 我有一个ImageView,它显示我的资源文件夹的Drawable。 Second i have a seekbar below the ImageView. 其次,我在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. 每个像素的颜色应以如下方式更改:当搜寻栏具有最大值时,每个像素的'g'和'b'值应为0,以便仅显示红色。 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: 编辑:在@pskink的帮助下(请参见下面的评论),我能够实现该功能:

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. 如果使用ColorMatrix也可以这样做会很酷,但我不确定。 Any suggestions? 有什么建议么? Could playing around with the Alpha Channel help? 可以使用Alpha Channel帮助吗?

If anyone is interested, here is how i solved the problem with pskinks help: 如果有人感兴趣,这是我如何通过pskinks帮助解决问题的方法:

(SeekBar still has maxValue 100) (SeekBar仍然具有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. 到目前为止,这些都是我获得最佳结果的价值观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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