/*封装设置默认壁纸的方法*/
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);
} 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;
}
}
}