theblitz
theblitz

Reputation: 6881

Change image fill color in Android

I have an ImageView with the source being an ImageAsset.

My image is a circle with a plus and I am looking to colour the inside of the circle only. How do I do that?

Using setBackgroundColor colours the whole of the view thus giving a square background like this:

enter image description here.

Upvotes: 0

Views: 4067

Answers (1)

A. AMAIDI
A. AMAIDI

Reputation: 6849

You can apply a color tint to your ImageView. This will not affect your background if it's transparent, just the colored part (which is the circle and the plus)

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), 
                         android.graphics.PorterDuff.Mode.MULTIPLY);

Update
Using android.graphics.PorterDuff.Mode.SRC_IN will also work (if you don't need to multiply the source and destination pixels)

Update 2
Since the destination part is not colored than the best solution is to use VectorDrawable.

You can use VectorChildFinder to find inner parts of your SVG resource and change its color.

VectorChildFinder vector = new VectorChildFinder(this, R.drawable.my_vector, imageView);

VectorDrawableCompat.VFullPath path1 = vector.findPathByName("path1");
path1.setFillColor(Color.RED);

imageView.invalidate();

to create your SVG, follow these steps :

  1. Just click right button on folder(drawable for ex.) and choose:

enter image description here

  1. then choose:

enter image description here

Upvotes: 1

Related Questions