使 ImageView 具有深色透明度

Make ImageView have dark transparency

我想在图像上添加透明的黑色并使它更暗。

          <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>

我可以设置 alpha 参数,但颜色变化为白色。 我想像这样制作较暗的图像。 我如何在 xml 或 Java 代码中做到这一点。我会根据条件设置。?

谢谢。

你需要的是调色。为您的 ImageView:

应用色调
<ImageView
    ...
    app:tint="#6F000000"
    />

Easiest/Fastest 解决方案在 XML

在您的 ImageView 之上添加第二层(可以是视图,不一定是 ImageView),并具有所需的 color/alpha。 Show/Hide 需要的时候。

            <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>

尝试这第二个图像视图将设置透明颜色。根据需要调整高度。

500000 - 最后 4 位数字代表黑色,前两位数字代表您要设置的透明度。

                      <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>

您可以使用色调模式添加前景色调。

确保在前景色中添加 alpha 通道。

前景="##AARRGGBB"

AA = HEX 中的 alpha 通道 R,G,B = 红、蓝、绿。

<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"/>