Apad Qzone項目總結(二)---換膚功能實現!!!

Hi,大家好,快元旦啦,提前祝大家元旦快樂,(*^__^*) 嘻嘻,今天給大家分享的是Apad Qzone換膚功能的實現,我們首先看下效果:

圖1:默認的皮膚.

圖2:點擊菜單護膚按鈕,應用更換皮膚.

通過上面的效果圖可以看出Apad Qzone的換膚功能其實是很簡單實現的,由於整個應用採取了單Activity實現方式,更換背景其實就是實現了更換主程序的Activity的背景。

這裏我們事先把幾套皮膚放在res/drawable目錄裏,然後用SharedPreferences來記錄當前皮膚的資源id.然後在程序啓動時加載Activity背景。

爲了讓大家更容易理解,我這裏簡單做了一個Demo,步驟分別如下:

第一步:新建一個Android工程命名爲SkinDemo.程序結構如下:

第二步:新建一個皮膚管理類SkinSettingManager.java,代碼如下:

  1. package com.tutor.skindemo;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.content.SharedPreferences;  
  6.   
  7. /** 
  8.  * PadQzone皮膚管理器 
  9.  * @author frankiewei 
  10.  * 
  11.  */  
  12. public class SkinSettingManager {  
  13.   
  14.   
  15.     public final static String SKIN_PREF = "skinSetting";  
  16.       
  17.     public SharedPreferences skinSettingPreference;  
  18.       
  19.     private int[] skinResources = { R.drawable.default_wallpaper,  
  20.             R.drawable.wallpaper_c,R.drawable.wallpaper_d,R.drawable.wallpaper_f,  
  21.             R.drawable.wallpaper_g  
  22.     };  
  23.       
  24.     private Activity mActivity;  
  25.       
  26.       
  27.     public SkinSettingManager(Activity activity) {  
  28.         this.mActivity = activity;    
  29.         skinSettingPreference = mActivity.getSharedPreferences(SKIN_PREF, 3);  
  30.     }  
  31.       
  32.     /** 
  33.      * 獲取當前程序的皮膚序號 
  34.      *  
  35.      * @return 
  36.      */  
  37.     public int getSkinType() {  
  38.         String key = "skin_type";  
  39.         return skinSettingPreference.getInt(key, 0);  
  40.     }  
  41.   
  42.     /** 
  43.      * 把皮膚序號寫到全局設置裏去 
  44.      *  
  45.      * @param j 
  46.      */  
  47.     public void setSkinType(int j) {  
  48.         SharedPreferences.Editor editor = skinSettingPreference.edit();  
  49.         String key  = "skin_type";  
  50.           
  51.         editor.putInt(key, j);  
  52.         editor.commit();  
  53.     }  
  54.       
  55.     /** 
  56.      * 獲取當前皮膚的背景圖資源id 
  57.      *  
  58.      * @return 
  59.      */  
  60.     public int getCurrentSkinRes() {  
  61.         int skinLen = skinResources.length;  
  62.         int getSkinLen = getSkinType();  
  63.         if(getSkinLen >= skinLen){  
  64.             getSkinLen = 0;  
  65.         }  
  66.           
  67.         return skinResources[getSkinLen];  
  68.     }  
  69.       
  70.     /** 
  71.      * 用於導航欄皮膚按鈕切換皮膚 
  72.      */  
  73.     public void toggleSkins(){  
  74.           
  75.         int skinType = getSkinType();  
  76.         if(skinType == skinResources.length - 1){  
  77.             skinType = 0;  
  78.         }else{            
  79.             skinType ++;  
  80.         }  
  81.         setSkinType(skinType);  
  82.         mActivity.getWindow().setBackgroundDrawable(null);  
  83.         try {  
  84.             mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());  
  85.         } catch (Throwable e) {  
  86.             e.printStackTrace();  
  87.   
  88.         }  
  89.           
  90.           
  91.     }  
  92.           
  93.     /** 
  94.      * 用於初始化皮膚 
  95.      */  
  96.     public void initSkins(){      
  97.         mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());  
  98.     }  
  99.   
  100. }  
第三步:在應用的主Activity--即SkinDemoActivity.java調用,代碼如下:

  1. package com.tutor.skindemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.MotionEvent;  
  6.   
  7. public class SkinDemoActivity extends Activity {  
  8.       
  9.     private SkinSettingManager mSettingManager;  
  10.       
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.         //初始化皮膚   
  16.         mSettingManager = new SkinSettingManager(this);  
  17.         mSettingManager.initSkins();  
  18.     }  
  19.     //這裏爲了簡單實現,實現換膚   
  20.     @Override  
  21.     public boolean onTouchEvent(MotionEvent event) {  
  22.         mSettingManager.toggleSkins();  
  23.         return super.onTouchEvent(event);  
  24.     }  
  25.       
  26. }  

以上三步就大功告成啦!,哈哈,很容易吧,今天就講到這裏,提前祝大家元旦快樂!!!

源代碼點擊進入==>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章