我可以在自定义 canvas 上放置一个固定层吗?

Can I lay a fixed layer over a custom canvas?

在 Android Studio 中,我正在开发一款游戏,我在其中使用 SurfaceView 对象以 60fps 的速度绘制动画。我使用半透明图像作为顶层来隐藏运动场的边缘——有点 "fogs of war" 效果,这样玩家就不知道阴影之外会发生什么。

我的问题是我需要为每一帧绘制这个阴影图像,即使阴影永远不会改变或移动,即使在运动场滚动时也是如此 right/left/up/down。而且它是一个全屏图像,所以它会减慢游戏速度。

有没有办法在动画自定义 canvas 之上创建一次性覆盖 ONCE?我还想与它下面的 canvas 进行交互,就好像叠加层根本不存在一样。

您是否尝试过在表面视图上放置一个全屏 ImageView 并将 "fog of war" 图像设置为此,应该绘制一次(假设背景为您的 "fog of war is transparent")。

如果您需要更高级的东西,那么 ViewOverlay 应该可以满足您的要求,但我的理解是,当单个视图希望在其边界之外绘制动画时使用它,因为您使用的是全屏表面视图,这应该是不必要的

https://developer.android.com/reference/android/view/ViewOverlay.html

也没有理由不能将表面视图粘贴到表面视图上。一种用于雾化war/画面效果,一种用于游戏表面。

根据 Thomas Stevens 的回复,我研究了用 ImageView 覆盖 SurfaceView 的选项。我的解决方案是添加一个包含所需 ImageView 的 FrameLayout。 (这样,我可以通过更新布局简单地向叠加层添加其他视图 XML。)

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //temporarily set the view to the overlay layout to gain access to the frame
    setContentView(R.layout.overlay_game);
    FrameLayout overlayLayout = (FrameLayout)findViewById(R.id.gameOverlayFrame);

    //set the final view to the active game surface (a Java-coded SurfaceView object)
    setContentView(new GameSurface(this));
    //add the overlay view on top of the game surface view
    addContentView(overlayLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
}

我不认为这是解决方案...只是适合我的解决方案。