以下纯属个人观点仅供参考:
基本思想:1. 把几套皮肤放在res/drawable目录里,然后用SharedPreferences来记录当前皮肤的资源id.然后在程序启动时加载Activity背景。 2. 主要的实现在皮肤管理器SkinManager类中. 将皮肤资源的ID加入集合中. 由该类同一调度皮肤更换,如初始化皮肤,获取当前皮肤符号以及具体的对应资源的更换皮肤. 3.要实现应用的皮肤更换只需要定义个基类BaseActivity继承Activity。然后其他的Activity继承Baseactivity就行了。主要:Activity·布局文件中不能自己设置背景
主要的是皮肤的管理类:
package com.mbm.manager;
import android.app.Activity;
import android.content.SharedPreferences;
import com.example.mybbmarket.R;
//换皮肤管理工具类
public class SkinSettingManager {
public final static String SKIN_PREF = "skinSetting";
public SharedPreferences skinSettingPreference;
private int[] skinResources = { R.drawable.bg, R.drawable.skin_peeler1_2,
R.drawable.skin_peeler1_3, R.drawable.skin_peeler1_4,
R.drawable.skin_peeler1_5, R.drawable.skin_peeler1_6 };
private Activity mActivity;
private int position = 0;
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() {
setSkinType(position);
mActivity.getWindow().setBackgroundDrawable(null);
try {
mActivity.getWindow().setBackgroundDrawableResource(
getCurrentSkinRes());
} catch (Throwable e) {
e.printStackTrace();
}
}
public void initSkins() {
mActivity.getWindow()
.setBackgroundDrawableResource(getCurrentSkinRes());
}
public void setPostion(int i) {
// TODO Auto-generated method stub
position = i;
}
}
在Baseactivity中的使用:
这是点击图片更换为图片为皮肤
package com.mbm.activity;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.mybbmarket.R;
import com.mbm.manager.SkinSettingManager;
//关于换肤模块
public class HuanfuActivity extends FragmentActivity {
private LinearLayout hf1, hf2, hf3, hf4, hf5, hf6;
ImageView use1, use2, use3, use4, use5, use6;
private LinearLayout huan1;
Intent intent;
public static final String SETTING_INFOS = "SETTINGInfos";
public static final String FLAG = "FLAG";
SharedPreferences settings;
private SkinSettingManager mSettingManager, b;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.huanfu);
// 初始化皮肤
mSettingManager = new SkinSettingManager(this);
mSettingManager.initSkins();
// 初始化变量
initData();
// 单击事件
onClickListener();
}
private void onClickListener() {
// TODO Auto-generated method stub
hf1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
use1.setVisibility(View.VISIBLE);
use2.setVisibility(View.GONE);
use3.setVisibility(View.GONE);
use4.setVisibility(View.GONE);
use5.setVisibility(View.GONE);
use6.setVisibility(View.GONE);
mSettingManager.setPostion(0);
mSettingManager.toggleSkins();
}
});
hf2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
use2.setVisibility(View.VISIBLE);
use1.setVisibility(View.GONE);
use3.setVisibility(View.GONE);
use4.setVisibility(View.GONE);
use5.setVisibility(View.GONE);
use6.setVisibility(View.GONE);
mSettingManager.setPostion(1);
mSettingManager.toggleSkins();
}
});
hf3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
use3.setVisibility(View.VISIBLE);
use2.setVisibility(View.GONE);
use1.setVisibility(View.GONE);
use4.setVisibility(View.GONE);
use5.setVisibility(View.GONE);
use6.setVisibility(View.GONE);
mSettingManager.setPostion(2);
mSettingManager.toggleSkins();
}
});
hf4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
use4.setVisibility(View.VISIBLE);
use2.setVisibility(View.GONE);
use3.setVisibility(View.GONE);
use1.setVisibility(View.GONE);
use5.setVisibility(View.GONE);
use6.setVisibility(View.GONE);
mSettingManager.setPostion(3);
mSettingManager.toggleSkins();
}
});
hf5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
use5.setVisibility(View.VISIBLE);
use2.setVisibility(View.GONE);
use3.setVisibility(View.GONE);
use1.setVisibility(View.GONE);
use4.setVisibility(View.GONE);
use6.setVisibility(View.GONE);
mSettingManager.setPostion(4);
mSettingManager.toggleSkins();
}
});
hf6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
use6.setVisibility(View.VISIBLE);
use2.setVisibility(View.GONE);
use3.setVisibility(View.GONE);
use1.setVisibility(View.GONE);
use5.setVisibility(View.GONE);
use4.setVisibility(View.GONE);
mSettingManager.setPostion(5);
mSettingManager.toggleSkins();
}
});
}
private void initData() {
// TODO Auto-generated method stub
hf1 = (LinearLayout) findViewById(R.id.hf1);
hf2 = (LinearLayout) findViewById(R.id.hf2);
hf3 = (LinearLayout) findViewById(R.id.hf3);
hf4 = (LinearLayout) findViewById(R.id.hf4);
hf5 = (LinearLayout) findViewById(R.id.hf5);
hf6 = (LinearLayout) findViewById(R.id.hf6);
use1 = (ImageView) findViewById(R.id.use1);
use2 = (ImageView) findViewById(R.id.use2);
use3 = (ImageView) findViewById(R.id.use3);
use4 = (ImageView) findViewById(R.id.use4);
use5 = (ImageView) findViewById(R.id.use5);
use6 = (ImageView) findViewById(R.id.use6);
huan1 = (LinearLayout) findViewById(R.id.huan1);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // 按下的如果是BACK,同时没有重复
Intent intent = new Intent(this, LieBiaoActivity.class);
intent.putExtra("flag", "flag");
startActivity(intent);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}