可自动调整的背景图像

时间:2014-11-13 17:32:38

标签: c# vb.net xna

我有.png HD(1024x768)背景图片。我的用户可能有不同的屏幕尺寸,但看起来总是一样,所以:

是否可以自动将其大小调整为中的背景大小?

1 个答案:

答案 0 :(得分:1)

首先,您必须将整个场景渲染到RenderTarget,然后使用矩形绘制位置和大小。

Rectangle dest = new Rectangle (0, 0, graphics.ViewPort.Width, graphics.ViewPort.Height);
spriteBatch.Draw(RenderTarget, dest, Color.White);
如果你在16:9工作,用户有4:3,那么它看起来会拉伸/挤压:

int height = (int)(graphics.ViewPort.Width * (16.0/9.0));
Rectangle dest = new Rectangle (0, graphics.ViewPort.Height -- (int)(height / 2.0), graphics.ViewPort.Width, graphics.ViewPort.Height);
spriteBatch.Draw(RenderTarget, dest, Color.White);

或一般来说,宽度> =高度:

double aspectratio = ((double)graphics.ViewPort.Width / (double)graphics.Viewport.Height);
int height = (int)(graphics.ViewPort.Width * aspectratio;
Rectangle dest = new Rectangle(0, graphics.ViewPort.Height - (int)(height / 2.0), graphics.ViewPort.Width, height);

这是一个很好的独立屏幕分辨率示例:

http://www.david-amador.com/2010/03/xna-2d-independent-resolution-rendering/