繁体   English   中英

在Android中处理背景图像的不同屏幕分辨率

[英]Handling different screen resolutions for background images in Android

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

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

别人怎么解决这个问题? 拥有主要分辨率/方向的单独图像对我来说不是一个选项,因为这会导致apk变得太大。

第一步是获取设备本身的屏幕高度和宽度,然后根据需要拉伸/缩小或填充位图。 这个SO答案的来源应该有所帮助,复制如下。 Josef道具的原始答案。

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

这是获取方向的一般来源。

int orientation = display.getOrientation(); 

或者来自这里的一些更多参与的来源(有点乱,但看起来正确)。

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
}

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

缺点当然是你只会展示背景的一部分。 由于手机的一般宽高比介于1:1.3和1:1.8之间,我想这不会是一个太大的问题。 我宁愿以正确的方式看到图像的一部分,而不是看一个难看的拉伸图像。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM