[sg] Android(安卓)4.4 屏幕旋转的两种方式
16lz
2021-01-26
通过配置系统属性 ro.sf.hwrotation
判断系统是否支持 ro.sf.hwrotation属性,因为这个属性4.4之后可能已经被移除。如果下面的方法存在,则系统支持通过设置该属性来进行旋转
void DisplayDevice::setProjection(int orientation, const Rect& newViewport, const Rect& newFrame) { Rect viewport(newViewport); Rect frame(newFrame); int displayOrientation = DisplayState::eOrientationDefault; char property[PROPERTY_VALUE_MAX]; if (mType == DISPLAY_PRIMARY) { if (property_get("ro.sf.hwrotation", property, NULL) > 0) { switch (atoi(property)) { case 90: displayOrientation = DisplayState::eOrientation90; break; case 270: displayOrientation = DisplayState::eOrientation270; break; } } }
修改方式:
- 在编译build.prop时添加进去,即修改 \build\tools\buildinfo.sh脚本添加
echo "ro.sf.hwrotation=270"
- 在init.rc 添加 Setprop ro.sf.hwrotation 270 进行初始化
优缺点:
- 开机动画同样可以进行旋转
- ro的属性初始化一次后就不能改变,所以不能动态改变
通过修改WindowsManagerServices
找到方法public boolean updateRotationUncheckedLocked(boolean inTransaction)
+ // 0 横屏, 3竖屏 + rotation = setDefaultScreenOrientation(0, rotation); if (mRotation == rotation && mAltOrientation == altOrientation) { // No change. return false; }
添加方法 ,修改rotation的值
/** * add by sg * change Lock Screen screen`s orientation, */ public int setDefaultScreenOrientation(int type,int rotation){ switch (type){ case Surface.ROTATION_0: rotation = Surface.ROTATION_0; break; case Surface.ROTATION_270: rotation = Surface.ROTATION_270; break; default: break; } return rotation; }
优缺点:
- 开机动画同样不会进行旋转
- type可以通过settings属性保存到系统中,可以动态修改
更多相关文章
- 修改WebView背景颜色为透明
- 换到android 3.0出现duplicate value for resource 'attr/textCo
- Android(安卓)WebView通过代码修改网页的字体大小
- 笔记33--ListView和ScrollView点击事件的冲突
- Hi3716C Android(安卓)SDK编译说明
- 关于EditText的属性和常识
- 【flutter】Running Gradle task 'assembleDebug' android 首次
- android 修改launcher行数和列数的方法
- Android(安卓)5.0新控件常用属性收集