gpt4 book ai didi

android - 如何在 Android 上显示具有渐变淡入淡出效果的 2 个 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:34 25 4
gpt4 key购买 nike

我想在屏幕上显示 2 个 View - 一个是相机预览,在顶部,而另一个是显示图像或谷歌地图 - 并显示在屏幕底部。

不过我希望在它们之间有类似渐变的过渡 - 所以它们之间没有粗糙的边缘。有可能有这样的效果吗?

编辑:我想要实现的效果应该是这样的(顶部来自相机预览,而底部应该是一张 map ...):

Map blending into camera photo

在 iOS 上,我通过 CameraOverlay 显示 map 并将图层蒙版设置为渐变得到了类似的效果:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.map.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite: 1.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 1.0 alpha: 1.0] CGColor], nil];
gradient.startPoint = CGPointMake(0.5f, 0.0f);
gradient.endPoint = CGPointMake(0.5f, 0.5f);
self.map.layer.mask = gradient;

最佳答案

不幸的是,据我所知,如果两个组件都必须是交互式/实时的,则您不能在相机预览和 map 之间交叉淡入淡出。正如之前评论中所述,这与小部件的性质和 Android 合成的局限性有关。

相机预览需要 SurfaceView 才能正常工作。来自官方文档:

the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes.

Google map v2 也使用 SurfaceView(参见 here),所以基本上您有两个 SurfaceView 实例,一个在另一个之上,您根本做不到应用渐变蒙版以实现您想要的效果,因为您无法控制每个小部件如何自行绘制:

  • 相机预览 SurfaceView 接收相机缓冲区并本地渲染
  • Maps SurfaceView 在另一个进程中呈现。

此外,如 here 所述,强烈建议不要同时使用 SurfaceView 的两个实例:

The way surface view is implemented is that a separate surface is created and Z-ordered behind its containing window, and transparent pixels drawn into the rectangle where the SurfaceView is so you can see the surface behind. We never intended to allow for multiple surface views.

我认为您唯一的选择是只选择其中一个进行实时/交互,并将另一个作为静态图像渐变绘制在其上。


编辑

为了进一步验证我之前的陈述,下面引用官方文档about Camera usage :

Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.

因此您不得不使用 SurfaceView 以从 Camera 获取预览。永远。
重复一遍:您无法控制这些像素的渲染方式,因为 Camera 使用预览 SurfaceHolder 直接写入帧缓冲区

总而言之,您有两个完全不透明 SurfaceView 实例,您根本无法对它们的内容应用任何花哨的渲染,所以我认为这种效果在安卓。

关于android - 如何在 Android 上显示具有渐变淡入淡出效果的 2 个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16091045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com