生活随笔
收集整理的這篇文章主要介紹了
Apad Qzone项目总结(二)---换肤功能实现!!!
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Hi,大家好,快元旦啦,提前祝大家元旦快樂,(*^__^*) 嘻嘻,今天給大家分享的是Apad Qzone換膚功能的實現(xiàn),我們首先看下效果:
圖1:默認的皮膚.
圖2:點擊菜單護膚按鈕,應用更換皮膚.
通過上面的效果圖可以看出Apad Qzone的換膚功能其實是很簡單實現(xiàn)的,由于整個應用采取了單Activity實現(xiàn)方式,更換背景其實就是實現(xiàn)了更換主程序的Activity的背景。
這里我們事先把幾套皮膚放在res/drawable目錄里,然后用SharedPreferences來記錄當前皮膚的資源id.然后在程序啟動時加載Activity背景。
為了讓大家更容易理解,我這里簡單做了一個Demo,步驟分別如下:
第一步:新建一個Android工程命名為SkinDemo.程序結(jié)構(gòu)如下:
第二步:新建一個皮膚管理類SkinSettingManager.java,代碼如下:
view plaincopy to clipboardprint?
package?com.tutor.skindemo;??????import?android.app.Activity;??import?android.content.SharedPreferences;??????????public?class?SkinSettingManager?{??????????public?final?static?String?SKIN_PREF?=?"skinSetting";????????????public?SharedPreferences?skinSettingPreference;????????????private?int[]?skinResources?=?{?R.drawable.default_wallpaper,??????????????R.drawable.wallpaper_c,R.drawable.wallpaper_d,R.drawable.wallpaper_f,??????????????R.drawable.wallpaper_g??????};????????????private?Activity?mActivity;??????????????????public?SkinSettingManager(Activity?activity)?{??????????this.mActivity?=?activity;????????????skinSettingPreference?=?mActivity.getSharedPreferences(SKIN_PREF,?3);??????}??????????????????????public?int?getSkinType()?{??????????String?key?=?"skin_type";??????????return?skinSettingPreference.getInt(key,?0);??????}??????????????????public?void?setSkinType(int?j)?{??????????SharedPreferences.Editor?editor?=?skinSettingPreference.edit();??????????String?key??=?"skin_type";????????????????????editor.putInt(key,?j);??????????editor.commit();??????}??????????????????????public?int?getCurrentSkinRes()?{??????????int?skinLen?=?skinResources.length;??????????int?getSkinLen?=?getSkinType();??????????if(getSkinLen?>=?skinLen){??????????????getSkinLen?=?0;??????????}????????????????????return?skinResources[getSkinLen];??????}????????????????????public?void?toggleSkins(){????????????????????int?skinType?=?getSkinType();??????????if(skinType?==?skinResources.length?-?1){??????????????skinType?=?0;??????????}else{????????????????????????skinType?++;??????????}??????????setSkinType(skinType);??????????mActivity.getWindow().setBackgroundDrawable(null);??????????try?{??????????????mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());??????????}?catch?(Throwable?e)?{??????????????e.printStackTrace();????????????}??????????????????????????}????????????????????????public?void?initSkins(){??????????????mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());??????}????}?? package com.tutor.skindemo;import android.app.Activity;
import android.content.SharedPreferences;/*** PadQzone皮膚管理器* @author frankiewei**/
public class SkinSettingManager {public final static String SKIN_PREF = "skinSetting";public SharedPreferences skinSettingPreference;private int[] skinResources = { R.drawable.default_wallpaper,R.drawable.wallpaper_c,R.drawable.wallpaper_d,R.drawable.wallpaper_f,R.drawable.wallpaper_g};private Activity mActivity;public SkinSettingManager(Activity activity) {this.mActivity = activity; skinSettingPreference = mActivity.getSharedPreferences(SKIN_PREF, 3);}/*** 獲取當前程序的皮膚序號* * @return*/public int getSkinType() {String key = "skin_type";return skinSettingPreference.getInt(key, 0);}/*** 把皮膚序號寫到全局設置里去* * @param j*/public void setSkinType(int j) {SharedPreferences.Editor editor = skinSettingPreference.edit();String key = "skin_type";editor.putInt(key, j);editor.commit();}/*** 獲取當前皮膚的背景圖資源id* * @return*/public int getCurrentSkinRes() {int skinLen = skinResources.length;int getSkinLen = getSkinType();if(getSkinLen >= skinLen){getSkinLen = 0;}return skinResources[getSkinLen];}/*** 用于導航欄皮膚按鈕切換皮膚*/public void toggleSkins(){int skinType = getSkinType();if(skinType == skinResources.length - 1){skinType = 0;}else{ skinType ++;}setSkinType(skinType);mActivity.getWindow().setBackgroundDrawable(null);try {mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());} catch (Throwable e) {e.printStackTrace();}}/*** 用于初始化皮膚*/public void initSkins(){ mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());}}
第三步:在應用的主Activity--即SkinDemoActivity.java調(diào)用,代碼如下:
view plaincopy to clipboardprint?
package?com.tutor.skindemo;?? ?? import?android.app.Activity;?? import?android.os.Bundle;?? import?android.view.MotionEvent;?? ?? public?class?SkinDemoActivity?extends?Activity?{?? ?????? ????private?SkinSettingManager?mSettingManager;?? ?????? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.main);?? ?????????? ????????mSettingManager?=?new?SkinSettingManager(this);?? ????????mSettingManager.initSkins();?? ????}?? ?????? ????@Override?? ????public?boolean?onTouchEvent(MotionEvent?event)?{?? ????????mSettingManager.toggleSkins();?? ????????return?super.onTouchEvent(event);?? ????}?? ?????? }?? package com.tutor.skindemo;import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;public class SkinDemoActivity extends Activity {private SkinSettingManager mSettingManager;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//初始化皮膚mSettingManager = new SkinSettingManager(this);mSettingManager.initSkins();}//這里為了簡單實現(xiàn),實現(xiàn)換膚@Overridepublic boolean onTouchEvent(MotionEvent event) {mSettingManager.toggleSkins();return super.onTouchEvent(event);}}
以上三步就大功告成啦!,哈哈,很容易吧,今天就講到這里,提前祝大家元旦快樂!!!
源代碼點擊進入==>
?
總結(jié)
以上是生活随笔為你收集整理的Apad Qzone项目总结(二)---换肤功能实现!!!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。