Add gradient to imageview

守給你的承諾、 提交于 2019-11-28 04:20:41

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

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.

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.

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>

this is how im gonna do, i used relative layout as my parent layout, use the following code

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

hope you can figure out, here i attach my gradiant code below.use it inside the drawable folder....

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!