Android 通过按键旋转屏幕
很好的帖子:
自己做的立方体旋转效果
http://www.eoeandroid.com/thread-185331-1-1.html
Android多线程后台下源码
http://www.eoeandroid.com/thread-185328-1-1.html
android仿酷狗播放器
http://www.eoeandroid.com/thread-185894-1-1.html
-------------------帖子正文-----------------------
有个功能,通过长按menu按键,可以旋转屏幕方向(0°或者90°),这个功能有时蛮有用的,下面来看看是如何实现的:
1 修改按键处理程序
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
函数
public boolean interceptKeyTi(WindowState win, int code, int metaKeys, boolean down,int repeatCount, int flags) //在处理菜单键的地方 if (code == KeyEvent.KEYCODE_MENU) { final int chordBug = KeyEvent.META_SHIFT_ON; if (down && repeatCount == 0) { if (mEnableShiftMenuBugReports && (metaKeys & chordBug) == chordBug) { Intent intent = new Intent(Intent.ACTION_BUG_REPORT); mContext.sendOrderedBroadcast(intent, null); return true; } else if (SHOW_PROCESSES_ON_ALT_MENU &&(metaKeys & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) { Intent service = new Intent(); service.setClassName(mContext, "com.android.server.LoadAverageService"); ContentResolver res = mContext.getContentResolver(); boolean shown = Settings.System.getInt(res, Settings.System.SHOW_PROCESSES, 0) != 0; if (!shown) { mContext.startService(service); } else { mContext.stopService(service); } Settings.System.putInt(res, Settings.System.SHOW_PROCESSES, shown ? 0 : 1); return true; } } //上面是原来的内容,下面是添加的新内容 else if (down && repeatCount == 20 && MenuKeyUp && (!keyguardOn)) { //如果按下Menu键一定时间,抬起时执行此段函数 MenuKeyUp = false; try { int ro = mWindowManager.getRotation(); //获取当前方向 if( ro == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ) { ro = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
ro = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
}catch (RemoteException e) {
Log.v(TAG, "!!! getRotation fail !!!");
}
try {
//旋转屏幕
mWindowManager.setRotation(ro, true, Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
//最后可跟不同的参数,可实现一些旋转效果
}
catch (RemoteException e) {
Log.v(TAG, "!!! mWindowManager.setRotation fail !!!");
}
return true;
}
if(!down)
{
MenuKeyUp = true;
}
}
2 修改实现选择的函数
/frameworks/base/services/java/com/android/server/WindowManagerService.java //找到该函数 public boolean setRotationUncheckedLocked(int rotation, int animFlags) //将以下妨碍选择的内容注释掉 //rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation, // mRotation, mDisplayEnabled);
3、当然也可以新作一个rotate键来选择屏幕,以下是引用代码
} else if (code == KeyEvent.KEYCODE_ROTATE) { // ROTATE KEY pressed if (down) { mButtonPushFlg = true; try { int ro = mWindowManager.getRotation(); // Orientation vertical if (ro == 3 ) { mWindowManager.setRotation (Surface.ROTATION_0,true,mFancyRotationAnimation); //Orientation landscape } else { mWindowManager.setRotation(Surface.ROTATION_270,true,mFancyRotationAnimation); //Orientation portlate } } catch (RemoteException e) { // Igbore Log.i("info", "Rotation failed "); } } return true; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架