Elevation not working for ImageView

2019-03-17 09:42发布

Elevation for ImageView is not working. I declared ImageView in XML like this:

<ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:elevation="10dp"
        android:src="@drawable/youtube" />

What else should I do for elevation to work properly for ImageView?

6条回答
我想做一个坏孩纸
2楼-- · 2019-03-17 10:22

The elevation shadow is derived from the background drawable of a View. If your ImageView has no background, you'll see no shadow.

If you want to change that behavior, you need to build your own ViewOutlineProvider and call View.setOutlineProvider() to set it (and this is not trivial).

查看更多
干净又极端
3楼-- · 2019-03-17 10:22

On Lollipop it works setting elevation. For pre-Lollipop, you need to implement shadows by yourself. Support-v4 has ViewCompat.setElevation(view, value) method for by if you check the sources there is no implementation. (at least, at the time of writing).

查看更多
男人必须洒脱
4楼-- · 2019-03-17 10:31

Works in all latest versions

Use CardView as parent layout. Use android:background="@drawable/your_img" instead of android:src="@drawable/your_img" in ImageView.

<android.support.v7.widget.CardView
    android:layout_gravity="center_horizontal"        
    app:cardElevation="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView       
        android:id="@+id/image"
        android:background="@drawable/your_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null" />

</android.support.v7.widget.CardView>
查看更多
淡お忘
5楼-- · 2019-03-17 10:31

If you are using CardView, be sure to leave the CardView background transparent.

<android.support.v7.widget.CardView
                android:layout_gravity="center_horizontal"
                app:cardElevation="@dimen/cardview_default_elevation"
                app:cardBackgroundColor="@android:color/transparent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
<ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:src="@drawable/youtube" />

</android.support.v7.widget.CardView>
查看更多
乱世女痞
6楼-- · 2019-03-17 10:37

Instead of using android:src you need to change to android:background for displaying shadows.

 <ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:elevation="10dp"
    android:background="@drawable/youtube" />
查看更多
何必那么认真
7楼-- · 2019-03-17 10:42

For displaying a round Image with SHADOW with elevation attribute, I created a round drawable WITHOUT SHADOW as shown below :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval">
            <solid android:color="@android:color/white" />
        </shape>
    </item>

</layer-list>

Use this drawable as background in your ImageView as shown below :

    <ImageView
        android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round_shape"
        android:elevation="5dp"
        android:src="@drawable/center" />

This will display ImageView with shadow.

One question that might be arising in your mind is why to use xml drawable. As mentioned by other developers on various answers to this question, shadow is formed from background and a bordered background to be more specific. Xml drawable helps us in creating a bordered background from which elevation attribute can create shadow.

查看更多
登录 后发表回答