本次示例我们整合了apidemo里面的两个demo:SetWallpaper and Wallpaper
demo:SetWallpaper 主要是获取用户系统的壁纸,并随机颜色过滤后再设置为壁纸
demo:Wallpaper 主要展示使用壁纸作为activity背景的使用(使用用户设定的壁纸 request api level 10 or lower)
下面我们开始
1、 定义layout文件:一个imageview 和三个按钮
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/app_activity_wallpaper_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/app_activity_wallpaper_randomize_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="@string/app_activity_set_wallpaper_colorfilter_text" /> <Button android:id="@+id/app_activity_wallpaper_setwallpaper_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="@string/app_activity_set_wallpaper_setwallpaper_text" /> <Button android:id="@+id/app_activity_wallpaper_translucent_background_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="@string/app_activity_set_wallpaper_translucent_background_text" /> </LinearLayout> </FrameLayout>
2、随机颜色过滤壁纸和设置壁纸
final static private int[] mColors = {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.WHITE}; final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); final Drawable wallpaperDrawable = wallpaperManager.getDrawable(); final ImageView imageView = (ImageView) findViewById(R.id.app_activity_wallpaper_imageview); imageView.setDrawingCacheEnabled(true); imageView.setImageDrawable(wallpaperDrawable); Button randomColorFilterButton = (Button) findViewById(R.id.app_activity_wallpaper_randomize_btn); randomColorFilterButton.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v) { int iColor = (int) Math.floor(Math.random() * mColors.length); wallpaperDrawable.setColorFilter(mColors[iColor], PorterDuff.Mode.MULTIPLY); imageView.setImageDrawable(wallpaperDrawable); imageView.invalidate(); } }); Button setWallpaperButton = (Button) findViewById(R.id.app_activity_wallpaper_setwallpaper_btn); setWallpaperButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { wallpaperManager.setBitmap(imageView.getDrawingCache()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ finish(); } } });
这里需要注意一下在manifest中定义设置壁纸的权限
<uses-permission android:name="android.permission.SET_WALLPAPER" />
3、壁纸作为activity背景的使用:只要是使用了android的提供的Theme.Wallpaper主题(使用该主题要求api的版本最小为10)
<!-- Theme.Wallpaper request API level 10 or lower --> <style name="Theme.Wallpaper" parent="android:style/Theme.Wallpaper"> <item name="android:colorForeground">#fff</item> </style>
而后在manifest定义activity时设置上定义的主题Theme.Wallpaper
<activity android:name=".app.activity.TranslucentBackground" android:label="@string/app_activity_set_wallpaper_translucent_background_lable" android:theme="@style/Theme.Wallpaper"> </activity>
仅此而已就实现了想要的效果,上图:
原图
颜色过滤
设置为壁纸
设置为activity的背景