视图上的 Android 投影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3567312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:46:48  来源:igfitidea点击:

Android Drop Shadow on View

androidxmlimageviewshadowdropshadow

提问by coneybeare

I have done some extensive searching for code examples on this but cannot find anything.

我已经对此进行了一些广泛的代码示例搜索,但找不到任何内容。

In particular, I am looking to add a shadow to a png drawable I am using in an ImageView. This png drawable is a rounded rect with transparent corners.

特别是,我希望为我在 ImageView 中使用的 png drawable 添加阴影。这个 png drawable 是一个带有透明角的圆角矩形。

Can somebody please provide a code example of how to add a decent drop shadow to a view either in code or XML?

有人可以提供一个代码示例,说明如何在代码或 XML 中向视图添加合适的阴影吗?

采纳答案by Josh

You could use a combination of Bitmap.extractAlpha and a BlurMaskFilter to manually create a drop shadow for any image you need to display, but that would only work if your image is only loaded/displayed once in a while, since the process is expensive.

您可以使用 Bitmap.extractAlpha 和 BlurMaskFilter 的组合为您需要显示的任何图像手动创建阴影,但这仅在您的图像仅偶尔加载/显示时才有效,因为该过程很昂贵。

Pseudo-code (might even compile!):

伪代码(甚至可以编译!):

BlurMaskFilter blurFilter = new BlurMaskFilter(5, BlurMaskFilter.Blur.OUTER);
Paint shadowPaint = new Paint();
shadowPaint.setMaskFilter(blurFilter);

int[] offsetXY = new int[2];
Bitmap shadowImage = originalBitmap.extractAlpha(shadowPaint, offsetXY);

/* Might need to convert shadowImage from 8-bit to ARGB here, can't remember. */

Canvas c = new Canvas(shadowImage);
c.drawBitmap(originalBitmap, offsetXY[0], offsetXY[1], null);

Then put shadowImage into your ImageView. If this image never changes but is display a lot, you could create it and cache it in onCreate to bypass the expensive image processing.

然后将 shadowImage 放入您的 ImageView。如果此图像从未更改但显示很多,您可以创建它并将其缓存在 onCreate 中以绕过昂贵的图像处理。

Even if that doesn't work as is, it should be enough to get you going in the right direction.

即使这不起作用,它也应该足以让您朝着正确的方向前进。

回答by om252345

For Drop shadow use below code

对于投影使用下面的代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <gradient
    android:startColor="#ffffff"
    android:centerColor="#d3d7cf"
    android:endColor="#2e3436"
    android:angle="90" />
</shape>

Use above drawable for a background of a view

使用上面的 drawable 作为视图的背景

<View 
    android:id="@+id/divider" 
    android:background="@drawable/black_white_gradient"
    android:layout_width="match_parent" 
    android:layout_height="10sp"
    android:layout_below="@+id/buildingsList"/>

回答by PeterE

This helped me to get the shadow working so I wanted to share the working code:

这帮助我让影子工作,所以我想分享工作代码:

private Bitmap createShadowBitmap(Bitmap originalBitmap) {
    BlurMaskFilter blurFilter = new BlurMaskFilter(5, BlurMaskFilter.Blur.OUTER);
    Paint shadowPaint = new Paint();
    shadowPaint.setMaskFilter(blurFilter);

    int[] offsetXY = new int[2];
    Bitmap shadowImage = originalBitmap.extractAlpha(shadowPaint, offsetXY);

    /* Need to convert shadowImage from 8-bit to ARGB here. */
    Bitmap shadowImage32 = shadowImage.copy(Bitmap.Config.ARGB_8888, true);
    Canvas c = new Canvas(shadowImage32);
    c.drawBitmap(originalBitmap, -offsetXY[0], -offsetXY[1], null);

    return shadowImage32;
}

回答by Artyom

For API 21(5.0)+add android:elevation="4dp"or android:translationZ="4dp"to view description. Documentation

对于 API 21(5.0)+添加android:elevation="4dp"android:translationZ="4dp"查看说明。文档

Elevation Attribute

高程属性

回答by Pradeep Kumar Kushwaha

Always use Transparent shadow such that they could stick to any color.

始终使用透明阴影,以便它们可以粘在任何颜色上。

shadow.xml

shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <gradient
    android:startColor="#002e3436"
    android:endColor="#992e3436"
    android:angle="90" />
</shape>

And in View

在视图中

<View 
    android:id="@+id/divider" 
    android:background="@drawable/shadow"
    android:layout_width="match_parent" 
    android:layout_height="5dp"/>