cping / LGame

A cross-platform Java game Engine (Framework) , support JavaFX / Android / IOS / HTML5 / Linux / MAC / Windows
Apache License 2.0
579 stars 171 forks source link

关于安卓的横竖屏问题 #23

Open androids7 opened 5 years ago

androids7 commented 5 years ago

loon类里默认是横屏。而我需要开发竖屏游戏,不得不更改loon安卓包并且发展里面调用了一些过时的opengles函数。还好我把它注释掉了目前还没有发现什么问题,另外图片按钮怎么用呢。有没有教程或者api呢

cping commented 5 years ago

您好,Loon中并没有强制横竖屏,横屏那是默认例子的而已,没有不能适配的情况。Android上的Loon类就是一个Activity的封装而已,使用上与其它的例子一样,Android的横竖屏通过文件AndroidManifest.xml设置screenOrientation项参数,取值参数为:

unspecified,默认值,由系统决定,不同手机可能不一致
landscape,强制横屏显示
portrait,强制竖屏显示
behind,与前一个activity的方向相同
sensor,重力感应,完全根据物理传感器方向转动,任何角度(比如90度,270度之类)activity都更着变化
sensorLandscape,只重力感应横屏旋转,一般横屏重力游戏会这样设置
sensorPortrait,只重力感应竖屏旋转,一般竖屏重力游戏会这样设置
nosensor,当旋转设备时候,界面不会跟着旋转。初始化界面方向由系统控制
user,旋转为用户当前设置的方向

一般情况下,游戏可以锁定为横屏 android:screenOrientation="landscape" 或者竖屏 android:screenOrientation="portrait"

而当屏幕大小改变时,会触发Screen的resize函数,重载此函数就可以监听屏幕旋转产生的大小改变了。

还有您是否使用的loon版本过低(虽然都是写0.5版,不过内部实现已经不同了),现在loon适配的就是android9.0的sdk,您可以在java文件夹中下载最新的jar。

基础使用示例请见java文件夹下samples文件夹中示例,都有完整注释的,还有一个可以桌面运行的jar。

cping commented 5 years ago

您好,刚才我又看了一下代码,发现这是我的锅,现在设置屏幕旋转方向需要重载loon中的usePortraitOrientation函数。

也就是android继承loon的中需要重载

protected boolean usePortraitOrientation() {
    return true;
}

只有这个函数return为true时竖屏Portrait才会生效,本来这是android的AndroidSetting设置里有的可选项,默认应该为true,以前调试时给去了,后来忘加上了……

现在已经修复,我把这参数改成了useOrientation,然后直接改成了能在AndroidSetting中设置屏幕方向,具体实现改成了这样:

protected boolean useOrientation() {
    if (game == null) {
        return true;
    }
    LSetting setting = game.setting;
    if (setting == null) {
        return true;
    }
    if (setting instanceof AndroidSetting) {
        return ((AndroidSetting) setting).useOrientation;
    }
    return true;
}

protected int orientation() {
    boolean use = useOrientation();
    int orientation = getRequestedOrientation();
    LSetting setting = game.setting;
    if (use) {
        if (setting instanceof AndroidSetting) {
            AndroidSetting aset = ((AndroidSetting) setting);
            if (aset.orientation != -1) {
                orientation = aset.orientation;
            }
        }
    } else {
        if (setting.landscape()) {
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        } else {
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }
    }
    return orientation;
}

如果useOrientation为false,则根据width和height的大小强制横屏或竖屏w>h就横,小于就竖

    setting.width = 320;
    setting.height = 480;
    setting.useOrientation = false;
    setting.orientation = -1;

如果

    setting.useOrientation = true;

就根据 setting.orientation = ActivityInfo.SCREEN_ORIENTATION_XXXXX;

决定屏幕旋转方式

如果不设置,默认则以orientation读取xml配置进行屏幕转换,默认情况下useOrientation为true,orientation = -1, 也就是xml配置优先。

晚上我更新上去。

androids7 commented 5 years ago

还有就是安卓权限的问题,如果读取AndroidMainfest的话最好不要获取包,因为这需要申请额外的权限,不过游戏都是需要权限的,一样动态加入申请权限功能