美工同学表示不能忍,那就找找scale的原因吧。
Google和百度找了很久发现 小米 ov 一加 nexus 都存在这个问题,Google关于android-11的新特性有一个相关提及:
Zoom animation on home screen wallpaper
As the name suggests, the home screen wallpaper has a zoom animation on Android 11. This happens when you swipe down the notification panel. The wallpaper will zoom out in the background whenever the notification panel is accessed.
google developper 网站上没有更详细的说明。但是在某论坛上找到了这个回复:
From a quick test (setting a grid wallpaper and counting the squares) it seems to zoom in to 90% of the original image. So you'd need to have a 5% margin on all sides of your wallpaper to fix it. 5% per side definitely worked. Depending on the wallpaper it might look a bit strange when it zooms out, but at least when it's on the home screen it looks back to normal.
缩放到90%。本地先试一下:新建一个原图110%大小的画布,然后在中间区域放上原来的墙纸,放上去之后确实都显示了,但是我的图在最右侧有一条黑线,通过缩放后在最右侧缺了一个像素。对于处女座来说这不能忍啊, 那就想办法找到缩放的原因然后disable不就可以了。
经过dump找到设置墙纸的activity是com.android.wallpapercropper/.WallpaperCropActivity
, 经过断点调试与增加log输出发现并没有crop或者scale输入的墙纸,那么问题就很明朗了:WallpaperService里缩放了。
关键代码位置
-
触发位置
- 系统开机时和设置墙纸时WallpaperService$$Engine$$mWindow#updateSurface()
代码位置:frameworks/base/core/java/android/service/wallpaper/WallpaperService.java void updateSurface(boolean forceRelayout, boolean forceReport, boolean redrawNeeded) { ...... mSession.setShouldZoomOutWallpaper(mWindow, shouldZoomOutWallpaper()); } //hidden API for app developpers. public boolean shouldZoomOutWallpaper() { return false; }
- 用户操作Notification时NotificationShadeDepthController.updateBlurCallback
代码位置:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt val zoomOut = blurUtils.ratioOfBlurRadius(blur) try { wallpaperManager.setWallpaperZoomOut(root.windowToken, zoomOut) } catch (e: IllegalArgumentException) { Log.w(TAG, "Can't set zoom. Window is gone: ${root.windowToken}", e) } listeners.forEach { it.onWallpaperZoomOutChanged(zoomOut) } notificationShadeWindowController.setBackgroundBlurRadius(blur)
- Launcher上滑进入app列表页时/点击HOME键返回桌面时DepthController#setDepth(float)
代码位置:packages/apps/Launcher3/quickstep/src/com/android/launcher3/statehandlers/DepthController.java private void setDepth(float depth) { ...... mDepth = depthF; ensureDependencies(); IBinder windowToken = mLauncher.getRootView().getWindowToken(); if (windowToken != null) { mWallpaperManager.setWallpaperZoomOut(windowToken, mDepth); } ......
- 系统开机时和设置墙纸时WallpaperService$$Engine$$mWindow#updateSurface()
-
控制逻辑 WallpaperController.java
代码位置:frameworks/base/services/core/java/com/android/server/wm/WallpaperController.java void setWallpaperZoomOut(WindowState window, float zoom) { //Launcher 和SystemUI 调用 if (Float.compare(window.mWallpaperZoomOut, zoom) != 0) { window.mWallpaperZoomOut = zoom; mShouldUpdateZoom = true; updateWallpaperOffsetLocked(window, false); } } void setShouldZoomOutWallpaper(WindowState window, boolean shouldZoom) { //设备启动时 设置新墙纸时调用 if (shouldZoom != window.mShouldScaleWallpaper) { window.mShouldScaleWallpaper = shouldZoom; updateWallpaperOffsetLocked(window, false); } } private void updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) { WindowState target = mWallpaperTarget; if (target != null) { ...... computeLastWallpaperZoomOut(); ...... } for (int curTokenNdx = mWallpaperTokens.size() - 1; curTokenNdx >= 0; curTokenNdx--) { mWallpaperTokens.get(curTokenNdx).updateWallpaperOffset(sync); } } boolean updateWallpaperOffset(WindowState wallpaperWin, boolean sync) { ...... boolean changed = wallpaperWin.mWinAnimator.setWallpaperOffset(xOffset, yOffset, wallpaperWin.mShouldScaleWallpaper ? zoomOutToScale(wallpaperWin.mWallpaperZoomOut) : 1); ...... }
-
Scale 动画的实现:WindowStateAnimator.java
代码位置:frameworks/base/services/core/java/com/android/server/wm/WindowStateAnimator.java boolean setWallpaperOffset(int dx, int dy, float scale) { if (mXOffset == dx && mYOffset == dy && Float.compare(mWallpaperScale, scale) == 0) { return false; } mXOffset = dx; mYOffset = dy; mWallpaperScale = scale; ...... try { ...... mService.openSurfaceTransaction(); setWallpaperPositionAndScale(dx, dy, scale, false); ...... } finally { mService.closeSurfaceTransaction("setWallpaperOffset"); ...... } private void setWallpaperPositionAndScale(int dx, int dy, float scale, boolean recoveringMemory) { DisplayInfo displayInfo = mWin.getDisplayInfo(); Matrix matrix = mWin.mTmpMatrix; matrix.setTranslate(dx, dy); matrix.postScale(scale, scale, displayInfo.logicalWidth / 2f, displayInfo.logicalHeight / 2f); matrix.getValues(mWin.mTmpMatrixArray); matrix.reset(); mSurfaceController.setPositionInTransaction(mWin.mTmpMatrixArray[MTRANS_X], mWin.mTmpMatrixArray[MTRANS_Y], recoveringMemory); mSurfaceController.setMatrixInTransaction( mDsDx * mWin.mTmpMatrixArray[MSCALE_X] * mWin.mHScale * mExtraHScale, mDtDx * mWin.mTmpMatrixArray[MSKEW_Y] * mWin.mVScale * mExtraVScale, mDtDy * mWin.mTmpMatrixArray[MSKEW_X] * mWin.mHScale * mExtraHScale, mDsDy * mWin.mTmpMatrixArray[MSCALE_Y] * mWin.mVScale * mExtraVScale, recoveringMemory); applyCrop(null, recoveringMemory); }
如何避免墙纸被缩放呢?
从代码中可以看到,控制缩放的逻辑有两个关键点:wallpaperWin.mShouldScaleWallpaper
和zoomOutToScale(zoom)
,因此可以从这两个方面入手:
- 方案1:将
mShouldScaleWallpaper
设置为false
,那么scale比例就一直是1即不缩放。- 经过代码追踪发现,
mShouldScaleWallpaper
的赋值来源于setShouldZoomOutWallpaper(window, shouldZoom)
的shouldZoom
参数,这个参数是WallpaperService$$Engine#shouldZoomOutWallpaper()
方法返回的,它的默认返回值是false
,但是SystemUI内继承了这个类并修改了返回值:public class ImageWallpaper extends WallpaperService { @Override public Engine onCreateEngine() { return new GLEngine(); } class GLEngine extends Engine { @Override public boolean shouldZoomOutWallpaper() {return true;} ......
- 显而易见,将SystemUI内的
shouldZoomOutWallpaper()
方法返回值改为false
,或直接删掉此override的方法即可制止壁纸缩放。
- 经过代码追踪发现,
- 方案2:确保
zoomOutToScale(zoom)
方法的返回值一直是1,那么wallpaper也不会被缩放。-
zoomOutToScale(zoom)
方法的逻辑如下://in com.android.server.wm.WallpaperController private float zoomOutToScale(float zoom) { return MathUtils.lerp(1, mMaxWallpaperScale, 1 - zoom); } //in android.util.MathUtils @UnsupportedAppUsage public static float lerp(float start, float stop, float amount) { return start + (stop - start) * amount; }
- 可以看到,如果参数stop也就是
mMaxWallpaperScale
的值也是1,那么返回值就恒为1,从而忽略了传递值float zoom
的变化。在WallpaperController
内,mMaxWallpaperScale
是被private final 修饰的,并且构造方法内赋值的,那么只需要修改config_wallpaperMaxScale
的值即可,而且它的默认字是1.10,和上文所说的放大10%的目测结果相符。我最终也是采用了这种方法,通过device的overlay功能覆盖了config_wallpaperMaxScale
的原始值,将它重置为1。//in com.android.server.wm.WallpaperController private final float mMaxWallpaperScale; //construct method WallpaperController(WindowManagerService service, DisplayContent displayContent) { ...... mMaxWallpaperScale = service.mContext.getResources() .getFloat(com.android.internal.R.dimen.config_wallpaperMaxScale); }
-
写在最后的总结
如果只是本地测试,那么以上两种方案随便使用即可。但如果是团队成员并且负责SystemUI(
frameworks/base/packages/SystemUI)那么第一种方案是最快捷的。
全文完,谢谢观看