gpt4 book ai didi

jquery - 锁定横向设备方向

转载 作者:行者123 更新时间:2023-12-01 00:23:42 24 4
gpt4 key购买 nike

正在开发phonegap应用程序,但遇到设备方向问题。

基本上,我希望用户可以按照自己的意愿自由地使用设备,无论是纵向还是横向。但是,我只想将屏幕变成纵向或横向。目前屏幕也正在反转横向。

是否可以将方向更改限制为仅纵向和横向? (因此您总共有 2 种方向改变的可能性,而不是 3 种)。

编辑

我使用了一个插件(下面建议)并提出了以下有效的代码。一旦屏幕旋转到反向横向,它就会以横向模式显示(正是我想要的)。但问题是,如果用户将手机转为纵向模式,方向将被锁定,并且不会转回纵向。

    $(window).bind('orientationchange', function(event){

if (event.orientation == 'landscape') {
navigator.screenOrientation.set('landscape');
}
if (event.orientation == 'portrait') {
navigator.screenOrientation.set('portrait');
}

});

有人知道我应该将以下行放在哪里以便解锁方向吗?

navigator.screenOrientation.set('fullSensor');

最佳答案

您将需要更改主要的 Phonegap Activity 类。首先从 AndroidManifest 文件中删除这一行:

android:screenOrientation="portrait"

我们还需要:

android:configChanges="orientation|keyboardHidden"  

不幸的是,我们无法在 screenOrientation 中配置多种方向模式。因此我们将通过java代码来改变它。

这是一个有效的代码示例:

package com.roadrunner.mobile21;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.Surface;

import com.phonegap.*;

public class RoadRunnerActivity extends DroidGap {
OrientationEventListener myOrientationEventListener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");

myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){

@Override
public void onOrientationChanged(int arg0) {

Display display;
display = getWindow().getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
unlockScreenOrientation();
}
}
};
if (myOrientationEventListener.canDetectOrientation()){
myOrientationEventListener.enable();
} else{
finish();
}
}


@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Display display;
display = getWindow().getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
if(rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
lockCurrentScreenOrientation();
}
}

private void lockCurrentScreenOrientation() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

private void unlockScreenOrientation() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}

}
  • 还可以通过 CSS 更改屏幕方向。我不喜欢,因为它有点问题,但是 here你可以找到更多相关信息。这是最简单的方法,但您需要稍微尝试一下才能发挥作用。

这很容易做到:

$(window).bind("orientationchange", function(){
var orientation = window.orientation;
var new_orientation = (orientation) ? 0 : 180 + orientation;
$('body').css({
"-webkit-transform": "rotate(" + new_orientation + "deg)"
});
});

您需要更改公式以仅适应 0 度和 90 度方向。

关于jquery - 锁定横向设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13988296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com