方向变化 - 背景

时间:2012-02-08 19:48:50

标签: android orientation landscape

如果手机处于横向模式,我的应用会通过加速计识别出这一点 (我使用加速度计,因为我确实有3个不同的方向和其他值的变化)。 我使用以下代码更改方向:

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

当方向改变时,我会看到一个扭曲的图像几秒钟。这样做的原因显然是在更改方向之前更改了BackgroundRessource。

我试图使用layout-land和layout-port文件夹,结果是我在横向方向上始终是一个扭曲的图像。

我该如何避免这种情况?

1 个答案:

答案 0 :(得分:0)

我有一个类似的问题,除了我切换到横向时只是黑屏。看到有人提到创建一个可能对你有用的新的drawable-land文件夹......对我来说没什么用。

我自己从这里的成员那里得到了这个答案。试一试。

@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);
 }
}
相关问题