【问题标题】:Handling different screen resolutions for background images in Android在 Android 中处理背景图像的不同屏幕分辨率
【发布时间】:2010-09-03 10:38:33
【问题描述】:

我的 Android 应用(基于文本的游戏)大量使用背景图片来提供更好的视觉氛围。例如,如果游戏中的动作将您带入酒馆,那么您将获得游戏中酒馆的背景图像。在图形上,这是一个巨大的改进,而不是您本来会得到的无聊的黑色背景。

然而,这也是一个问题,因为 android:background 总是会拉伸到屏幕的尺寸。结果是,如果玩家在纵向和横向模式之间切换,背景图像看起来很糟糕。更糟糕的是,许多设备具有非常不同的纵横比(例如,320x480 mdpi、480x800 hdpi 和 480x852 hdpi),而且还会出现更多变化。

其他人如何解决这个问题?为主要分辨率/方向设置单独的图像对我来说不是一个选择,因为这会导致 apk 变得太大。

【问题讨论】:

标签: android user-interface screen-resolution aspect-ratio


【解决方案1】:

第一步是获取设备本身的屏幕高度和宽度,然后根据需要拉伸/收缩或填充位图。这个SO answer 有可以提供帮助的来源,复制如下。支持Josef 获取原始答案。

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

这是获取方向的通用来源。

int orientation = display.getOrientation(); 

或来自here 的更多相关来源(有点乱但看起来正确)。

public int getscrOrientation()
{
  Display getOrient = getWindowManager().getDefaultDisplay();

  int orientation = getOrient.getOrientation();

  // Sometimes you may get undefined orientation Value is 0
  // simple logic solves the problem compare the screen
  // X,Y Co-ordinates and determine the Orientation in such cases
  if(orientation==Configuration.ORIENTATION_UNDEFINED){

      Configuration config = getResources().getConfiguration();
      orientation = config.orientation;

      if(orientation==Configuration.ORIENTATION_UNDEFINED){
        //if height and widht of screen are equal then
        // it is square orientation
            if(getOrient.getWidth()==getOrient.getHeight()){
            orientation = Configuration.ORIENTATION_SQUARE;
            }else{ //if widht is less than height than it is portrait

                if(getOrient.getWidth() < getOrient.getHeight()){
                orientation = Configuration.ORIENTATION_PORTRAIT;
                }else{ // if it is not any of the above it will defineitly be landscape
                orientation = Configuration.ORIENTATION_LANDSCAPE;
                }
            }
       }
  }
  return orientation; // return value 1 is portrait and 2 is Landscape Mode
}

【讨论】:

    【解决方案2】:

    我建议您获取当前视图的宽度和高度。现在我不确定哪些函数会给你这些值,但我相信这是可能的。然后,您可以计算纵横比。我会将图像设置为 1000x1000 并只显示图像的某个部分,这样纵横比就不会出错。
    我的相机也有同样的问题。所以我的解决方案是选择固定的宽度或高度这两个值之一,并根据纵横比计算正确的另一个值。我不确定是否已经有功能可以仅显示图像的一部分,但我相信您可以编写一些东西来仅复制图像的一部分并显示该部分。

    当然,缺点是您将只显示背景的一部分。由于手机的一般纵横比介于 1:1.3 和 1:1.8 之间,我想这不会是一个太大的问题。我宁愿以正确的方式看到图像的一部分,也不愿看到丑陋的拉伸图像。

    【讨论】:

      【解决方案3】:

      也许您可以将背景图像创建为NinePatch 图像,以便更好地控制它的拉伸方式?

      否则,您可以通过设置 scaleType 属性来防止图像拉伸(或至少保持正确的纵横比),例如android:scaleType="center".

      【讨论】:

        猜你喜欢
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-04
        • 1970-01-01
        • 2016-07-23
        相关资源
        最近更新 更多