Make ImageView have dark transparency
Asked Answered
E

4

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.

Edmee answered 4/10, 2017 at 13:2 Comment(2)
You can create a darker shape and use as background on a FrameLayout .Wesle
Or you can google for android colormatrix darkenMogilev
F
48

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

<ImageView
    ...
    app:tint="#6F000000"
    />
Faitour answered 4/10, 2017 at 13:8 Comment(1)
This didn't work for me without adding android:tintMode="src_atop".Analyst
G
7

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"/>
Griseofulvin answered 19/6, 2021 at 14:56 Comment(0)
L
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>
Lampley answered 4/10, 2017 at 13:8 Comment(0)
S
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>
Spoof answered 4/10, 2017 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.