天天看点

launcher3增加壁纸到桌面壁纸列表中

在写本文之前,首先推荐一篇详细介绍Launcher3桌面壁纸的博客,博客链接为:Launcher3--壁纸:http://blog.csdn.net/dingfengnupt88/article/details/51833046

本文就不详细叙述launcher3添加修改壁纸的原理了,直接提供一个方法,把您喜欢的图片添加到桌面壁纸列表中。

/**
     * 增加自定义桌面壁纸
     *
     * @param wallpaperPath 桌面壁纸路径文件夹
     */
    private void addMyWallpaper(String wallpaperPath, ArrayList<WallpaperTileInfo> bundled) {

        try {
            File mWallpaperDir = new File(wallpaperPath);
            if (mWallpaperDir.exists()) {
                if (mWallpaperDir.isDirectory()) {
                    File[] files = mWallpaperDir.listFiles();
                    for (File file : files) {
                        if (!file.isFile()) {
                            continue;
                        }
                        String name = file.getName();
                        int dotPos = name.lastIndexOf('.');
                        String extension = "";
                        if (dotPos >= -1) {
                            extension = name.substring(dotPos);
                            name = name.substring(0, dotPos);
                        }

                        if (!extension.endsWith("png") && !extension.endsWith("jpg")) {
//                    if (!extension.endsWith("jpg")) {
                            // PNG格式会出错,但设置没有问题,JPG格式则不会出现出错日志
                            continue;
                        }

                        File thumbnail = new File(mWallpaperDir, name + extension);
                        String absolutePath = thumbnail.getAbsolutePath();
                        Log.d(TAG, "wallpaper file path name: " + absolutePath);
//                    Bitmap myThumbnail = createMyThumbnail(this, thumbnail.getAbsolutePath(), 0, false);
                        Bitmap myThumbnail = BitmapFactory.decodeFile(thumbnail.getAbsolutePath());
                        bundled.add(new FileWallpaperInfo(thumbnail, new BitmapDrawable(myThumbnail)));
                    }
                }
            } else
                Log.e(TAG, "File path " + wallpaperPath + " not exists");
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
    }
           

上面这个方法就是通过扫描目标文件夹,把文件夹中的图片添加到壁纸列表中。

该方法可在WallpaperPickerActivity中的findBundledWallpapers方法中执行,这样就可以添加自己想要的图片到壁纸列表中。

通过代码修改桌面默认壁纸,添加或删除动态桌面壁纸等内容的修改,请仔细阅读推荐的博客,里面对launcher3的壁纸有十分详细的介绍。

继续阅读