Skip to content
Advertisement

How to set filter property in ImageView Android studio

I have a yellow sparkline png and I would like to change the filter by adding some css. Colors will change according to positive or negative number.

So, if the number is negative, the line should be:

CSS

JavaScript

If positive:

JavaScript

ImageView:

JavaScript

MainActivity.Java

JavaScript

EDIT:

What I have:

enter image description here

What I want:

enter image description here

EDIT:

Now I used an ImageFilterView:

JavaScript

But I still need to add the hue-rotate(300deg) I couldn’t find anything yet to change the color.

enter image description here

Advertisement

Answer

The HSB (Hue-Saturation-Brightness) Model is also alternatively known as HSV where V stands for Value. More details about HSB colour Model with a graphical description and the Hue Wheel can be found on this Medium.

To apply an HSV Color filter on an ImageView you can use the method:

public static int HSVToColor (float[] hsv)

Convert HSV components to an ARGB color. Alpha set to 0xFF.

  • hsv[0] is Hue ([0..360])
  • hsv[1] is Saturation ([0…1])
  • hsv[2] is Value ([0…1])

If hsv values are out of range, they are pinned.

All you need is to create this float[] array with the above ranges and apply it into ImageView like below:

JavaScript

Result based on your 1st (yellow image) before and after applying the color Filter (green image) will be:

orange_line

green_line

If we pick a pixel sample from the attached Red/Green images the RGB colors are for Red:R(209)G(63)B(84) and for Green:R(69)G(186)B(116). Now if we convert these RGB values to HSV values using an RGB to HSV color conversion tool we get the below:

Red

RGB: R(209)G(63)B(84) -> HSV: H(351deg)S(69.9%)V(82%)

In code:

JavaScript

Result:

red_line

Green

RGB: R(69)G(186)B(116) -> HSV: H(144deg)S(62.9%)V(72.9%)

In code:

JavaScript

Result:

green_line

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement