【问题标题】:Stretching game screen on android to uniform resolution across devices将安卓上的游戏屏幕拉伸到跨设备的统一分辨率
【发布时间】:2017-12-25 22:49:16
【问题描述】:

目前,我的游戏使用以下代码以适合设备的正确纵横比填充屏幕:

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;
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# android xna 2d monogame


    【解决方案1】:

    我检查了我的旧 android 项目并使用了:

    ScreenWidth = graphics.PreferredBackBufferWidth;
    ScreenHeight = graphics.PreferredBackBufferHeight;
    

    另外,你的活动课是什么样子的?

    【讨论】:

    • 您好,感谢您的回答,我已经添加了我的活动课程(见上文)。我尝试使用 graphics.PreferredBackBuffer 但它似乎对分辨率或屏幕尺寸没有任何影响。
    • 你能解释一下你是如何使用你的代码的吗?
    猜你喜欢
    • 1970-01-01
    • 2017-02-16
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多