Launcher2中的壁纸选择器在Launcher2 这个Application中是一个Activity,先看一下Manifest.xml
1 | <activity |
2 | android:name="com.android.launcher2.WallpaperChooser" |
3 | android:theme="@style/Theme.WallpaperPicker" |
4 | android:label="@string/pick_wallpaper" |
5 | android:icon="@mipmap/ic_launcher_wallpaper" |
6 | android:finishOnCloseSystemDialogs="true" |
7 | android:process=":wallpaper_chooser"> |
8 | <intent-filter> |
9 | <action android:name="android.intent.action.SET_WALLPAPER" /> |
10 | <category android:name="android.intent.category.DEFAULT" /> |
11 | </intent-filter> |
12 | <meta-data android:name="android.wallpaper.preview" |
13 | android:resource="@xml/wallpaper_picker_preview" /> |
14 | </activity> |
1,壁纸选择器的启动方式有两种,一种是在Workspace的空白处长按,另外一种是在Workspace按MENU键
a,长按启动
->
startWallpaper();
->
startActivityForResult(chooser, REQUEST_PICK_WALLPAPER );
其中chooser 是一个Intent
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER );
=>启动了wallpaperChooser Activity
WallpaperChooser.java
=>
WallpaperChooserDialogFragment. newInstance();
=>
WallpaperChooserDialogFragment.java
public static WallpaperChooserDialogFragment newInstance() {
WallpaperChooserDialogFragment fragment = new WallpaperChooserDialogFragment();
fragment.setCancelable( true);
return fragment;
}
启动的这个小窗口就是一个DialogFragment
先看这下WallpaperChooser 这个Activity的布局
wallpaper_chooser_base.xml调用wallpaper_chooser.xml
里面三个就是一个ListView的View
<Gallery android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spacing="-4dp" />
大概Layout如下
b,menu启动
重写onCreateOptionsMenu()
@Override
public boolean onCreateOptionsMenu (Menu menu) {
增加wallpaper的选项
menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
.setIcon(android.R.drawable. ic_menu_gallery)
.setAlphabeticShortcut( 'W');
在onOptionsItemSelected()函数是响应这个Item选择。殊途同归。
case MENU_WALLPAPER_SETTINGS :
startWallpaper();
return true;
}
2,Question:
为什么在workspace界面按menu有wallpaper的选项,而在customerview/all app 却没有wallpaper的选项。暂时还没找到逻辑。