天天看點

實作Launcher預設桌面、選擇桌面定制化功能

需求功能說明:

    該定制需求為在系統中添加一個新的分區如myimage。用以實作存放定制資源。比如在myimage下建立wallpaper目錄用于存放定制的牆紙圖檔資源,當Launcher載入預設牆紙或者選擇設定牆紙時會優先從該路徑下讀取資源。

第一部分:客制化預設桌面:

    Launcher預設的桌面配置是放在framework下res以下配置的,圖檔也是放在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);

                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());

}

本文轉自mfrbuaa部落格園部落格,原文連結:http://www.cnblogs.com/mfrbuaa/p/5307978.html,如需轉載請自行聯系原作者