[DESCRIPTION]
Launcher3如何实现壁纸居中?
[SOLUTION]
Launcher3的Wallpaper显示是动态的,与Launcher预置桌面屏数有关,只能确保第一次开机时壁纸居中。如果用户修改桌面数目,就无法让壁纸仍然居中。以默认只有1屏为例:
请修改Launcher3的Workspace.java的updateOffset方法,如下:
private void updateOffset(boolean force) {
if (mWaitingForUpdate || force) {
mWaitingForUpdate = false;
LauncherLog.d(TAG, "updateOffset: mWallpaperOffset = " + mWallpaperOffset.getCurrX());
if (computeScrollOffset() && mWindowToken != null) {
try {
if (getChildCount() <= 1) {
LauncherLog.d(TAG, "Default screen number is 1.");
mWallpaperManager.setWallpaperOffsets(mWindowToken,
/* mWallpaperOffset.getCurrX()*/0.5f, 0.5f);
}else{
LauncherLog.d(TAG, "Default screen number >= 1.");
mWallpaperManager.setWallpaperOffsets(mWindowToken,
mWallpaperOffset.getCurrX(), 0.5f);
}
setWallpaperOffsetSteps();
} catch (IllegalArgumentException e) {
Log.e(TAG, "Error updating wallpaper offset: " + e);
}
}
}
}
if (mWaitingForUpdate || force) {
mWaitingForUpdate = false;
LauncherLog.d(TAG, "updateOffset: mWallpaperOffset = " + mWallpaperOffset.getCurrX());
if (computeScrollOffset() && mWindowToken != null) {
try {
if (getChildCount() <= 1) {
LauncherLog.d(TAG, "Default screen number is 1.");
mWallpaperManager.setWallpaperOffsets(mWindowToken,
/* mWallpaperOffset.getCurrX()*/0.5f, 0.5f);
}else{
LauncherLog.d(TAG, "Default screen number >= 1.");
mWallpaperManager.setWallpaperOffsets(mWindowToken,
mWallpaperOffset.getCurrX(), 0.5f);
}
setWallpaperOffsetSteps();
} catch (IllegalArgumentException e) {
Log.e(TAG, "Error updating wallpaper offset: " + e);
}
}
}
}
注意:不能将桌面默认屏设置为非0屏,否则Launcher可能会报出各种Exception。