1

In my app I want to set color for an ImageView with alpha applied on the color. I've tried to set the color in the colors.xml file in HEX-format (my color #89000000 is a dimmed black color - 89 in hex means 137 in dec, it's about 54% of FF or 255), then I've applied it on my vector asset I would like to use. It looks like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="200dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="@color/dimmed_black"
        android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z" />

It looked nice. But then I've tried something different. I set vector asset's fill color to black, and did the same thing programatically in the code you can see below:

//Field of my Activity
private static final int NORMAL_ALPHA = (255 / 100) * 54; // 100% = 255

//In my Activity's onCreate() method
imageView.getRightBitmap().setAlpha(NORMAL_ALPHA);

Then I've created two screenshots: one when XML-based color was applied and one when code was applied. Here they are:

enter image description here

The difference is visible with bare eyes, and I think that a less than half percent difference is not so big to do this. I've checked every part of my layout searching for any "alpha" attributes which could cause any difference but there isn't any. Did anyone else experience the same before? What's the cause? Is there any way to solve it?

Geryson
  • 647
  • 2
  • 8
  • 23

2 Answers2

1

It's because the one calculated by code is not 54%, but it's about 42%.

You defined the calculation in integer.

private static final int NORMAL_ALPHA = (255 / 100) * 54;

So 255/100 = 2

2 * 54 = 108 that is 42% (108/255 = 0.42)

Try using:

private static final int NORMAL_ALPHA = 255 * 54 / 100;
ARLabs
  • 1,118
  • 1
  • 10
  • 23
  • I've tested this behavior in a programming calculator recently, by following your steps. Your results appeared. Not able currently to test it in Android API, but makes sense :) – Geryson Mar 27 '20 at 23:28
  • @Geryson. I answered 22 months after your question, and you commented 18 months after my answer... you are faster than me!!! :-D – ARLabs Apr 22 '20 at 14:39
0

may be this link will help you to calculate alpha with transprancy.

How to make a background 20% transparent on Android

Heena M. Patel
  • 144
  • 1
  • 7
  • As I've stated on the image of my question the values are calculated the same way you just linked. – Geryson Jun 01 '17 at 12:38