Implementation of the skin replacement function module for Android Learning (2)

Source: Internet
Author: User

Implementation of the skin replacement function module for Android Learning (2)

In the previous article, I learned the basic idea of Using Skin Resources built into applications to achieve skin replacement, this article will continue with the above ideas to learn the built-in methods of skin replacement, but this article focuses on the Code Design and Implementation of the skin replacement function in the application! The skin resource to be switched is located in different skin resource folders under assets.

The code structure of this demo program is as follows:

The code design UML class diagram for skin replacement is as follows:


The skin-changing effect of this demo is as follows:





The main implementation code is:

1. SkinConfigManager. java

Role: Skin Configuration Management encapsulates the sequence number Storage of SharedPreferences on the selected skin, and locks the skin resources under assets according to the skin type. The Code is as follows:

Package com. ice. skininnerdemo; import android. content. Context; import android. content. SharedPreferences;/*** skin Configuration Management
 <单例> 
  
* Stores the SharedPreferences object. * 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 ;} /*** set and save the 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 ();}/*** get the current skin type currently stored in SharedPreferences Value * @ return */public int getCurSkinType () {if (mSharedPreferences! = Null) {return mSharedPreferences. getInt (CURSKINTYPEKEY, 0);} return 0;}/*** get the name of the skin folder corresponding to the current skin resource under the assets folder * @ return */public String getSkinFileName () {String skinFileName = 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 Resource Manager, used to obtain image resource files of different skin types under the assets File

The 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 Resource Manager
 <单例>
  
*/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) {mSkinManager = new SkinManager (context);} return mSkinManager ;} /*** obtain the skin resource Drawable object in Assets based on the skin file name and resource file name * @ 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 ;} /*** obtain the skin resource Bitmap object in Assets based on the skin file name and resource file name * @ 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

Purpose: Skin-changing Activity abstract class. If an Activiy in an application needs skin-changing, inherit SkinableActivity!

It implements the OnSharedPreferenceChangeListener listener interface and registers the event listening for SharedPreferences content changes.

The changeSkin () abstraction method is called back when the SharedPreferences change. This method is used by the SkinableActivity subclass to change Skin Resources on the UI.

The Code is as follows:

Package com. ice. skininnerdemo; import android. app. activity; import android. content. sharedPreferences;/*** skin 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 the listener and listen to the notification SkinConfigManager. getInstance (this ). getSkinConfigPreferences (). when the sharedPreferences content changes, * @ param sharedPreferences * @ param key sharedPreferences key value */@ Override public void equals (SharedPreferences sharedPreferences, string key) {if (SkinConfigManager. CURSKINTYPEKEY. equals (key) {changeSkin () ;}@ Override protected void onStop () {super. onStop (); SkinConfigManager. getInstance (this ). getSkinConfigPreferences (). unregisterOnSharedPreferenceChangeListener (this);}/*** to change the skin settings, the SkinableActivity subclass must implement this method to complete skin replacement process */protected abstract void changeSkin ();}

4. SkinSettingActivity. java

Role: the skin setting Activity inherits SkinableActivity, implements the changeSkin () abstract method, and completes skin replacement on the skin setting UI.

(Other skin-changing activities in the application are similar, and the interface skin-changing needs are mainly implemented in the changeSkin () method)

The 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 setting 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_orange, 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
 
  
> Data = new ArrayList
  
   
> (); Int length = skinTypeImage. length; for (int I = 0; I
   
    
Map = new HashMap
    
     
(); Map. put ("skinTypeImage", skinTypeImage [I]); map. put ("skinTypeName", getString (skinTypeName [I]); data. add (map);} SimpleAdapter simpleAdapter = new SimpleAdapter (this, data, R. layout. traffic_item, new String [] {"skinTypeImage", "skinTypeName"}, new int [] {R. id. iv_traffic, R. id. TV _trafficName}); mGridView. setAdapter (simpleAdapter);} private void bindEvent () {gv_skin_type.setOnItemClickListener (new AdapterView. onItemClickListener () {@ Override public void onItemClick (AdapterView
     AdapterView, View view, int I, long l) {TV _skin_cur.setText (skinTypeName [I]); // Save the sequence number of the selected skin to SkinConfigManager in SharedPreferences. getInstance (SkinSettingActivity. this ). setCurSkinType (I) ;}}) ;}@ Override protected void changeSkin () {SkinConfigManager mSkinConfigManager = SkinConfigManager. getInstance (SkinSettingActivity. this); String skinFileName = mSkinConfigManager. getSkinFileName (); Lo G. d (TAG, "changeSkin () executed/skinFileName:" + skinFileName); // obtain the currently used skin number int skinType = mSkinConfigManager. getCurSkinType (); TV _skin_cur.setText (skinTypeName [skinType]); if (skinFileName! = Null) {Drawable drawable = SkinManager. getInstance (this ). getSkinDrawable (skinFileName, "bg_title.9.png"); TV _title_skin_setting.setBackground (drawable);} else {TV _title_skin_setting.setBackgroundResource (R. drawable. bg_title );}}}
    
   
  
 


-- OK. Here are the main code and design ideas for skin replacement in this demo! Lu actually thought about another method for configuring skin resources,

The general idea is as follows:

1. Create a configuration file for skin resources under the assets file, for example, skin. properties or skin_configure.xml.

2. All skin Resources (images) will be placed under res/drawable/, and any skin resource images corresponding to the skin type will be skin in 1. properties or skin_configure.xml are referenced by configuration.


Of course, this is only a preliminary idea and Design Concept of Mr. Lu. If you see other design ideas for skin replacement, you also want to leave a message, learn, and communicate with Mr. Lu.


Supplement: This demo contains the. 9 Format Image in the assets file.

It is interesting to get the. 9 Format Image under assets,

In this demo, The. 9 format images under assets are compiled and processed by the AAPT command and compiled by the Android system.

So why? You can first study it yourself or search for answers on the Internet. Then you will have time to organize your blog.


The code for this demo is attached. <免下载积分> : Http://download.csdn.net/detail/l416112167/8027581

-----------------

In the next article, Mr. Lu will learn and introduce the second implementation method: [separation of Skin Resources and applications]




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.