Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add semi-transparent overlay on imageview

How can I add semi-transparent overlay on imageview?

enter image description here

change it to

enter image description here

like image 985
Android Guy Avatar asked Nov 07 '17 06:11

Android Guy


People also ask

How do I overlay one image over another Android?

Tap Edit and choose an image from your Photo Library or Stock Images. Scroll through the bottom menu and tap on Overlays.

Can ImageView display video?

You can add ImageView and VideoView in RelativeLayout and set ImageView to invisible and VideoView to visible and vice-versa and you can play video on onClick.


2 Answers

A simpler solution could be to use a ColorFilter on the ImageView with PorterDuff.Mode.SRC_OVER

ImageView.setColorFilter(ContextCompat.getColor(
        context,
        R.color.black_opacity_200),
        PorterDuff.Mode.SRC_OVER
);

If you want to get rid of the overlay simply clear it

ImageView.clearColorFilter()
like image 99
dell116 Avatar answered Oct 05 '22 13:10

dell116


for me, the selected answer didn't worked.But I got it using layer-list..All you got to do is to create a filter file(having your topmost color), a layer-drawable file(joining your filtercolor and image) and use this layer-drawable in your image view.
filter.xml

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

    <gradient
        android:startColor="#cf64b5f6"
        android:endColor="#cf1E88E5"
        android:angle="270"
        />

</shape>
  • layerlist_drawable.xml

    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/my_img" />
        <item android:drawable="@drawable/filter" />
    </layer-list>
    
  • main_activity.xml :

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:scaleType="centerCrop"
        android:src="@drawable/layerlist_drawable"
        />
    

like image 31
ansh sachdeva Avatar answered Oct 05 '22 14:10

ansh sachdeva