【问题标题】:Add gradient to imageview向图像视图添加渐变
【发布时间】:2014-07-22 09:15:22
【问题描述】:

我想在我的图像底部添加一个渐变。像这样:

我尝试过这样的事情,但我只得到渐变没有图像..

    <ImageView
    android:id="@+id/trendingImageView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/trend_donald_sterling"
    android:src="@drawable/trending_gradient_shape"
  />

trending_gradient_shape:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="@android:color/darker_gray"
        android:startColor="@android:color/darker_gray" />

    <corners android:radius="0dp" />

</shape>

【问题讨论】:

    标签: android android-imageview android-drawable


    【解决方案1】:

    您需要两层:ImageViewView 在其上,渐变为android:background。将这两个Views 放入FrameLayout

    <FrameLayout
        ... >
    
        <ImageView
            ...
            android:src="@drawable/trend_donald_sterling" />
    
        <View
            ...
            android:background="@drawable/trending_gradient_shape"/>
    
    
    </FrameLayout>
    

    【讨论】:

    • 不要忘记为您的颜色添加一个 alpha 值。尝试从android:endColor="#00000000" android:startColor="ff000000" 开始,然后根据您的需要进行调整。
    • UI 需要做的工作太多。就个人而言,我更喜欢使用单个 ImageView 的解决方案。我没有做数学,但似乎在性能上更有效
    【解决方案2】:

    只需在您的 gardient.xml 中设置 alpha 值:

    你的图像视图:

    android:background="@drawable/trend_donald_sterling"
    android:src="@drawable/trending_gradient_shape"
    

    你的渐变 xml 文件:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    
    <gradient
        android:angle="90"
        android:endColor="#00ffffff"
        android:startColor="#aa000000"
        android:centerColor="#00ffffff" />
    
    <corners android:radius="0dp" />
    </shape>
    

    在颜色值中,#后面的前两位对应的是alpha值,其余的是R G B格式的实际颜色值,各两位。

    【讨论】:

      【解决方案3】:

      尝试在图像视图中使用“前景”属性

      <ImageView
              ...
              android:src="@drawable/trend_donald_sterling"
              android:foreground="@drawable/trending_gradient_shape" />
      

      它对我有用。

      【讨论】:

      • 这适用于我的目的,但如果您只希望渐变上升到某个高度,则在 ImageView 顶部有一个视图更有意义。
      • 注意:属性android:foreground 对低于 23 的 API 级别没有影响(当前最小值为 16)。这就是 IDE 向我展示的内容。
      【解决方案4】:

      使用android:foreground="..." 而不是android:background="..."

      现在您不需要将 ImageView 和 View 放在 FrameLayout 中!

      所以你的最终代码将是:

      图像视图

      <ImageView
          ...
          android:foreground="@drawable/trend_donald_sterling"/>
      

      可绘制

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle" >
      
          <gradient
              android:angle="90"
              android:endColor="#00ffffff"
              android:startColor="#aa000000"
              android:centerColor="#00ffffff" />
      
          <corners android:radius="0dp" />
      </shape>
      

      【讨论】:

      • 注意:属性 android:foreground 对低于 23 的 API 级别没有影响(当前最小值为 16)。这就是 IDE 向我展示的内容。
      • 正如您将在我的回答中看到的那样,我使用的是android:background 而不是android:foreground。有关 API 级别警告的问题,请参阅另一个 SO question。
      • 但是出于某种原因你写了:Use android:foreground="..." instead of android:background="..."
      • 糟糕,那是我的错。在这种情况下,您可以将您的 minSdkVersion 更改为 22 以上或按照其他需要 FrameLayout 的问题进行操作
      【解决方案5】:

      这就是我要做的, 我使用相对布局作为我的父布局,使用以下代码

       <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent">
              <ImageView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:scaleType="centerCrop"
                  android:src="@drawable/img_sample"/>
              <View
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@drawable/gradiant"/>
              <LinearLayout
                  android:layout_marginLeft="10dp"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                  android:weightSum="1">
                  <View
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight="0.55"/>
                  <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="0dp"
                      android:layout_weight="0.25"
                      android:text="Events"
                      android:gravity="bottom"
                      android:textStyle="bold"
                      android:textSize="18sp"
                      android:textColor="#ffffff"/>
                  <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="0dp"
                      android:layout_weight="0.25"
                      android:text="Some description about the events goes here"
                      android:textSize="14sp"
                      android:textColor="#ffffff"/>
              </LinearLayout>
          </RelativeLayout>
      

      希望你能弄清楚,我在下面附上我的渐变代码。在可绘制文件夹中使用它....

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >
      
      <gradient
          android:angle="90"
          android:endColor="#00ffffff"
          android:startColor="#aa000000"
          android:centerColor="#00ffffff" />
      
      <corners android:radius="0dp" />
      </shape>
      

      【讨论】:

        【解决方案6】:
        **1> Create file black_shadow.xml**
        
        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <shape>
                    <gradient
                        android:angle="270"
                        android:startColor="#00000000"
                        android:centerColor="#9c000000"
                        android:endColor="#000000"
                        android:type="linear" />
        
                </shape>
            </item>bla
        </selector>
            
        **2> Just add below line in Imageview.**
        
          android:foreground="@drawable/black_shadow"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多