Change the color of a background image on Android

Asked

Viewed 354 times

2

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

inserir a descrição da imagem aqui

How do I change only the green, without changing that edge the arrow is pointing at (darker part)?

How I tried:

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

I tried to use all Porterduff.Mode that the android provides...

  • I don’t think you can do that. There’s no way to modify a location specifically.

1 answer

2


You can’t do this with images, you can do it with xml (vector files).

For this, in your case, your vector needs to be divided into 2 parts... 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:

https://github.com/devsideal/VectorChildFinder

Browser other questions tagged

You are not signed in. Login or sign up in order to post.