Android engineering implements wallpaper change function

Source: Internet
Author: User

In recent work, we need to implement the small wallpaper function and make the code into a demo.

The zip format is not used for skin replacement, because only an image background is replaced.

1. Put the three images into drawable-hdpi. I put the three images 480*800.

2. Use sharedpreference to access the skin ID, so that you can select the skin to use based on the ID at next startup, and refresh the skin in onresume ().

3. The data stored in sharedpreference uses string, and the image ID cannot be used, because the image ID is generated again after each program starts.

Let's just talk about the code.

Public class changeskinactivity extends activity {Private Static final string skin_id = "skin_id"; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. requestwindowfeature (window. feature_no_title); setcontentview (R. layout. main); button btnchangeskin = (button) findviewbyid (R. id. btnchangeskin); btnchangeskin. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {If ("bg0 ". equals (getskinresourcename () {setskinresourcename ("Bg1");} else if ("Bg1 ". equals (getskinresourcename () {setskinresourcename ("bg2");} else if ("bg2 ". equals (getskinresourcename () {setskinresourcename ("bg0") ;}refreshskin () ;}}) ;}@ override protected void onresume () {super. onresume (); refreshskin ();}/*** change skin * 1. click the shortcut menu for skin replacement * 2. after the program runs */private void refreshskin () {int skinid = getskinresourceid (); findviewbyid (R. id. layout ). setbackgroundresource (skinid);} private int getskinresourceid () {int skinid = R. drawable. bg0; string skinname = getskinresourcename (); If (skinname. equals ("Bg1") {skinid = R. drawable. bg1;} else if (skinname. equals ("bg2") {skinid = R. drawable. bg2;} return skinid;} private string getskinresourcename () {try {sharedpreferences preferences = getsharedpreferences ("skinxml", context. mode_private); Return preferences. getstring (skin_id, "bg0");} catch (exception e) {e. printstacktrace (); Return "bg0" ;}} private void setskinresourcename (string skinname) {sharedpreferences preferences = getsharedpreferences ("skinxml", context. mode_private); Editor editor = preferences. edit (); Editor. putstring (skin_id, skinname); Editor. commit ();}}

Then apply the results

: Http://

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: 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.