zoukankan      html  css  js  c++  java
  • 实现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;
                }
            }
    }

         

  • 相关阅读:
    7、猜年龄
    6、continue语句
    5、break语句
    4、while循环练习
    poj 2378
    poj 2342
    poj 2287
    poj 2228
    poj 1191
    srm 578 dv2 1000pt
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5307978.html
Copyright © 2011-2022 走看看