Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add gradient to imageview

I want to add a gradient on the bottom of my image . Something like this :

enter image description here

I tried something like this but I only get the gradient no image..

    <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>
like image 874
user1163234 Avatar asked Jun 02 '14 09:06

user1163234


4 Answers

You need two layers: An ImageView, and a View on top of that with your gradient as android:background. Put these two Views in a FrameLayout:

<FrameLayout
    ... >

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

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


</FrameLayout>
like image 141
nhaarman Avatar answered Nov 10 '22 17:11

nhaarman


Simply set the alpha value in your gardient.xml:

Your imageView:

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

Your gradient xml file:

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

In the color value, the first two places after # correspond to the alpha value, while the rest are the actual color value in R G B format, two for each.

like image 23
Kaustuv Avatar answered Nov 10 '22 19:11

Kaustuv


try using the "foreground" attribute in your imageview

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

it worked for me.

like image 30
swetabh suman Avatar answered Nov 10 '22 17:11

swetabh suman


Use android:foreground="..." instead of android:background="..."

Now you won't need to put ImageView and View inside a FrameLayout!

So your final code will be:

ImageView

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

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>
like image 12
Slick Slime Avatar answered Nov 10 '22 19:11

Slick Slime