Change the color of a background image on Android

2

I have a background image in a textview, I need the color of this image to be dynamically changed, I tried changing it using setColorFilter, however, it changes the color of the entire image, I can not change the border color where the arrow is pointing .

How do I change just the green, without changing that border that the arrow is pointing (darkest part)?

How I tried:

setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN)

I tried using all PorterDuff.Mode that android provides ...

    
asked by anonymous 18.04.2018 / 17:33

1 answer

2

You can not do this with images, you can do with xml (vector files).

For this, in your case, your vector must be divided into 2 parts ... The one with the largest area and this folding ...

Example:

<vector
    android:height="100dp"
    android:viewportHeight="512"
    android:viewportWidth="512"
    android:width="100dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

<group
    android:name="group1" >

    <path
        android:name="right_top"
        android:fillColor="#1EA1E3"
        android:pathData="M421,226c0,-24.899 -20.098,-45 -45,-45s-45,20.101 
       -45,45c0,11.4 4.2,21.901 11.4,30h-83.588h-0.938H256v-86.4c-8.101,7.2 
      -18.6,11.4 -30,11.4c-24.902,0 -45,-20.099 -45,-45s20.098,-45 
       45,-45c11.4,0 
       21.899,4.2 30,11.4V0h71.25H467c24.814,0 45,20.186 
       45,45v211H409.6C416.8,247.901 421,237.4 421,226z"/>

    <path
        android:name="left_top"
        android:fillColor="#FFDA45"
        android:pathData="M256,256l-42.188,39.375L149.124,353.5l- 
        83.5,1.875L0,256V45C0,20.186 20.186,0 
        45,0h211l40.313,33.75v136.938L256,256z"/>

</group>

<path
    android:name="right_bottom"
    android:fillColor="#355382"
    android:pathData="M467,512H256l-36.563,-40.313l- 
    1.875,-160.375L256,256l80.624,-104.063l136,-8.438L512,
    256v211C512,491.814 
    491.814,512 467,512z"/>

<path
    android:name="left_bottom"
    android:fillColor="#CF0404"
    android:pathData="M91,286c0,24.901 20.098,45 45,45s45,-20.099 
    45,-45c0,-11.4 -4.2,-21.899 -11.4,-30H256v86.4c8.101,-7.2 18.6,-11.4 
    30,-11.4c24.899,0 45,20.099 45,45s-20.101,45 -45,45c-11.4,0 -21.899,-4.2 
    -30,-11.4V512H45c-24.814,0 -45,-20.186 -45,-45V256h102.4C95.2,264.101 
    91,274.6 91,286z"/>

</vector>

You can see the example here:

link

    
20.04.2018 / 16:28