方向变化 - 背景

发布于 2025-01-03 18:05:43 字数 376 浏览 1 评论 0原文

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

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

当方向更改时,我会看到扭曲的图像几秒钟。其原因显然是BackgroundRessource 在方向改变之前就被改变了。

我尝试使用布局土地和布局端口文件夹,结果是我在横向方向上总是出现扭曲的图像。

我怎样才能避免这种情况?

If the phone is in landscape mode my app recognizes this with the accelerometer
(I use the accelerometer, because i do have 3 differnt orientations and other values for the change).
I change the orientation with the following code:

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

When the orientation gets changed I see a distorted image for a few seconds. The reason of this is apparently that the BackgroundRessource gets changed before the change of orientation.

I have tried to use layout-land and layout-port folders with the consequence that i have in the landscape orientation always a distorted image.

How can I avoid this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青瓷清茶倾城歌 2025-01-10 18:05:43

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

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

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

I have a similar problem except that i just get a black screen when switching to landscape. Saw someone mention creating a new drawable-land folder which might work for you...didnt work for me tho.

Just got this answer myself from a member here. Give it a shot.

@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);
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文