Story Behind Android desktop (launcher application) (5) -- add desktop Wallpaper

Source: Internet
Author: User

 Blog migration-I have migrated my blog to www.ijavaboy.com to better manage it. We are sorry for the inconvenience caused by no updates! New address of this article: Click me 

In the previous article, we learned how the workspace handles sliding between multiple celllayout. This article will record how to add the wallpaper to the desktop and how the workspace slides the wallpaper.

The addition of wallpaper is also provided by the calling system. It is called as follows:

// Call the built-in wallpaper selection function. When action_set_wallpaper is selected, intent chooseintent = new intent (intent. action_set_wallpaper); // select the application intent = new intent (intent. action_chooser); intent. putextra (intent. extra_intent, chooseintent); intent. putextra (intent. extra_title, "select Wallpaper"); startactivity (intent );

In this way, all applications whose actions contain Android. Intent. Action. set_wallpaper are listed. Of course, the above Code can also be abbreviated:

Intent chooseintent = new intent (intent. action_set_wallpaper); startactivity (intent. createchooser (chooseintent, "select Wallpaper "));

 

However, it is common to start startactivityforresult, and then get the returned wallpaper in onactivityresult. However, after selecting a wallpaper, we do not get the selected wallpaper in this way. How do we get the selected wallpaper?

In fact, when we select a wallpaper, the system will issue a broadcast and cache the selected wallpaper in the context of the entire application, it can be changed not only on the desktop, but also in the image display application. Therefore, the broadcast mechanism is used to notify the wallpaper that it has been replaced. We need to implement a broadcastreciever to listen to the wallpaper selection:

/*** When another app changes the wallpaper, define a broadcastreceiver to receive the notification. **/class wallpaperintentreceiver extends broadcastreceiver {private application; // weakreference prevents wallpaperintentreceiver from being deregistered by the launcher because of its reference. Private weakreference <uorderlauncher> rlauncher; Public wallpaperintentreceiver (Application application, uorderlauncher launcher) {This. application = application; this. rlauncher = new weakref Erence <uorderlauncher> (launcher);} public void setlauncher (uorderlauncher L) {This. rlauncher = new weakreference <uorderlauncher> (l) ;}@ overridepublic void onreceive (context, intent) {log. V (TAG, "changed Wallpaper");/*** get wallpaper from applicationcontext */FINAL drawable ldrawable = application. getwallpaper (); If (ldrawable instanceof bitmapdrawable) {log. V (TAG, "wallpaper is of the bitmapdrawable type"); mwallpaper = (bitma Pdrawable) ldrawable ). getbitmap ();} else {Throw new illegalstateexception ("the wallpaper must be a bitmapdrawable object");}/*** if the launcher is active at this time and is not locked, load the new wallpaper */If (rlauncher! = NULL) {final uorderlauncher launcher = rlauncher. Get (); If (launcher! = NULL) {launcher. loadwallpaper ();}}}}

In this broadcastreciever, get the latest wallpaper directly through application. getwallpaper (); so how do we know that this reciever is the broadcast that receives the replacement of the wallpaper? Therefore, we need to register it:

Private void registerintentreceivers () {If (mwallpaperreceiver = NULL) {mwallpaperreceiver = new wallpaperintentreceiver (getapplication (), this);/*** specifies the intentfilter when registering, in this way, the broadcastreciver is the broadcast that receives the wallpaper replacement */intentfilter filter = new intentfilter (intent. action_wallpaper_changed); getapplication (). registerreceiver (mwallpaperreceiver, filter);} else {mwallpaperreceiver. setlauncher (this );}}

Now that we know how to obtain the wallpaper, then we naturally think that I have obtained the wallpaper, but how can we draw it on the workspace? Note that the launcher is called when broadcastreceiver obtains the wallpaper. loadwallpaper () to load the wallpaper. In this method, we can see that it calls mworkspace. loadwallpaper (mwallpaper); then, you need to go back to the Workspace:

/*** To load the wallpaper here, you need to redraw the interface * @ Param wallpaper */Public void loadwallpaper (Bitmap wallpaper) {wallpaperloaded = true; mwallpaper = wallpaper; // re-draw the requestlayout (); invalidate ();}

This method only requires re-drawing the layout, so we know that the wallpaper should be painted in the method. In dispatchdraw, the wallpaper is processed,

/*** The wallpaper itself may not be as wide as the entire workspace *. Therefore, when the screen slides, the distance from the wallpaper to the right needs to be adjusted according to mwallpaperoffset */float x = getscrollx () * mwallpaperoffset; // What is the meaning here, todo: log. V (TAG, "getright-getleft =" + getright () + "-" + getleft () + "=" + (getright ()-getleft ())); /*** getright ()-getleft () = mobile phone screen width */If (x <getright ()-getleft ()-mwallpaperwidth) {// when the wallpaper width is smaller than the screen width, the wallpaper width is smaller than the screen width. // This is fixed. // However, when obtaining the wallpaper selected by the user, we have adjusted the size of it, so this is basically not the case here X = getright ( )-Getleft ()-mwallpaperwidth;} float y = (getbottom ()-gettop ()-mwallpaperheight)/2; log. V (TAG, "Start Wallpaper: X, Y are:" + x + "," + Y); // canvas. drawcolor (color. black); If (mwallpaper! = NULL) {log. V (TAG, "Start Wallpaper"); canvas. drawbitmap (mwallpaper, X, Y, mpaint); // invalidate ();}

Here, we can see that it uses canvas. drawbitmap (mwallpaper, X, Y, mpaint); Draws the wallpaper. The key here is the calculation of X and Y. The desktop can slide horizontally. When re-draw after each slide, the value of X is changing. Through code, we can find that there is a variable mwallpaperoffset, which searches for this variable and assigns a value to this variable in onmeasure:

// Load the wallpaper if (wallpaperloaded) {wallpaperloaded = false; mwallpaper = bitmaputils. centertofit (mwallpaper, width, height, getcontext (); mwallpaperwidth = mwallpaper. getwidth (); mwallpaperheight = mwallpaper. getheight (); log. V (TAG, "wallpaper Size Measurement:" + mwallpaperwidth + "," + mwallpaperheight);} final int wallpaperwidth = mwallpaperwidth; // calculate the gap between wallpaper and if (wallpaperwidth> width) {/*** calculate the sliding rate of the wallpaper * the distance between the wallpaper and Its sliding speed is Count * width-wallpaperwidth * the distance between the screens that can be swiped is (count-1) * width, the first division is the sliding rate of the wallpaper relative to the screen * // mwallpaperoffset = wallpaperwidth/(count * (float) width); mwallpaperoffset = (count * width-wallpaperwidth) /(count-1) * (float) width);} else {mwallpaperoffset = 1.0f;} log. V (TAG, "wallpaper offset:" + mwallpaperoffset );

In this way, each time we slide, workspace calculates this value during re-painting, and first draws the wallpaper in dispatchdraw, and then draws each celllayout.

Now, we should be clear about the wallpaper adding mechanism.

The next article will reveal the riddle of dragging items...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.