In the previous article in the main study to the skin resources built into the application of the way to achieve the basic idea of skinning, this article will continue the idea of learning skin resources built-in way to achieve the effect of skinning, but this article focuses on the application of skin-changing function of the code design implementation! The switch skin resources are located in different skin resource folders under assets.
The code structure of this demo program is as follows:
This article implements the skin-changing function code design UML class diagram as follows:
This demo's skin-changing effect is as follows:
The main implementation code is:
1, Skinconfigmanager.java
Role: Skin configuration management, encapsulated sharedpreferences to select the skin of the ordinal storage, and according to the skin type locked assets under the skin resources. The detailed code is as follows:
Package Com.ice.skininnerdemo;import android.content.context;import android.content.sharedpreferences;/** * Skin Configuration Management < single > </br> * encapsulates storage operations for Sharedpreferences objects * Created by ice on 14-10-9. */public class Skinconfigmanager {private static Skinconfigmanager Mskinconfigmanager; public static final String skinconfig = "Skinconfig"; public static final String Curskintypekey = "Curskintypekey"; private static sharedpreferences msharedpreferences; Private Skinconfigmanager (Context context) {msharedpreferences = context.getsharedpreferences (skinconfig, 0); } public synchronized static Skinconfigmanager getinstance (context context) {if (Mskinconfigmanager = = null) { Mskinconfigmanager = new Skinconfigmanager (context); } return Mskinconfigmanager; }/** * Sets the current selected skin type value (int type value) to Sharedpreferences * @param skintype Skin type */public void Setcurskintype (int skintype) {Sharedpreferences.editor Editor = Msharedpreferences.edit (); Editor.putint (Curskintypekey, Skintype); Editor.commit (); /** * Gets the current skin type <CURSKINTYPE> value currently stored in Sharedpreferences * @return * * * public int getcurskintype () { if (msharedpreferences! = null) {return msharedpreferences.getint (Curskintypekey, 0); } return 0; /** * Get the Skin folder name for the current skin resource under the assets folder * @return */public String Getskinfilename () {string skin FileName = null; Switch (Getcurskintype ()) {case 0://default skin type break; Case 1:skinfilename = "Skin_blue"; Break Case 2:skinfilename = "Skin_orange"; Break Case 3:skinfilename = "skin_red"; Break } return skinfilename; } public sharedpreferences Getskinconfigpreferences () {return msharedpreferences; }}
2, Skinmanager.java
Role: Skin Explorer, used to get picture resource files of different skin types under assets file
The detailed code is as follows:
Package Com.ice.skininnerdemo;import Android.content.context;import Android.content.res.assetmanager;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.drawable.drawable;import Java.io.ioexception;import java.io.inputstream;/** * Created by ice on 14-10-8. * Skin Explorer < singleton > */public class Skinmanager {private static Skinmanager Mskinmanager; Private Assetmanager Massetmanager; Private Skinmanager (Context context) {This.massetmanager = Context.getassets (); } public synchronized static Skinmanager getinstance (context context) {if (Mskinmanager = = null) {MS Kinmanager = new Skinmanager (context); } return Mskinmanager; /** * Gets the skin resources inside the assets based on the skin file name and resource file name drawable Object * @param skinfilename skin file name * @param filename resource file name * @return */Public drawable getskindrawable (string skinfilename, String fileName) {drawable drawable = Null try { InputStream InputStream = Massetmanager.open (Skinfilename + "/" + fileName); drawable = Drawable.createfromstream (InputStream, NULL); } catch (IOException e) {e.printstacktrace (); } return drawable; /** * Gets the skin resources inside the assets based on the skin file name and resource file name bitmap Object * @param skinfilename * @param filename * @return */Public Bitmap Getskinbitmap (string skinfilename, String fileName) {Bitmap image = null; try {inputstream InputStream = Massetmanager.open (Skinfilename + "/" + fileName); Image = Bitmapfactory.decodestream (InputStream); } catch (IOException e) {e.printstacktrace (); } return image; }}
3, Skinableactivity.java
Function: Change the activity abstract class, if the application of a ACTIVIY has the need to change skin, then inherit skinableactivity it!
It mainly realizes the Onsharedpreferencechangelistener listening interface, and registers the event listener of sharedpreferences content change.
The Changeskin () abstract method, which is implemented by the subclass of Skinableactivity, implements the replacement of the skin resources on the UI, while the supervisor hears sharedpreferences changes.
The detailed code is as follows:
Package Com.ice.skininnerdemo;import Android.app.activity;import android.content.sharedpreferences;/** * Skin Rejuvenation Activity Abstract class * Created by ice on 14-10-8. */public abstract class Skinableactivity extends Activity implements sharedpreferences.onsharedpreferencechangelistener{@Override protected void OnStart () {Super.onstart (); Initskin (); }/** * Initialize skin */private void Initskin () {Changeskin (); Register for monitoring, monitor the change of skin notification skinconfigmanager.getinstance (this). Getskinconfigpreferences (). Registeronsharedpref Erencechangelistener (this); }/** * Sharedpreferences when content is changed * @param sharedpreferences * key value in key sharedpreferences */@param @Override public void onsharedpreferencechanged (Sharedpreferences sharedpreferences, String key) {if (Skinco NfigManager.CURSKINTYPEKEY.equals (key)) {Changeskin (); }} @Override protected void OnStop () {super.onstop (); SkincoNfigmanager.getinstance (This). Getskinconfigpreferences (). Unregisteronsharedpreferencechangelistener (this); }/** * Change settings skin, skinableactivity subclasses must implement the method to complete the skinning process */protected abstract void Changeskin ();}
4, Skinsettingactivity.java
Function: Skin set activity, inherit skinableactivity, implement Changeskin () abstract method, complete skin change on skin set UI.
(Other activity in the application that requires skin resurfacing is similar, mainly in the Changeskin () method to achieve the interface of skin-changing needs )
The detailed code is as follows:
Package Com.ice.skininnerdemo;import Android.graphics.drawable.drawable;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.widget.adapterview;import Android.widget.GridView;import Android.widget.simpleadapter;import Android.widget.textview;import Java.util.arraylist;import Java.util.HashMap; Import java.util.list;/** * Skin settings activity * Created by ice on 14-10-10. */public class Skinsettingactivity extends skinableactivity{private static final String TAG = "skinsettingactivity"; Private GridView Gv_skin_type; Private TextView tv_skin_cur; Private TextView tv_title_skin_setting; Private int[] Skintypeimage = new int[]{R.drawable.overview_skin_default, R.drawable.overview_skin_blue, R.drawable.overview_skin_orange, r.drawable.overview_skin_red}; Private int[] Skintypename = new int[]{R.string.skin_default, R.string.skin_blue, R.string.skin_ora Nge, r.string.skin_red}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_skin_setting); Initview (); Bindevent (); } private void Initview () {tv_title_skin_setting = (TextView) Findviewbyid (r.id.tv_title_skin_setting); Tv_skin_cur = (TextView) Findviewbyid (r.id.tv_skin_cur); Gv_skin_type = (GridView) Findviewbyid (R.id.gv_skin_type); Setgridviewadapter (Gv_skin_type); private void Setgridviewadapter (GridView mgridview) {list
----OK, this demo to achieve the main code of skin change, design ideas in this! Xiao Lv actually thought about another way to configure the skin resources,
The general idea is as follows:
1. Under the assets file, create a new skin resource profile such as: Skin.properties or Skin_configure.xml
2, all the skin resources (Pictures) will be placed under the res/drawable/, and what skin type of the corresponding skin resources image will be in 1 skin.properties or skin_configure.xml Referenced in a configuration way.
Of course, this is only a small LU at present a preliminary ideas and design ideas, if you see here if you have other design ideas to achieve the skin, but also hope that the atmosphere of the small LU message, learning, and communication.
Supplemental Content: This demo contains a picture of the. 9 format in the Get assets file.
About getting assets in the following. 9 format picture problem, here is very interesting,
The. 9 format images in the demo assets are compiled with the AAPT command and compiled by the Android system.
Then why did you do it? You can first study on their own or on the Internet to search for answers, small Lu behind the time will be organized into a blog.
Finally attach this demo code < free download points;: http://download.csdn.net/detail/l416112167/8027581
-----------------
The next small LU will learn and introduce the second way of realization: [Separation of skin resources and applications]
Android Learning for skin-changing function modules < two >