时间:/04/07
之前公司不允许csdn,笔记写在其它地方。最近整理过来
问题描述:
原生壁纸设置壁纸之后,图片被拉伸。导出/data/system/users/0/wallpaper_info.xml
手机分辨率480854 壁纸被设置成2480 * 854
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><wp id="10" width="960" height="854" cropLeft="0" cropTop="0" cropRight="0" cropBottom="0" colorsCount="3" colorValue0="-13485218" colorValue1="-801681" colorValue2="-2239870" colorHints="4" name="" />
修复方案
1、壁纸代码中宽高计算
packages/apps/WallpaperPicker2/src/com/android/wallpaper/util/WallpaperCropUtils.java
public static Point getDefaultCropSurfaceSize(Resources resources, Display display, boolean scroll) {Point minDims = new Point();Point maxDims = new Point();display.getCurrentSizeRange(minDims, maxDims);int maxDim = Math.max(maxDims.x, maxDims.y);int minDim = Math.max(minDims.x, minDims.y);if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {Point realSize = new Point();display.getRealSize(realSize);maxDim = Math.max(realSize.x, realSize.y);minDim = Math.min(realSize.x, realSize.y);}final int defaultWidth, defaultHeight;if (resources.getConfiguration().smallestScreenWidthDp >= 720) {defaultWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim));defaultHeight = maxDim;} else {//这个值默认是2 WALLPAPER_SCREENS_SPAN 双屏壁纸defaultWidth = minDim;//scroll ? Math.max((int) (minDim * WALLPAPER_SCREENS_SPAN), maxDim) : minDim;defaultHeight = maxDim;}return new Point(defaultWidth, defaultHeight);}
2、packages/apps/Launcher3/src/com/android/launcher3/Workspace.java中设置壁纸宽高
protected void setWallpaperDimension() {Executors.THREAD_POOL_EXECUTOR.execute(new Runnable() {@Overridepublic void run() {final Point size = LauncherAppState.getIDP(getContext()).defaultWallpaperSize;if (size.x != mWallpaperManager.getDesiredMinimumWidth()|| size.y != mWallpaperManager.getDesiredMinimumHeight()) {mWallpaperManager.suggestDesiredDimensions(size.x, size.y);}}});}
3、frameworks/base/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java中根据显示大小计算壁纸宽高
loadSettingsLocked中:private void ensureSaneWallpaperDisplaySize(DisplayData wpdData, int displayId) {// We always want to have some reasonable width hint.final int baseSize = getMaximumSizeDimension(displayId);final int width = getMinimumSizeDimension(displayId);//我自己加的Slog.i(TAG, "baseSize:" + baseSize + " width: "+width);Slog.i(TAG, "wpdData.mWidth:" + wpdData.mWidth + " wpdData.mHeight: "+wpdData.mHeight);if (wpdData.mWidth < width) {wpdData.mWidth = width;}if (wpdData.mHeight < baseSize) {wpdData.mHeight = baseSize;}Slog.w(TAG, "wpdData.mWidth:" + wpdData.mWidth + " wpdData.mHeight: "+wpdData.mHeight);}
以上修改android11上无效
4、frameworks/base/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java
@Overridepublic boolean shouldZoomOutWallpaper() {//修改这里的返回值,并且修改WallpaperCropUtils中的修改,可以解决缩放问题return !SPRD_STABLE_WALLPAPER;}//默认会放大1.1倍<!-- The max scale for the wallpaper when it's zoomed in --><item name="config_wallpaperMaxScale" format="float" type="dimen">1.10</item>