2014-11-13 41 views
-1

我有.png高清(1024x768)背景圖片。我的用戶可能有不同的屏幕尺寸,但它應該看起來總是相同的,所以:Autoscalable background image

是否可以在中自動縮放到背景尺寸?

+2

是的,它應該是可以的。 – LearnCocos2D

+0

你做了什麼研究?任何代碼顯示? – MickyD

回答

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/