简体   繁体   English

Android ImageView 投影

[英]Android ImageView drop shadow

I'm trying to add a drop shadow to an ImageView.我正在尝试向 ImageView 添加投影。 The other Stackoverflow answer seems to be using canvas and bitmaps etc, way more complicated than it needs to be.另一个 Stackoverflow 答案似乎使用画布和位图等,比它需要的复杂得多。

On iOS I would do something like this:在 iOS 上,我会做这样的事情:

myImageView.layer.shadowColor = [UIColor redColor].CGColor;
myImageView.layer.shadowRadius = 5;
myImageView.layer.shadowOffset = CGRectMake(0, 5);

and it would render a drop shadow, regardless of whether the shadow is being applied on a view, an image or text.并且它会渲染一个阴影,无论阴影是应用于视图、图像还是文本。

I've tried to do the same on Android but it just refuses to work:我试图在 Android 上做同样的事情,但它只是拒绝工作:

birdImageView = new ImageView(context);
birdImageView.setImageResource(R.drawable.yellow_bird);

Paint paint = new Paint();
paint.setAntiAlias(true);


birdImageView.setLayerType(LAYER_TYPE_SOFTWARE, null);
paint.setShadowLayer(5, 0, 5, Color.argb(255, 255, 0, 0));
birdImageView.setLayerPaint(paint);

I don't see any expected red drop shadow on my bird image at all.我的鸟图像上根本没有看到任何预期的红色阴影。

Am I doing something wrong?难道我做错了什么?

Example例子

Let's say I want the drop shadow like this:假设我想要这样的阴影:

投影示例

Update更新

Do I have to resort to Android 5.0 and the new Elevation api ( http://developer.android.com/training/material/shadows-clipping.html ) ?我是否必须求助于 Android 5.0 和新的 Elevation api ( http://developer.android.com/training/material/shadows-clipping.html )?

But if one was to use new API, then according to current demographics ( http://www.droid-life.com/2016/02/02/android-distribution-february-2016/ ) more than 50% of users will not be able to use the app.但是如果要使用新的 API,那么根据目前的人口统计数据( http://www.droid-life.com/2016/02/02/android-distribution-february-2016/ ),超过 50% 的用户不会能够使用该应用程序。

T_T T_T

In android studio there is in build drawable that you can use for apply shadow to any View .在 android studio 中有构建drawable ,您可以使用它来将阴影应用于任何View That is similar like a drop shadow.这类似于阴影。

android:background="@drawable/abc_menu_dropdown_panel_holo_light"

Using this you can not change the background color of the view & its border color.使用它您不能更改视图的背景颜色及其边框颜色。 If you want your own custom drawable then use layer-list如果您想要自己的自定义可绘制对象,请使用layer-list

custom_drop_shadow_drawable.xml custom_drop_shadow_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--the shadow comes from here-->
    <item
        android:bottom="0dp"
        android:drawable="@android:drawable/dialog_holo_light_frame"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">

    </item>

    <item
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <!--whatever you want in the background -->
        <shape android:shape="rectangle">
            <solid android:color="@android:color/red" />

        </shape>
    </item>
</layer-list>

and apply to your view like below并适用于您的视图,如下所示

android:background="@drawable/custom_drop_shadow_drawable"

Hope it will help you!希望它会帮助你! Thanks Android View shadow感谢Android 查看阴影

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM