Apad Qzone Project Summary (2)-skin swap function implementation !!!

Source: Internet
Author: User

Hi, everyone. New Year's day is coming soon. I wish you a happy New Year's Day in advance. (* ^__ ^ *) Hey, today I will share with you the IMPLEMENTATION OF THE apad Qzone skin change function, first, let's take a look at the effect:

Figure 1: Default skin.

Figure 2: Click the skin care button in the menu to apply skin replacement.

From the above, we can see that the skin replacement function of apad Qzone is actually very easy to implement. Because the whole application adopts the single activity implementation method, replacing the background is actually the background of the activity that replaces the main program.

Here we put several skins in the Res/drawable directory in advance, then use sharedpreferences to record the resource ID of the current skin, And then load the activity background when the program starts.

To make it easier for everyone to understand, I have made a simple demo here. The steps are as follows:

Step 1: Create an android project named skindemo. The program structure is as follows:

Step 2: Create a skin management class skinsettingmanager. java. The Code is as follows:

Package COM. tutor. skindemo; import android. app. activity; import android. content. sharedpreferences;/*** padqzone skin manager * @ author frankiewei **/public class skinsettingmanager {public final static string skin_pref = "skinsetting"; Public sharedpreferences skinsettingpreference; private int [] skinresources = {R. drawable. default_wallpaper, R. drawable. wallpaper_c, R. drawable. wallpaper_d, R. drawable. wallpaper_f, R. drawable. wallpaper_g}; private activity mactivity; Public skinsettingmanager (activity) {This. mactivity = activity; skinsettingpreference = mactivity. getsharedpreferences (skin_pref, 3);}/*** get the skin Number of the current program ** @ return */Public int getskintype () {string key = "skin_type"; return skinsettingpreference. getint (Key, 0);}/*** write the skin number to the global settings. ** @ Param J */Public void setskintype (Int J) {sharedpreferences. editor editor = skinsettingpreference. edit (); string key = "skin_type"; Editor. putint (Key, J); Editor. commit ();}/*** obtain the background image resource ID of the current skin ** @ return */Public int getcurrentskinres () {int skinlen = skinresources. length; int getskinlen = getskintype (); If (getskinlen >=skinlen) {getskinlen = 0;} return skinresources [getskinlen];} /*** switch skin using the skin button in the navigation bar */Public void toggleskins () {int skintype = getskintype (); If (skintype = skinresources. length-1) {skintype = 0;} else {skintype ++;} setskintype (skintype); mactivity. getwindow (). setbackgrounddrawable (null); try {mactivity. getwindow (). setbackgrounddrawableresource (getcurrentskinres ();} catch (throwable e) {e. printstacktrace () ;}/ *** used to initialize the skin */Public void initskins () {mactivity. getwindow (). setbackgrounddrawableresource (getcurrentskinres ());}}

Step 3: Call skindemoactivity. Java in the main activity of the application. The Code is as follows:

Package COM. tutor. skindemo; import android. app. activity; import android. OS. bundle; import android. view. motionevent; public class skindemoactivity extends activity {private skinsettingmanager msettingmanager; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // initialize the skin msettingmanager = new skinsettingmanager (this); msettingmanager. initskins () ;}// here to achieve simple skin replacement @ override public Boolean ontouchevent (motionevent event) {msettingmanager. toggleskins (); return Super. ontouchevent (event );}}

The above three steps are all done !, Haha, it's easy. Let's talk about it today. I wish you a happy New Year !!!

Click to enter the source code ==>

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.