Monogame android stretch screen

Hello, i wonder how can i stretch/fit/set the screen size of my app, because it’s always getting different on my phone, and on my android emulator.

One way to do it is using a scaling matrix.

You can setup a scaling matrix in a method with no parameters and add that method to windowsClientSizedChanged in your constructor or initialize.
Then later pass that to spritebatch

Within your method get the current screen width and height compared to some set screen size you design your game in to create a streaching ratio.

Matrix screenScale = Matrix.Identity;

public Game1()
        {
         ... 
            // add your recieving method to be called back
            this.Window.ClientSizeChanged += HeyCallMeWhenTheWindowChangesSize;
            // this sender can take more then one call back method +=
        }

public void HeyCallMeWhenTheWindowChangesSize(object sender, EventArgs e) 
        { 
            /* we are here when the windows being resized */
            var r = GraphicsDevice.Viewport.Bounds;
            screenScale = Matrix.Identity * Matrix.CreateScale(r.Width / designWidth, r.Height / designHeight, 0f);
            // design is typically a constant to the screen resolution you designed your game in.
        }

       // in spriteBatch.Begin( .... , ..., .... there is a overload that takes a matrix ..)
       // spriteBatch.Begin( .... , ... , screenScale

As you can see on this ss http://puu.sh/uIaNj/4310ffc7d5.png i have 2 different emulator’s resolutions and it’s not scaling properly, i have used your code, i have used code from internet either. What’s wrong with this?

Guess everyone is zzzz right now.

Sorry
The resolution behavior is somewhat inconsistent atm i think across dx gl / platforms.

Try using the backbuffer instead of the viewport.
Its possible on resize isn’t getting called on that platform at game start. Stick that code in load as well.
If that doesn’t work i don’t know whats going on.

var bw = GraphicsDevice.PresentationParameters.BackBufferWidth;
var bh = GraphicsDevice.PresentationParameters.BackBufferHeight;
screenScale = Matrix.Identity * Matrix.CreateScale(bw / designWidth, bh / designHeight, 0f);

and…
If that doesn’t work i suggest displaying the backbuffer viewport and window bounds for each and taking a look at them or posting pics here with them, that would be of help in seeing what is actually going on.

1 Like

I think i know what’s the problem.

It can be this sidebar http://puu.sh/uIeox/c951d34171.png it’s taking space so my resolution isn’t always 16:9 as it should be.

I’ve found code to remove this

`protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

        var g = new Game1();
        SetContentView((View)g.Services.GetService(typeof(View)));
        g.Run();
        HideSystemUI();
    }
    private void HideSystemUI()
    {
        if (Android.OS.Build.VERSION.SdkInt >= (Android.OS.BuildVersionCodes)19)
        {
            View decorView = Window.DecorView;
            var uiOptions = (int)decorView.SystemUiVisibility;
            var newUiOptions = (int)uiOptions;

            newUiOptions |= (int)SystemUiFlags.LowProfile;
            newUiOptions |= (int)SystemUiFlags.Fullscreen;
            newUiOptions |= (int)SystemUiFlags.HideNavigation;
            newUiOptions |= (int)SystemUiFlags.ImmersiveSticky;
            
            decorView.SystemUiVisibility = (StatusBarVisibility)newUiOptions;
        }
    }`

But when it comes to var bw = GraphicsDevice.PresentationParameters.BackBufferWidth; it’s throwing an error http://puu.sh/uIeyR/f6fc754ae7.png

It can be this sidebar http://puu.sh/uIeox/c951d34171.png it’s taking space so my resolution isn’t always 16:9 as it should be.

gotta be more then just that the scaling is way off.

But when it comes to var bw = GraphicsDevice.PresentationParameters.BackBufferWidth; it’s throwing an error http://puu.sh/uIeyR/f6fc754ae7.png

is it tossing that error in your load method or on your resize method ?

if its doing that from the On resize call back just comment it out for now.

It just means its getting called but too early. Which is just a left over misbehavior compared to xna on some systems. It can be dealt with by making a bool and wrapping the code block with a if(hasLoaded). That only gets set true at the end of load.

If that’s coming from load that would be really odd and somewhat concerning.
The graphics device should be fully setup by the time it hits load.

I have found the solution on some russian site and it’s working perfectly https://github.com/Dageron/Angry-PacMan/blob/master/PacMan/Game.cs. Thanks for your help i know now where was the problem.