正文  UI设计 > Launcher >

Luncher修改wallpaper(壁纸)

以下将为大家简单的分析一下源代码中luncher修改壁纸的过程(一下代码均来自Luncher源代码):一.当我们在luncher界面按下menu的时候,第三个选项就是Wallpaper,定义如下(源码1116行,局部),menu.add第二项既......

 以下将为大家简单的分析一下源代码中luncher修改壁纸的过程(一下代码均来自Luncher源代码):

 

一.当我们在luncher界面按下menu的时候,第三个选项就是Wallpaper,定义如下(源码1116行,局部),menu.add第二项既是选择wallpaper:

 

源码copy to clipboard打印?
  1. public boolean onCreateOptionsMenu(Menu menu) {
  2. if (isWorkspaceLocked()) {
  3. return false;
  4. }
  5.  
  6. super.onCreateOptionsMenu(menu);
  7.  
  8. menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
  9. .setIcon(android.R.drawable.ic_menu_add)
  10. .setAlphabeticShortcut('A');
  11. menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
  12. .setIcon(android.R.drawable.ic_menu_gallery)
  13. .setAlphabeticShortcut('W');
public boolean onCreateOptionsMenu(Menu menu) {
        if (isWorkspaceLocked()) {
            return false;
        }

        super.onCreateOptionsMenu(menu);

        menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
                .setIcon(android.R.drawable.ic_menu_add)
                .setAlphabeticShortcut('A');
        menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
                 .setIcon(android.R.drawable.ic_menu_gallery)
                 .setAlphabeticShortcut('W');

二.当我们按下Wallpaper触发什么事件呢?看一下代码(源代码1171行):

 

 

源码copy to clipboard打印?
  1. @Override
  2. public boolean onOptionsItemSelected(MenuItem item) {
  3. switch (item.getItemId()) {
  4. case MENU_ADD:
  5. addItems();
  6. return true;
  7. case MENU_WALLPAPER_SETTINGS:
  8. startWallpaper();
  9. return true;
  10. case MENU_SEARCH:
  11. onSearchRequested();
  12. return true;
  13. case MENU_NOTIFICATIONS:
  14. showNotifications();
  15. return true;
  16. }
  17.  
  18. return super.onOptionsItemSelected(item);
  19. }
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_ADD:
                addItems();
                return true;
            case MENU_WALLPAPER_SETTINGS:
                startWallpaper();
                return true;
            case MENU_SEARCH:
                onSearchRequested();
                return true;
            case MENU_NOTIFICATIONS:
                showNotifications();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

我们看到:

 

而它调用的事件是

源码copy to clipboard打印?
  1. startWallpaper();
startWallpaper();

 

你们看到的图片跟我的都不一样,呵呵,这是因为我写了一个很简单的demo,然后就系统给调用了,所以你们看到一个比你们多了一个wallpaperdemo。在下一篇日志中,我会说下这个demo是如何实现的。

三.下面让我们看下startWallpaper()这个方法(源代码1370行):

 

源码copy to clipboard打印?
  1. private void startWallpaper() {
  2. closeAllApps(true);
  3. final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
  4. Intent chooser = Intent.createChooser(pickWallpaper,
  5. getText(R.string.chooser_wallpaper));
  6. // NOTE: Adds a configure option to the chooser if the wallpaper supports it
  7. // Removed in Eclair MR1
  8. // WallpaperManager wm = (WallpaperManager)
  9. // getSystemService(Context.WALLPAPER_SERVICE);
  10. // WallpaperInfo wi = wm.getWallpaperInfo();
  11. // if (wi != null && wi.getSettingsActivity() != null) {
  12. // LabeledIntent li = new LabeledIntent(getPackageName(),
  13. // R.string.configure_wallpaper, 0);
  14. // li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
  15. // chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
  16. // }
  17. startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
  18. }
private void startWallpaper() {
        closeAllApps(true);
        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
        Intent chooser = Intent.createChooser(pickWallpaper,
                getText(R.string.chooser_wallpaper));
        // NOTE: Adds a configure option to the chooser if the wallpaper supports it
        //       Removed in Eclair MR1
//        WallpaperManager wm = (WallpaperManager)
//                getSystemService(Context.WALLPAPER_SERVICE);
//        WallpaperInfo wi = wm.getWallpaperInfo();
//        if (wi != null && wi.getSettingsActivity() != null) {
//            LabeledIntent li = new LabeledIntent(getPackageName(),
//                    R.string.configure_wallpaper, 0);
//            li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
//            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
//        }
        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
    }
很多人可能会对他是如何实现跳转设置的呢?Intent.createChooser()这个方法的实现原理赶到好奇,因为你在Luncher源代码里面找不到Live Wallpapers和Galleryde的实现。因为他是通过一个类似广播的机制。