Android的阴影上查看 [英] Android Drop Shadow on View

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

问题描述

我已经做了一些广泛的搜索查找有关此code的例子,但无法找到任何东西。

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

在特别的,我期待阴影添加到我正在使用的ImageView的一个PNG绘制。 PNG文件可绘制的是一个圆角的矩形透明的角落。

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.

有人可以请提供如何体面的阴影添加到无论是在code或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.

伪code(甚至可能编译):

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天全站免登陆