我的Android应用程序(基于文本的游戏)大量使用背景图像,以提供更好的视觉氛围。例如,如果游戏中的动作将您带入小酒馆,那么您将获得游戏中小酒馆的背景图像。这是一个巨大的改进,图形上,在你无法获得的枯燥的黑色背景上。
然而,这也是一个问题,因为android:background总是延伸到屏幕的尺寸。结果是,如果播放器在纵向和横向模式之间切换,背景图像看起来非常糟糕。更糟糕的是,许多设备具有非常不同的宽高比(例如,320x480 mdpi,480x800 hdpi和480x852 hdpi),甚至会有更多变化。
其他人如何解决这个问题?拥有主要分辨率/方向的单独图像对我来说不是一个选项,因为这会导致apk变得太大。
答案 0 :(得分:5)
第一步是获取设备本身的屏幕高度和宽度,然后根据需要拉伸/缩小或填充位图。这个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
}
答案 1 :(得分:2)
我建议您获取当前视图的宽度和高度。现在我不确定哪些函数会给你这些值,但我相信它是可能的。然后,您可以计算宽高比。我将图像设置为1000x1000,只显示图像的某个部分,因此纵横比不会出错。
我对相机有同样的问题。所以我的解决方案是选择两个值中的一个,宽度或高度,固定,并根据纵横比计算正确的其他值。我不确定是否有功能只显示图像的一部分,但我相信你可以写一些东西来复制图像的一部分并显示该部分。
缺点当然是你只会展示背景的一部分。由于手机的一般宽高比介于1:1.3和1:1.8之间,我想这不会是一个太大的问题。我宁愿以正确的方式看到图像的一部分,而不是看一个难看的拉伸图像。
答案 2 :(得分:2)