Android ImageView投影 [英] Android ImageView drop shadow

查看:442
本文介绍了Android ImageView投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add a drop shadow to an ImageView. The other Stackoverflow answer seems to be using canvas and bitmaps etc, way more complicated than it needs to be.

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

On iOS I would do something like this:

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.

我试图在Android上做同样的事情,但它只是拒绝工作:

I've tried to do the same on Android but it just refuses to work:

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.

我做错了什么?

让我们说我想要这样的投影:

Let's say I want the drop shadow like this:

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

Do I have to resort to Android 5.0 and the new 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%的用户将无法使用该应用。

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.

T_T

推荐答案

在android studio中有构建 drawable 可用于将阴影应用于任何查看。这类似于投影。

In android studio there is in build drawable that you can use for apply shadow to any View. That is similar like a drop shadow.

android:background="@drawable/abc_menu_dropdown_panel_holo_light"

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

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


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, 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"

希望它能为您提供帮助!

Hope it will help you!

这篇关于Android ImageView投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆