Joe Gruberman
Joe Gruberman

Reputation: 40

Can I lay a fixed layer over a custom canvas?

In Android Studio, I'm developing a game in which I draw my animation at 60fps using a SurfaceView object. I use a semitransparent image as the top layer to hide the edges of the playing field -- sort of a "fogs of war" effect so that the player does not know what to expect beyond the shadows.

My problem is that I need to draw this shadow image for every frame, even though the shadow never changes or moves, even when the playing field scrolls right/left/up/down. And it's a full-screen image, so it slows the game to a crawl.

Is there a way to create a one-time overlay ONCE on top of the animated custom canvas? I also want to interact with the canvas beneath it, as if the overlay was not even there.

sample fogs-of-war effect

Upvotes: 0

Views: 853

Answers (3)

Gleick
Gleick

Reputation: 1

I created two GameViews in the same layout, one for the scenery and one for the objects

Upvotes: 0

Joe Gruberman
Joe Gruberman

Reputation: 40

Based on the response from Thomas Stevens, I researched my options for overlaying my SurfaceView with an ImageView. My solution is to add a FrameLayout that contains the needed ImageView. (That way, I can add additional views to the overlay simply by updating the layout 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));
}

I don't claim this to be THE solution...just the one that works for me.

Upvotes: 0

user8921383
user8921383

Reputation:

Have you tried putting a full screen ImageView over your surface view and setting the "fog of war" image to this, should draw it once (this is assuming the background to your "fog of war is transparent").

If you need anything fancier then ViewOverlay should cover you, but my understanding is this is used when individual views wish to draw outside their bounds for animations, as you are using full screen surface view this should be unnecessary

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

Also no reason why you can't stick a surface view over a surface view. One for the fog of war / screen effects, one for the game surface.

Upvotes: 0

Related Questions