的第一步是獲得裝置本身的屏幕的高度和寬度,然後伸展/根據需要縮小或墊的位圖。這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
}
感謝您的答案。對不起,我只能選中其中一個。 :) – 2010-09-05 21:47:25
羅曼提供了另一個有用的答案http://stackoverflow.com/questions/3944080/android-avoid-scaling-of-background – 2010-10-29 10:17:03