Android8.1 添加修改默认壁纸

关于壁纸的尺寸,建议静态壁纸的宽,高是:宽=屏幕分辨率的宽*2,高=屏幕分辨率的高;当然如果静态壁纸的宽,高与屏幕分辨率相等也是可以的,但是需要修改下代码不然Launcher workspace的背景会被拉伸。

1.添加多张静态壁纸
1.1 在/packages/apps/WallpaperPicker/res/drawable-nodpi/的文件夹下新增加wallpaper图片,每个wallpaper需要两种图片一张原图一张缩略图,如下形式:
wallpaper01.png
wallpaper01_small.png
wallpaper02.png
wallpaper02_small.png

缩略图的文件名必须是原图"文件名"+“_small”

1.2 在/packages/apps/WallpaperPicker/res/values-nodpi的wallpapers.xml中修改:
<resources>
    <string-array name="wallpapers" translatable="false">
    	<item>wallpaper01</item>
    	<item>wallpaper02</item>
    </string-array>
</resources>

没有这个目录或者文件的直接创建一个。
如果你的wallpaper图片尺寸宽是两倍的屏幕宽,这样修改就成功添加多张壁纸;
如果你的wallpaper图片尺寸宽高与屏幕宽高相同,还需进行第三步修改避免Launcher workspace的背景被拉伸

1.3 请在WallpaperUtils.java的getDefaultWallpaperSize方法中,找到如下代码:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Point getDefaultWallpaperSize(Resources res, WindowManager windowManager) {
    
    
    if (sDefaultWallpaperSize == null) {
    
    
        Point realSize = new Point();
        windowManager.getDefaultDisplay().getRealSize(realSize);
        int maxDim = Math.max(realSize.x, realSize.y);
        int 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 (res.getConfiguration().smallestScreenWidthDp >= 720) {
    
    
        	defaultWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim));
        	defaultHeight = maxDim;
 		} else {
    
    
     		defaultWidth = Math.max((int) (minDim * WALLPAPER_SCREENS_SPAN), maxDim);
     		defaultHeight = maxDim;
     		/*defaultWidth = realSize.x
     		defaultHeight = realSize.y */
 		}
 		sDefaultWallpaperSize = new Point(defaultWidth, defaultHeight);
 	}
 	return sDefaultWallpaperSize;
}

请先确认代码走那个分支(与设备分辨率有关),然后把defaultWidth 和defaultHeight 修改为屏幕宽高 defaultWidth = realSize.xdefaultHeight = realSize.y

2.替换系统默认静态壁纸

请用目标壁纸替换掉/frameworks/base/core/res/res目录下drawable-nodpi/drawable-xhpi/drawablexxhdpi/drawable-xxxhdpi这四个folder下面的default_wallpaper

3.只设置主屏幕壁纸,锁屏壁纸也变成桌面壁纸

这是Android N的默认设计,具体请参考WallpaperManagerService的setWallpaper方法,如下:

/* If we're setting system but not lock, and lock is currently sharing the system
 * wallpaper, we need to migrate that image over to being lock-only before
 * the caller here writes new bitmap data.
 */
  if (which == FLAG_SYSTEM && mLockWallpaperMap.get(userId) == null) {
    
    
      if (DEBUG) {
    
    
          Slog.i(TAG, "Migrating system->lock to preserve");
      }
      migrateSystemToLockWallpaperLocked(userId);
  }

可以把这段注释掉

猜你喜欢

转载自blog.csdn.net/wxd_csdn_2016/article/details/131114835