Android Drop Shadow on View(视图上的 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 投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:视图上的 Android 投影
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
