今天看了下android源码中的关于壁纸设定程序。它通过一个Gallery对象来实现拖动选择的功能。
结合源码分析如下:
package com.android.launcher2; import com.android.launcher.R; import android.app.Activity; import android.app.DialogFragment; import android.app.Fragment; import android.os.Bundle; public class WallpaperChooser extends Activity { private static final String TAG = "WallpaperChooser"; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.wallpaper_chooser_base); Fragment fragmentView = getFragmentManager().findFragmentById(R.id.wallpaper_chooser_fragment); //跳转到WallpaperChooserDialogFragment if (fragmentView == null) { DialogFragment fragment = WallpaperChooserDialogFragment.newInstance(); //当获得的fragment为空时,会调用wallpaperchooserdialogfragment中的newinstance方法 fragment.show(getFragmentManager(), "dialog"); } } }
该程序是当用户单击桌面菜单中的wallpaper按钮后进入的第一个Activity,他只是简单定义了一个只含有一个fragment的Framelayout,然后系统会根据其中定义的fragment自动跳转到下一个文件:WallpaperChooserDialogFragment。
wallpaper_chooser_base的代码如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="com.android.launcher2.WallpaperChooserDialogFragment" //根据路径直接跳转 android:id="@+id/wallpaper_chooser_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>
然后就是对于WallpaperChooserDialogFragment的分析,部分源码及分析如下:
public class WallpaperChooserDialogFragment extends DialogFragment implements AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener { ... public static WallpaperChooserDialogFragment newInstance() { //该方法会在WallpaperChooser中被调用,作用相当于回调。 WallpaperChooserDialogFragment fragment = new WallpaperChooserDialogFragment(); fragment.setCancelable(true); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { //该方法会在创建fragment时被调用,用来初始化参数 ... } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { //在该方法中创建对话框 ... } @Override public void onDismiss(DialogInterface dialog) { //该方法会在对话框解除时被调用,用来确保Activity的状态 ... } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, //重头戏,选择界面的铺设,拖动、选择神码的都在这里完成 Bundle savedInstanceState) { ... } private void selectWallpaper(int position) { //根据获得的position,在这里进行壁纸设置的实现 ... } private void findWallpapers() { //获得系统中的壁纸资源 ... } private void addWallpapers(Resources resources, String packageName, int list) { //定义了获得壁纸资源的方法,供findWallpaper调用 ... } private class ImageAdapter extends BaseAdapter implements ListAdapter, SpinnerAdapter { //内部类,为onCreateView所用,根据壁纸的拖动设置本界面的大背景 private LayoutInflater mLayoutInflater; ImageAdapter(Activity activity) { mLayoutInflater = activity.getLayoutInflater(); } public int getCount() { return mThumbs.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ... } } class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> { //内部类,如其命名,提供了加载Wallpaper的方法,在生命周期中多有调用 ... } static class WallpaperDrawable extends Drawable { //最基本的draw实现,以及壁纸图片的各个属性设置 ... }