Package COM. easyWay. android. ui; import Java. io. ioexception; import android. app. activity; import android. app. wallpapermanager; import android. graphics. color; import android. graphics. porterduff; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imageview;/*** get and set the wallpaper of the mobile phone in Android *** // get the wallpaper manager * Final wallpapermanager = wallpapermanager. getinstance (this); * // get the wallpaper image * Final drawable wallpaperdrawable = wallpapermanager. getdrawable (); * method for setting wallpaper * // change wallpaper manager. setbitmap (imageview. getdrawingcache (); * @ author longgangbai **/public class androidwallpaperactivity extends activity {final static private int [] mcolors = {color. blue, color. green, color. red, color. ltgray, color. magenta, color. cyan, color. yellow, color. white};/*** initialization of the activity after it is first created. must at least * call {@ link android. app. activity # setcontentview ()} to * describe what is to be displayed in the screen. * // @ override protected void oncreate (bundle savedinstancestate) {// be sure to call the super class. super. oncreate (savedinstancestate); // see Res/layout/wallpaper_2.xml for this // view layout definition, which is being set here as // the content of our screen. setcontentview (R. layout. wallpaper); // obtain the wallpaper manager final wallpapermanager = wallpapermanager. getinstance (this); // get the wallpaper image final drawable wallpaperdrawable = wallpapermanager. getdrawable (); // Image view final imageview = (imageview) findviewbyid (R. id. imageview); // draw the cached imageview. setdrawingcacheenabled (true); // sets the image imageview. setimagedrawable (wallpaperdrawable); // change the wallpaper color. Button randomize = (button) findviewbyid (R. id. randomize); randomize. setonclicklistener (New onclicklistener () {public void onclick (view) {int mcolor = (INT) math. floor (math. random () * mcolors. length); wallpaperdrawable. setcolorfilter (mcolors [mcolor], porterduff. mode. multiply); imageview. setimagedrawable (wallpaperdrawable); // call this method to re-call ondraw to draw the imageview of the entire interface. invalidate () ;}}); // button setwallpaper = (button) findviewbyid (R. id. setwallpaper); setwallpaper. setonclicklistener (New onclicklistener () {public void onclick (view) {try {// change wallpaper wallpapermanager. setbitmap (imageview. getdrawingcache (); finish ();} catch (ioexception e) {e. printstacktrace ();}}});}}
Address: http://topmanopensource.iteye.com/blog/1310542 #