【问题标题】:Make ImageView have dark transparency使 ImageView 具有暗透明度
【发布时间】:2017-10-04 13:02:20
【问题描述】:

我想在图像顶部添加透明的黑色并使其更暗。

          <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 代码中做到这一点。我会根据情况设置。?

谢谢。

【问题讨论】:

  • 您可以创建一个较暗的shape 并用作FrameLayout 的背景。
  • 或者你可以googleandroid colormatrix darken

标签: android image android-layout android-imageview android-view


【解决方案1】:

您需要的是所谓的着色。为您的ImageView 应用颜色:

<ImageView
    ...
    app:tint="#6F000000"
    />

【讨论】:

    【解决方案2】:

    最简单/最快的解决方案是 XML

    在您的 ImageView 之上添加第二层(可以是 View,不一定是 ImageView),具有所需的颜色/alpha。在需要时显示/隐藏它。

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

    【讨论】:

      【解决方案3】:

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

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

      foreground="##AARRGGBB"

      AA = 十六进制的 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"/>
      

      【讨论】:

        【解决方案4】:

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

        500000 - 后 4 位代表黑色,前两位代表您要设置的 alpha。

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

        【讨论】:

          猜你喜欢
          • 2015-12-18
          • 2012-02-05
          • 1970-01-01
          • 2016-04-05
          • 1970-01-01
          • 1970-01-01
          • 2015-07-05
          • 2011-07-05
          • 1970-01-01
          相关资源
          最近更新 更多