实现Launcher默认壁纸、选择壁纸定制化功能

简介:
需求功能说明:
    该定制需求为在系统中添加一个新的分区如myimage。用以实现存放定制资源。比如在myimage下新建wallpaper目录用于存放定制的墙纸图片资源,当Launcher载入默认墙纸或者选择设置墙纸时会优先从该路径下读取资源。


第一部分:客制化默认壁纸:
    Launcher默认的壁纸配置是放在frameworkres以下配置的,图片也是放在framework以下,对于独立的第三方Launcher要想绕开framework实现默认壁纸则须要自身实现设置默认壁纸的功能。因此要在Launcher第一次执行或者重置时设置默认壁纸。实现方式为在Launcher.java类的onCreate()方法下的showFirstRunWorkspaceCling()执行设置默认壁纸的功能代码,例如以下:

  /*封装设置默认壁纸的方法*/

    private void setDefaultWallpaper(){

        WallpaperManager wm = (WallpaperManager)getSystemService(Context.WALLPAPER_SERVICE);

        try{

            wm.setBitmap(getBitmap("/myimage/wallpaper/wallpaper_01.jpg"));

        }catch(Exception e){

            e.printStackTrace();

        }

    }

    /*得到绝对路径下的图片为bitmap类型*/

    public Bitmap getBitmap(String path) {

        Bitmap bitmap = null;

        File file = new File(path);

        if (file.exists())

            bitmap = BitmapFactory.decodeFile(path);

            return bitmap;

        }

    /*第一次启动时显示的指导画面*/

    public void showFirstRunWorkspaceCling() {

        setDefaultWallpaper();

        ......

    }

第二部分 客制化选择壁纸:

    因为在Launcher2中对墙纸资源的引用是通过id引用,可是当前客制化定制的文件路径为绝对路径如/myimage/wallpaper,也就说须要通过路径进行引用,因此改动例如以下:

    第一步:wallpapers.xml

        如:

        <item>wallpaper_01</item>

        <item>wallpaper_02</item>

        <item>wallpaper_04</item>

        <item>wallpaper_05</item>

        <item>wallpaper_06</item>

        <item>wallpaper_07</item>

        <item>wallpaper_08</item>

注:每一项的值应与实际图片名字同样

    第二步:改动WallpaperChooserDialogFragment.java类

 
 
    private ArrayList<String> mThumbs;
    private ArrayList<String> mImages;
    private void selectWallpaper(int position) {
        if (LauncherLog.DEBUG) {
            LauncherLog.d(TAG, "selectWallpaper: position = " + position + ", this = " + this);
        }
        try {
            WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(
                    Context.WALLPAPER_SERVICE);
            wpm.setBitmap(getBitmap(mImages.get(position)));
            Activity activity = getActivity();
            activity.setResult(Activity.RESULT_OK);
            activity.finish();
        } catch (IOException e) {
            Log.e(TAG, "Failed to set wallpaper: " + e);
        }
    }
 
    public Bitmap getBitmap(String path) {
        Bitmap bitmap = null;
        File file = new File(path);
        if (file.exists())
            bitmap = BitmapFactory.decodeFile(path);
        return bitmap;
    }
    private void findWallpapers() {
        mThumbs = new ArrayList<String>();
        mImages = new ArrayList<String>();
 
        final Resources resources = getResources();
        final String packageName = resources.getResourcePackageName(R.array.wallpapers);
        addWallpapers(resources, packageName, R.array.wallpapers);
        addWallpapers(resources, packageName, R.array.extra_wallpapers);
    }
 
    private void addWallpapers(Resources resources, String packageName, int list) {
        final String[] extras = resources.getStringArray(list);
        for (String extra : extras) {
            String res="/myimage/wallpaper/"+extra+".jpg";
            String thumbRes="/myimage/wallpaper/"+extra+"_small.jpg";
            mThumbs.add(thumbRes);
            mImages.add(res);
        }
    }
 
 
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
 
            if (convertView == null) {
                view = mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
            } else {
                view = convertView;
            }
 
            ImageView image = (ImageView) view.findViewById(R.id.wallpaper_image);
 
            String thumbRes = mThumbs.get(position);
            image.setImageBitmap(getBitmap(thumbRes));
            Drawable thumbDrawable = image.getDrawable();
            if (thumbDrawable != null) {
                thumbDrawable.setDither(true);
            } else {
                Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #"
                        + position);
            }
 
            return view;
        }
    }
        @Override
        protected Bitmap doInBackground(Integer... params) {
            if (isCancelled() || !isAdded()) {
                LauncherLog.d(TAG, "WallpaperLoader doInBackground: canceled = " + isCancelled()
                        + ",isAdded() = " + isAdded() + ",activity = " + getActivity());
                return null;
            }
            try {
                return BitmapFactory.decodeFile(mImages.get(params[0]), mOptions);
            } catch (OutOfMemoryError e) {
                LauncherLog.e(TAG, "WallpaperLoader decode resource out of memory " + e.getMessage());
                return null;
            }
        }
}





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5307978.html,如需转载请自行联系原作者
相关文章
|
iOS开发
iOS 启动图设置及icon图标设置
OS 启动图设置及icon图标设置
736 0
iOS 启动图设置及icon图标设置
|
XML Java Android开发
Android6.0 源码修改之Settings音量调节界面增加通话音量调节
Android6.0 源码修改之Settings音量调节界面增加通话音量调节
58 0
|
Java Android开发
关于android studio开发APP中,给单个Activity设置隐藏上面标题栏的简单方案。
关于android studio开发APP中,给单个Activity设置隐藏上面标题栏的简单方案。
208 0
关于android studio开发APP中,给单个Activity设置隐藏上面标题栏的简单方案。
|
缓存 Android开发
解决bug:Android 更换新logo图标后,运行项目图标没有变化
解决bug:Android 更换新logo图标后,运行项目图标没有变化
549 0
|
iOS开发
手机桌面应用图标和APP启动画面全尺寸
手机桌面应用图标和APP启动画面全尺寸
手机桌面应用图标和APP启动画面全尺寸