16

I want to add the transparent black color on top of the image and make it darker.

enter image description here

          <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/rest_image"
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    />
            </RelativeLayout>

I can set the alpha parameter but the color change is in white. I want to make the darker image like this. How can i do it in xml or Java code. I will set it based on condition.?

Thanks.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Karesh A
  • 1,731
  • 3
  • 22
  • 47

4 Answers4

47

What you need is called tinting. Apply a tint to your ImageView:

<ImageView
    ...
    app:tint="#6F000000"
    />
azizbekian
  • 60,783
  • 13
  • 169
  • 249
6

You can add foreground tint with tint mode.

Make sure you add alpha channel in your foreground color.

foreground="##AARRGGBB"

AA = alpha channel in HEX R,G,B = Red, Blue, Green.

<ImageView
            android:id="@+id/vidthumbnail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:foreground="#96222f3e"
            android:foregroundTintMode="src_atop"/>
Khay Leng
  • 391
  • 5
  • 8
5

Easiest/Fastest solution would be in XML

Add a second layer (can be a View, doesn't have to be an ImageView) on top of your ImageView, with the desired color/alpha. Show/Hide it when needed.

            <ImageView
                android:id="@+id/rest_image"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                />

            <View
                android:id="@+id/overlay_image"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background=“@color/yourColorWithAlpha"
                />
        </RelativeLayout>
NSimon
  • 5,212
  • 2
  • 22
  • 36
2

Try this second imageview will set the transparent color. Adjust the height according to your need.

500000 - last 4 digits stands for black color and first two stands for alpha you want to set.

                      <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
        
                        <ImageView
                            android:id="@+id/rest_image"
                            android:layout_width="match_parent"
                            android:layout_height="150dp"
                            android:adjustViewBounds="true"
                            android:scaleType="centerCrop"
                            />
    
                          <ImageView 
                            android:layout_width="match_parent"
                            android:layout_height="150dp" 
                            android:background="#500000"
                            />
                    </RelativeLayout>
Community
  • 1
  • 1
Harminder Singh
  • 316
  • 1
  • 11