Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 方向改变-背景_Android_Orientation_Landscape - Fatal编程技术网

Android 方向改变-背景

Android 方向改变-背景,android,orientation,landscape,Android,Orientation,Landscape,如果手机处于横向模式,我的应用程序会通过加速计识别 (我使用加速计,因为我有3个不同的方向和其他变化值)。 我使用以下代码更改方向: myLayout.setBackgroundResource(R.drawable....); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 当方向改变时,我会在几秒钟内看到扭曲的图像。这显然是因为BackgroundRessource在方向

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

myLayout.setBackgroundResource(R.drawable....);                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
当方向改变时,我会在几秒钟内看到扭曲的图像。这显然是因为BackgroundRessource在方向改变之前就改变了

我曾尝试使用layout land和layout port文件夹,其结果是,我在横向方向上的图像总是扭曲的


如何避免这种情况?

我有一个类似的问题,除了切换到横向时会出现黑屏。看到有人提到创建一个新的可绘制的土地文件夹,这可能对你有用…但对我不起作用

我刚从这里的一位会员那里得到这个答案。试试看

@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);
 }
}