3

I am grabbing a few material design icons from here:

https://google.github.io/material-design-icons/#getting-icons

And I need a number of them to also be a little transparent 'gray'/'muted'.

I tried to create a drawable bitmap to define the alpha:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_info_outline_black_24dp"
    android:alpha=".7">
</bitmap>

However this does not work.

Is the right answer to create multiple pngs with different levels of transparency? If so, any worries about apps getting unnecessarily big?

lostintranslation
  • 23,756
  • 50
  • 159
  • 262

1 Answers1

1

you can make dynamically using setColorFilter

private Drawable changeColor() {

    try {
        Drawable image;

        int imageId = R.drawable.ic_folder_black;
        int color = Color.parseColor("#A6000000");

        image = getResources().getDrawable(imageId, getTheme());


        image.setColorFilter(color, PorterDuff.Mode.SRC_IN);

        return image;
    } catch (IllegalStateException e) {

    }
    return null;
}

And then call the method on an ImageView like this

imageView.setImageDrawable(changeColor());

On the color the A6 is the alpha and it's like 65% of transparency. Below is the list with the transparency levels and their hex value. I got the list from here.

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

** EDIT **

We set the filter as PorterDuff.Mode.SRC_IN because it's the only way to change a black image on any other color you wish. For images with different colors use PorterDuff.Mode.MULTIPLY

Community
  • 1
  • 1
Vasileios Pallas
  • 4,801
  • 4
  • 33
  • 50
  • Thanks. At this point not sure what is better. Dynamically modifying in code like this or including multiple resources that have been modified alphas. – lostintranslation Dec 06 '15 at 15:40
  • By including duplicate images with different colors you will make your app very big. Let's take an instance of a scenario that you will need for your app almost 60 images. If you want the half of them with different color or transparency you will have almost 90+ images! On the other hand if you are afraid about the speed of your app use the logic I told you above on a AsyncTask class. – Vasileios Pallas Dec 06 '15 at 15:55