视图上的 Android 投影 [英] Android Drop Shadow on View

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

问题描述

我已经对这方面的代码示例进行了广泛的搜索,但找不到任何东西.

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

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

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.

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

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

推荐答案

您可以结合使用 Bitmap.extractAlpha 和 BlurMaskFilter 为您需要显示的任何图像手动创建阴影,但这仅适用于您的图像只偶尔加载/显示一次,因为这个过程很昂贵.

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.

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

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);

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

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.

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

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

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

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