Android4.4 應用分析——修改Launcher3應用以適應單屏壁紙
Launcher3壁紙的規格預設是:壁紙高度=螢幕高度,壁紙寬頻=螢幕寬度*2倍
Android4.4的壁紙資訊存放在/data/system/users/0/目錄下,WallpaperManagerService啟動後,會產生如下兩個檔案在/data/system/users/0/目錄下:
static final String WALLPAPER = wallpaper; //設定的壁紙圖片,一般為jpeg格式 static final String WALLPAPER_INFO = wallpaper_info.xml; //包含牆紙的規格資訊:高、寬
Wallpaper_info.xml的解析可以查看WallpaperManagerService的loadSettingsLocked()方法。下面是WallpaperManagerService的啟動時序圖,不解釋了,用的上朋友可以對著查看源碼:
下面我們就應該在Launcher3裡將壁紙的高、寬資訊寫入上面說的wallpaper_info.xml檔案中(當然時機寫入動作還是在Framework中),Launcher3隻是將其自身需要的牆紙高、寬寫入wallpaper_info.xml,你使用Go案頭,那肯定是在Go案頭設定高、寬了。
1. 找對位置後很簡單,就是Launcher3的WallpaperCropActivity.java檔案getDefaultWallpaperSize()。
static protected Point getDefaultWallpaperSize(Resources res, WindowManager windowManager) { Point minDims = new Point(); Point maxDims = new Point(); windowManager.getDefaultDisplay().getCurrentSizeRange(minDims, maxDims); int maxDim = Math.max(maxDims.x, maxDims.y); int minDim = Math.max(minDims.x, minDims.y); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { Point realSize = new Point(); windowManager.getDefaultDisplay().getRealSize(realSize); maxDim = Math.max(realSize.x, realSize.y); minDim = Math.min(realSize.x, realSize.y); } // We need to ensure that there is enough extra space in the wallpaper // for the intended // parallax effects final int defaultWidth, defaultHeight; if (isScreenLarge(res)) { defaultWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim)); defaultHeight = maxDim; } else { defaultWidth = Math.max((int) (minDim * WALLPAPER_SCREENS_SPAN), maxDim); defaultHeight = maxDim; } return new Point(defaultWidth, defaultHeight); }
defaultWidth和defaultHeight分別賦值為螢幕的寬、高就行了。
2. cropImageAndSetWallpaper()方法,這個方法應該是設定牆紙時,按牆紙規格裁剪圖片用到的,該法與上面一樣。
3. 阻止Launcher3滑屏時,滑動牆紙:在Workspace.java中,注釋updateOffset()方法的調用(有兩處)。