實現Launcher默認壁紙、選擇壁紙定製化功能

需求功能說明:
    該定製需求爲在系統中增長一個新的分區如myimage,用以實現存放定製資源。例如在myimage下新建wallpaper文件夾用於存放定製的牆紙圖片資源,當Launcher加載默認牆紙或者選擇設置牆紙時會優先從該路徑下讀取資源。


第一部分:客製化默認壁紙:
    Launcher默認的壁紙配置是放在frameworkres下面配置的,圖片也是放在framework下面,對於獨立的第三方Launcher要想繞開framework實現默認壁紙則須要自身實現設置默認壁紙的功能。所以要在Launcher第一次運行或者重置時設置默認壁紙。實現方式爲在Launcher.java類的onCreate()方法下的showFirstRunWorkspaceCling()執行設置默認壁紙的功能代碼,以下:

  /*封裝設置默認壁紙的方法*/

    private void setDefaultWallpaper(){java

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

        try{this

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

        }catch(Exception e){code

            e.printStackTrace();xml

        }圖片

    }資源

    /*獲得絕對路徑下的圖片爲bitmap類型*/get

    public Bitmap getBitmap(String path) {it

        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;
            }
        }
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息