向imageview添加渐变

时间:2014-06-02 09:33:56

标签: android android-imageview android-drawable

我想在图片底部添加渐变。像这样:

enter image description here

我试过这样的东西,但我只得到渐变没有图像..

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

6 个答案:

答案 0 :(得分:49)

您需要两个图层:一个ImageView和一个View,其渐变为android:background。将这两个View放在FrameLayout

<FrameLayout
    ... >

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

    <View
        ...
        android:background="@drawable/trending_gradient_shape"/>


</FrameLayout>

答案 1 :(得分:28)

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

您的imageView:

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格式的实际颜色值,每个两个。

答案 2 :(得分:20)

尝试使用imageview中的“foreground”属性

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

它对我有用。

答案 3 :(得分:7)

使用android:foreground="..."代替android:background="..."

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

所以您的最终代码将是:

ImageView的

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

答案 4 :(得分:3)

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

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

希望你能弄明白,我在这里附上我的渐变代码。在drawable文件夹中使用它....

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

答案 5 :(得分:0)

**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"
相关问题