开发者

Do not change background when changing orientation

开发者 https://www.devze.com 2023-04-05 14:18 出处:网络
In my application, I have an image as background. When the application is in portrait mode, that\'s fine. But when it is in landscape mode, the image turns and is stretched.

In my application, I have an image as background. When the application is in portrait mode, that's fine. But when it is in landscape mode, the image turns and is stretched.

I know that we can use the layout-land folder, but with this solution, all my layouts will be in double just for an image (not very logical..).

Is there another solution?

PS : My image is just a carbon texture so, a solution to prevent it to turn with orientation would开发者_运维技巧 be ok.

EDIT :

hotveryspicy, your solution works BUT, there are activities which have the property android:configChanges="orientation" (in the Manifest), and it doesn't work for them. How to solve this?

In fact, I use this property to save state when the orientation changes but it's not the good way to do that. I need to use onSaveInstanceState for that.

In the meantime, is there a solution which allows me to keep this bad solution?


ya, first check orientation in onCreate()

Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int height= mDisplay.getHeight();

if(width>height)
{
//Landscape
//set landscape compatible image 
}
else
{
//Portrait
//set portrait compatible image
}


Would it do the trick if you placed alternate version of the background image into drawable-land res folder? This way you can still use the same layout files and refer to the @drawable/background in them.


I doubt it would be possible to do what you had described. The only solution which was possible (layout-land) you had already ruled out. It is the only way to go about it I guess.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号