wallpaper设置壁纸图片被拉伸

时间:2021/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() {
        @Override
        public 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

@Override
public 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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值