Android ImageView投影

我正在尝试向 ImageView添加投影.另一个Stackoverflow答案似乎是使用画布和位图等,比它需要的方式更复杂.

在iOS上我会做这样的事情:

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

无论是否在视图,图像或文本上应用阴影,它都会渲染阴影.

我试图在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,5,Color.argb(255,255,0));
birdImageView.setLayerPaint(paint);

我的鸟图像上根本没有看到任何预期的红色阴影.

难道我做错了什么?

假设我想要这样的投影:

更新

我是否必须使用Android 5.0和新的Elevation api(http://developer.android.com/training/material/shadows-clipping.html)?

但如果要使用新的API,那么根据当前的人口统计数据(http://www.droid-life.com/2016/02/02/android-distribution-february-2016/),超过50%的用户将无法使用该应用.

T_T

解决方法

在android studio中有build drawable,你可以使用它来将阴影应用到任何View.这类似于投影.
android:background="@drawable/abc_menu_dropdown_panel_holo_light"

使用此功能,您无法更改视图的背景颜色.它的边框颜色.如果您想要自己的自定义drawable,请使用layer-list

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,here i preferred solid white -->
        <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"

希望它能帮到你!
谢谢Android View shadow

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...