Android Easy to replace skin

Source: Internet
Author: User

Skin replacement

1. Changing the skin is actually changing the background image of the activity

Directly on the code:

Activity:

1  Public classMainactivityextendsActivityImplementsOnclicklistener {2 3     PrivateSkinmanager Skinmanager;4     Private intdownnums;5     PrivateButton btn;6 7 @Override8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); OneSkinmanager =NewSkinmanager ( This); A skinmanager.init (); -BTN =(Button) Findviewbyid (R.id.btn_skin); -Btn.setonclicklistener ( This); the     } -  -     /** - * Background Switch +      */ - @Override +      Public Booleanontouchevent (Motionevent event) { A         Switch(Event.getaction ()) { at          CaseMotionevent.action_down: -downnums++; -Skinmanager.toggleskin (downnums% 5); -              Break; -  -         } in  -         return Super. Ontouchevent (event); to     } +  -     /** the * Click to pop up the theme selection box, that is, select the picture *      */ $ @OverridePanax Notoginseng      Public voidOnClick (View v) { -  the         Switch(V.getid ()) { +          CaseR.id.btn_skin: AGridView Grid =NewGridView ( This); thelistNewArraylist(); +hashmap<string, object> map =NewHashmap<string, object>(); -Map.put ("Imgid", r.drawable.default_wallpaper); $ list.add (map); $hashmap<string, object> map1 =NewHashmap<string, object>(); -Map.put ("Imgid", r.drawable.wallpaper_c); - List.add (MAP1); theSimpleadapter adapter =NewSimpleadapter ( This, List, -R.layout.item,NewString[] {"Imgid" },Wuyi                     New int[] {r.id.image}); the Grid.setadapter (adapter); -             NewAlertdialog.builder ( This). Settitle ("Select Picture"). Setview (GRID) Wu . Create (). Show (); -              Break; About  $         } -     } -}

Activity_main.xml

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 Tools:context=". Mainactivity " >7 8     <TextView9         Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One Android:text= "@string/hello_world" /> A  -     <Button -         Android:id= "@+id/btn_skin" the Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:text= "Skin Selection" /> -  + </LinearLayout>

Item.xml

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7     <ImageView8         Android:id= "@+id/image"9 Android:layout_width= "70DP"Ten Android:layout_height= "70DP" /> One  A </LinearLayout>

Skinmanager.class

1  Public classSkinmanager {2 3     PrivateString spname = "Skin_storage";4 5     Privatesharedpreferences sp;6 7     PrivateActivity activity;8 9     Private intDrawablearray[] ={r.drawable.default_wallpaper,Ten r.drawable.wallpaper_c, R.drawable.wallpaper_d, One r.drawable.wallpaper_f, r.drawable.wallpaper_g}; A  -      PublicSkinmanager (activity activity) { -          This. Activity =activity; theSP = activity.getsharedpreferences (spname, 0); -     } -  -     /** + * Save Skin ID -      */ +     Private voidSaveimgid (intID) { AString editorname = "SkinID"; atSharedpreferences.editor Editor =Sp.edit (); - Editor.putint (Editorname, ID); - editor.commit (); -     } -  -     /** in * Get current skin -      */ to     Private intGetcurrentimgid () { +String editorname = "SkinID"; -         returnSp.getint ("SkinID", r.drawable.default_wallpaper); the  *     } $ Panax Notoginseng      Public voidToggleskin (inti) { -         Try { the Activity.getwindow () + . Setbackgrounddrawableresource (Drawablearray[i]); A}Catch(Exception e) { the         } +     } -  $      Public voidinit () { $  -Activity.getwindow (). Setbackgrounddrawableresource (drawablearray[0]); -     } the  -}

SOURCE download

Android Easy to replace skin

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.