古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述在Android上拉伸游戏画面以跨设备统一分辨率相关的知识,希望能为你提供帮助。
目前,我的游戏使用以下代码以正确的宽高比填充屏幕:
在Initialise()
我有:
protected override void Initialize()
{
graphics.IsFullScreen = true;
screen_width = graphics.GraphicsDevice.Viewport.Width;
screen_height = graphics.GraphicsDevice.Viewport.Height;
graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
graphics.ApplyChanges();
base.Initialize();
}
在
LoadContent()
我有:protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
float screenscale = screen_width / screen_height;
SpriteScale = Matrix.CreateScale(screenscale, screenscale, 1f);
//load textures here//
}
在
Draw()
我有:protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, SpriteScale);
//Draw sprites here//spriteBatch.End();
base.Draw(gameTime);
}
但是,使用此代码会根据设备宽度显示不同数量的游戏屏幕。
如何根据设备显示和缩放相同数量的游戏画面(如下图所示)?
文章图片
编辑:
在活动类我有:
using android.App;
using Android.Content.PM;
using Android.OS;
using Android.Views;
namespace android_game3
{
[Activity(Label = "android_game3"
, MainLauncher = true
, Icon = "@drawable/icon"
, Theme = "@style/Theme.Splash"
, AlwaysRetainTaskState = true
, LaunchMode = Android.Content.PM.LaunchMode.SingleInstance
, ScreenOrientation = ScreenOrientation.UserLandscape
, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var g = new Game1();
SetContentView((View)g.Services.GetService(typeof(View)));
g.Run();
//Hide action bar
View vw = (View)g.Services.GetService(typeof(View));
vw.SystemUiVisibility = (StatusBarVisibility)SystemUiFlags.HideNavigation | (StatusBarVisibility)SystemUiFlags.ImmersiveSticky;
vw.SetOnSystemUiVisibilityChangeListener(new MyUiVisibilityChangeListener(vw));
}//Hide action bar
private class MyUiVisibilityChangeListener : java.Lang.Object, View.IOnSystemUiVisibilityChangeListener
{
View targetView;
public MyUiVisibilityChangeListener(View v)
{
targetView = v;
}
public void OnSystemUiVisibilityChange(StatusBarVisibility v)
{
if (targetView.SystemUiVisibility != ((StatusBarVisibility)SystemUiFlags.HideNavigation | (StatusBarVisibility)SystemUiFlags.Immersive))
{
targetView.SystemUiVisibility = (StatusBarVisibility)SystemUiFlags.HideNavigation | (StatusBarVisibility)SystemUiFlags.ImmersiveSticky;
}
}
}
}
}
答案我检查了我的旧android项目,我用过:
ScreenWidth = graphics.PreferredBackBufferWidth;
ScreenHeight = graphics.PreferredBackBufferHeight;
【在Android上拉伸游戏画面以跨设备统一分辨率】另外,你的活动课怎么样?
推荐阅读
- 以编程方式更改Chip Widget样式不起作用 - Android
- 如何在android中同步/限制某些异步http调用
- AndroidPlot - 显示垂直域标签
- Android oreo - 推送通知崩溃
- Android与其他手机的连接
- 将com.google.android.gcm.GCMBaseIntentService升级到com.google.android.gms.gcm.GoogleCloudMessaging
- 从通知-Android打开活动
- CodenameOne - 使用android.buildToolsVersion = 27进行构建时,Android应用程序无法启动
- 将自定义JAR库添加到Android项目时Delphi10.2中的编译器错误