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;

 

}

 

 

 

 

 

 

你可能感兴趣的:(android)