Scaling texture on Android

Hello. I’m trying to transform my texture for different screen resolutions.
I use the following code:

const int DesignWidth = 1920;
const int DesignHeight = 1080;
Matrix scaleMatrix;

protected override void Initialize()
{
    float scale_wid = (float)graphics.GraphicsDevice.PresentationParameters.BackBufferWidth / DesignWidth;
    float scale_hei = (float)graphics.GraphicsDevice.PresentationParameters.BackBufferHeight / DesignHeight;
    scaleMatrix = Matrix.CreateScale(scale_wid, scale_hei, 1.0f);
    ...
}

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin(transformMatrix: scaleMatrix);
    spriteBatch.Draw(static_background, Vector2.Zero, Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}

The size of the used texture: 1920: 1080 pixels

The result is the following (see image)
Why does the blue bar appear?

not sure if that’s the case here, but had a similar case, where the game started/initialized in fullscreen with the full display size, and then the softbuttons sclaed the viewing rectangle down (after initialize) - long story short: your backbuffer size changed after initialize.

I did try various things, so I am not sure which was the right in the end - it’s best to implement viewport resizing during runtime and react on the resize event to reinitialize your scale Matrix and it should work. There is a function in the game class you can overload

I solved this problem when I added the following code to the constructor

var metric = new Android.Util.DisplayMetrics();
Activity.WindowManager.DefaultDisplay.GetMetrics(metric);
1 Like