2012-02-08 82 views
0

如果手機處於橫向模式,我的應用程序會使用加速度計 (我使用加速度計,因爲我確實有3種不同的方向和其他更改值)識別此值。 我更改方向用下面的代碼:方向更改 - 背景

myLayout.setBackgroundResource(R.drawable....);     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

當方向得到改變我看到失真的圖像幾秒鐘。原因很明顯,BackgroundRessource在方向改變之前得到了改變。

我試圖使用layout-land和layout-port文件夾,結果是我在橫向方向總是出現扭曲的圖像。

我怎樣才能避免這種情況?

回答

0

我有一個類似的問題,但我只是在切換到橫向時出現黑屏。看到有人提到創建一個新的可繪製的土地文件夾,可能適合你...沒有爲我工作壽。

剛從一個成員這裏得到了答案。試一試。

@Override 
public void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout); 
Resources res = getResources(); 
Drawable portrait = res.getDrawable(R.drawable.portrait); 
Drawable landscape = res.getDrawable(R.drawable.landscape); 

WindowManager window = (WindowManager)getSystemService(WINDOW_SERVICE); 
Display display = window.getDefaultDisplay(); 
int num = display.getRotation(); 
if (num == 0){ 
    linearLayout.setBackgroundDrawable(portrait); 
}else if (num == 1 || num == 3){ 
    linearLayout.setBackgroundDrawable(landscape); 
}else{ 
    linearLayout.setBackgroundDrawable(portrait); 
} 
}