android - 具有软件控制的设备上的分辨率不一致

标签 android xamarin.android xamarin monogame

我正在使用 Xamarin 使用 MonoGame.Android 制作游戏。

我发现带有软件控件(此处以红色突出显示)的设备提供不一致的分辨率。

Android Software Controls

具体而言,此问题发生在以横向模式创建但在手机以纵向模式打开时打开的应用程序。 当您执行此操作时,手机会忽略手机的物理方向并无论如何以横向模式呈现。

例如,我为 Samsung Galaxy S2 创建了一个模拟器。我克隆了 2 个副本,一个使用软件控制 (S2Soft),另一个使用硬件控制 (S2Hard)。我在两部手机上都以纵向和横向启动了我的应用程序。

在 Game.Initialize 中,我检查了 PreferredBackBufferWidth 和 PreferredBackBufferHeight 的值。这是我发现的:

S2Soft:

  • 纵向:736x480
  • 横向:744x480

S2硬:

  • 纵向:分辨率:800x480
  • 横向:分辨率:800x480

现在,我了解到 S2Soft 与 S2Hard 相比可能具有更小的分辨率(以便为软件按钮腾出空间),我不明白为什么 S2Soft 有两种不同分辨率,具体取决于启动时手机的方向。

有没有人处理过这个问题?这对我来说是个问题,因为我的 UI 是根据这些分辨率放置的,如果有人以纵向模式启动应用程序,则放置不正确。当后台缓冲区未被渲染时,它还会在屏幕的一侧留下细框。

这是细条的示例(如果以横向模式启动,则顶部和底部的蓝色条不存在)。

enter image description here

谢谢!!

最佳答案

简答题

将以下内容添加到我的 Activity 配置中解决了这个问题。

ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape

通过添加这一行, Activity 在 MonoGame 初始化之前切换到横向,并完全避免了这些问题。

    [Activity (Label = "Swipe Tap Smash", 
    MainLauncher = true,
    Icon = "@drawable/icon_large",
    Theme = "@style/Theme.Splash",
    AlwaysRetainTaskState = true,
    LaunchMode = Android.Content.PM.LaunchMode.SingleTask,
    ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape,
    ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation |
    Android.Content.PM.ConfigChanges.KeyboardHidden |
    Android.Content.PM.ConfigChanges.Keyboard)]
public class Activity1  

长答案

我对此进行了调查,发现我所看到的是在 MonoGame 中设计的。如果您查看 MonoGame\v3.0\MonoGame.Framework\GraphicsDeviceManager.cs 中的 ResetClientBounds,您会发现这条评论:

    /// <summary>
    /// This method is used by MonoGame Android to adjust the game's drawn to area to fill
    /// as much of the screen as possible whilst retaining the aspect ratio inferred from
    /// aspectRatio = (PreferredBackBufferWidth / PreferredBackBufferHeight)
    ///
    /// NOTE: this is a hack that should be removed if proper back buffer to screen scaling
    /// is implemented. To disable it's effect, in the game's constructor use:
    ///
    ///     graphics.IsFullScreen = true;
    ///     graphics.PreferredBackBufferHeight = Window.ClientBounds.Height;
    ///     graphics.PreferredBackBufferWidth = Window.ClientBounds.Width;
    ///
    /// </summary>
    internal void ResetClientBounds()

这就是为什么我们会在屏幕的一侧看到横条; MonoGame 已经创建了渲染区域,现在被告知渲染到不同大小的区域。它不是创建一个新的渲染表面,也不是拉伸(stretch)现有的渲染表面,而是对图像进行信箱处理,以尽可能地适合它。

但是,在我的情况下这实际上不应该被触发,因为它不应该在事后切换方向。这已修复我的 Activity 中的设置 ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape

关于android - 具有软件控制的设备上的分辨率不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22885965/

相关文章:

c# - 防止用户返回上一个 Activity

android - 发送数据到 Android 设备 Notification to IOS 设备

android - 通过将 Imageview 左上角视为 (0,0) 来获取 Imageview 的 X,Y 坐标

c# - 如何访问 NSMutableArray 索引 Xamarin.ios

xamarin.forms - Httpclient Xamarin Forms 调用在 Android 中不起作用

ios - 无法在 iOS 上的 Azure NotificationHub 中注册设备,无回调

java - 在 View 寻呼机中滚动时折叠工具栏不会折叠

Android 如何更新 ProgressBar 的值?

java - Xamarin - 使用 JavaScript 调用 Android 函数

Xamarin.forms nugetpackage 冲突且无休止的更新