Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two gradients(top-bottom and bottom-top) in a single image view in android?

Tags:

android

I am trying to add gradient to an imageview on both top and bottom side of the image. I dont want to add a textview on top of image view. How do I implement it?

like image 729
Pavithran K Avatar asked Dec 05 '25 15:12

Pavithran K


1 Answers

It appears that your simple and clean solution would be to wrap ImageView with FrameLayout and use foreground property as follows:

Layout

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foreground="@drawable/gradient">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/male"/>
</FrameLayout>

And gradient just in case

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient
        android:startColor="#33000000"
        android:centerColor="@android:color/transparent"
        android:endColor="#33000000"
        android:angle="90"/>
</shape>

Produces this:

enter image description here

A FrameLayout is relatively simple structure which will not add complexity to your view hierarchy, as opposed to RelativeLayout for example, which will cause extra measurements.

If you absolutely have to avoid adding any extra wrapper views, then your next option would be creating a CustomView, extending from ImageView and overriding onDraw() method and manually covering your existing canvas with pixels from gradient bitmap.

like image 53
dkarmazi Avatar answered Dec 08 '25 06:12

dkarmazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!