具有透明背景的视图中的海拔
Elevation in a View with transparent background
我有一个带有自定义背景的项目列表。背景是一个简单的形状,带有填充白色的圆角矩形可绘制对象。所以我的观点类似于一张卡片。我可以在上面设置一个高度并且它有效。底下有个影子。
但是我想优化一下。我的 window 的背景是白色的,所以我想我可以删除 View 的背景色以减少透支。但这不是那么简单。一旦我在视图的可绘制背景中将颜色设置为透明,海拔就会停止工作并且不再绘制阴影。我尝试在视图的 bakcground 可绘制对象中使用笔触而不是纯色,但它也不起作用。大概是因为一个ViewOutline
。所以我创建了一个自定义 OutlineProvider
returns 像这样的东西:
outline.drawRoundRect(0,0,view.getWidth(), view.getHeight(), someRadius);
现在我的 View
有透明背景,阴影也可见,但有问题。它看起来不太好。在 View
的顶部,在其下方可以看到某种背景。下面是我的输出图片。左侧是具有透明背景和自定义 OutlineProvider 的视图。右边是不透明白色背景的视图。
这是一个错误吗?还是有另一种方法来实现这一目标?看起来这是一个非常简单的性能优化,但事实证明它比应该的要复杂得多。
我认为这与 Lollipop 中的一个已知问题有关 https://code.google.com/p/android/issues/detail?id=78248
Thanks for the report and repro steps - this is indeed a bug, and it
has just been fixed in an internal branch. Will be released externally
in a future release.
As a workaround, you can either set an alpha on the GradientDrawable,
or set a custom outline provider on the view casting the shadow (via
View#setOutlineProvider) to query the outline from the background and
override the alpha provided by the drawable.
The issue was that GradientDrawable was being too conservative in
reporting a 0 alpha in cases where it had a transparent fill. (See
GradientDrawable#getOutline(), for the curious)
我有一个带有自定义背景的项目列表。背景是一个简单的形状,带有填充白色的圆角矩形可绘制对象。所以我的观点类似于一张卡片。我可以在上面设置一个高度并且它有效。底下有个影子。
但是我想优化一下。我的 window 的背景是白色的,所以我想我可以删除 View 的背景色以减少透支。但这不是那么简单。一旦我在视图的可绘制背景中将颜色设置为透明,海拔就会停止工作并且不再绘制阴影。我尝试在视图的 bakcground 可绘制对象中使用笔触而不是纯色,但它也不起作用。大概是因为一个ViewOutline
。所以我创建了一个自定义 OutlineProvider
returns 像这样的东西:
outline.drawRoundRect(0,0,view.getWidth(), view.getHeight(), someRadius);
现在我的 View
有透明背景,阴影也可见,但有问题。它看起来不太好。在 View
的顶部,在其下方可以看到某种背景。下面是我的输出图片。左侧是具有透明背景和自定义 OutlineProvider 的视图。右边是不透明白色背景的视图。
这是一个错误吗?还是有另一种方法来实现这一目标?看起来这是一个非常简单的性能优化,但事实证明它比应该的要复杂得多。
我认为这与 Lollipop 中的一个已知问题有关 https://code.google.com/p/android/issues/detail?id=78248
Thanks for the report and repro steps - this is indeed a bug, and it has just been fixed in an internal branch. Will be released externally in a future release.
As a workaround, you can either set an alpha on the GradientDrawable, or set a custom outline provider on the view casting the shadow (via View#setOutlineProvider) to query the outline from the background and override the alpha provided by the drawable.
The issue was that GradientDrawable was being too conservative in reporting a 0 alpha in cases where it had a transparent fill. (See GradientDrawable#getOutline(), for the curious)