简体   繁体   English

科尔多瓦android:锁定方向后获取屏幕大小

[英]cordova android: get screen size after lock orientation

I'm using the screen orientation plugin . 我正在使用屏幕方向插件 It works well but after I lock the screen orientation the screen size remains with the previous orientation size. 效果很好,但是在锁定屏幕方向后,屏幕尺寸仍保持先前的方向尺寸。 So I have to reload the app to get the right screen size after lock the orientation. 因此,锁定方向后,我必须重新加载应用程序才能获得正确的屏幕尺寸。 Any idea why this behavior? 任何想法为什么这种行为? I need to get the right size after the lock. 锁定后,我需要获得正确的尺寸。 Here a code sample: 这里是一个代码示例:

console.log(window.screen.width) // 450   
screen.orientation.lock('landscape').then(() => {
  console.log(window.screen.width) // 450. Should be 640
});

BTW: I didn't find a issue tracker of this plugin 顺便说一句:我没有找到此插件的问题跟踪器

As a workaround , you could try listening for a proper orientation change event. 解决方法是 ,您可以尝试监听适当的方向更改事件。 The following snippet listens for a change event, and then removes the event listener after it fires once: 以下代码段侦听更改事件,然后在触发一次后将其删除:

window.addEventListener('orientationchange', function() {
    // After orientationchange, add a one-time resize event
    var afterOrientationChange = function() {
        // YOUR POST-ORIENTATION CODE HERE
        // Remove the resize event listener after it has executed
        window.removeEventListener('resize', afterOrientationChange);
    };
    window.addEventListener('resize', afterOrientationChange);
});

Add this snippet before you lock the orientation. 锁定方向之前,请添加此代码段。

Edit : Here is why I suggest using a resize event in conjunction with the orientation change event: https://stackoverflow.com/a/49383279/508098 编辑 :这就是为什么我建议将调整大小事件与方向更改事件结合使用的原因: https : //stackoverflow.com/a/49383279/508098

I do not know why that plugin does not work as expected -- I did not look. 我不知道为什么该插件无法按预期工作-我没有看。

Whilst this would be better to discuss with the developers of that project, as you pointed out, there is no issue tracker for that plugin's project. 正如您所指出的那样,最好与该项目的开发人员进行讨论,但是该插件项目没有问题跟踪程序。

Try this and see if you get the right numbers. 尝试一下,看看您是否获得正确的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM