转自http://blog.csdn.net/android_tutor/article/details/7166186
Hi,大家好,快元旦啦,提前祝大家元旦快乐,(*^__^*) 嘻嘻,今天给大家分享的是Apad Qzone换肤功能的实现,我们首先看下效果:
![Apad Qzone项目总结(二)---换肤功能实现!!!_第1张图片](http://img.e-com-net.com/image/info5/67db07e658544382b829fc0f52a11190.jpg)
图1:默认的皮肤.
![Apad Qzone项目总结(二)---换肤功能实现!!!_第2张图片](http://img.e-com-net.com/image/info5/bbfd113a83814f769ffba8e3a33f81bb.jpg)
图2:点击菜单护肤按钮,应用更换皮肤.
通过上面的效果图可以看出Apad Qzone的换肤功能其实是很简单实现的,由于整个应用采取了单Activity实现方式,更换背景其实就是实现了更换主程序的Activity的背景。
这里我们事先把几套皮肤放在res/drawable目录里,然后用SharedPreferences来记录当前皮肤的资源id.然后在程序启动时加载Activity背景。
为了让大家更容易理解,我这里简单做了一个Demo,步骤分别如下:
第一步:新建一个Android工程命名为SkinDemo.程序结构如下:
![Apad Qzone项目总结(二)---换肤功能实现!!!_第3张图片](http://img.e-com-net.com/image/info5/2df38624a8f8416c895888e4143770a2.jpg)
第二步:新建一个皮肤管理类SkinSettingManager.java,代码如下:
- 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());
- }
-
- }
第三步:在应用的主Activity--即SkinDemoActivity.java调用,代码如下:
- 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);
- }
-
- }
以上三步就大功告成啦!,哈哈,很容易吧,今天就讲到这里,提前祝大家元旦快乐!!!
源代码点击进入==>