今天看到有同事在群里聊到仿QQQ 皮肤切换主题,然后我就试着写一个简单的Demo希望大家能够支持下!
像以前扣扣换肤
废话不多说上效果
思路如下
通过应用程序内置资源实现换肤,典型的应用为QQ空间中换肤的实现. 应用场景为: 应用一般不大,且页面较少,风格相对简单,一般只用实现部分资源或者只用实现背景的更换.
此种换肤方式实现的思路:
1. 把几套皮肤放在res/drawable目录里,然后用SharedPreferences来记录当前皮肤的资源id.然后在程序启动时加载Activity背景。
2. 主要的实现在皮肤管理器SkinSettingManager类中. 将皮肤资源的ID加入集合中. 由该类同一调度皮肤更换,如初始化皮肤,获取当前皮肤符号以及具体的对应资源的更换皮肤.
SkinSettingManager
package com.zm.util;
import com.zm.recordtest.R;
import android.app.Activity;
import android.content.SharedPreferences;
public class SkinSettingManager {
public final static String SKIN_PREF="skinSetting";
private SharedPreferences sp;
private int[] skinResources={
R.drawable.gril,R.drawable.grl2,R.drawable.grl3,R.drawable.grl4,
R.drawable.grl5
};
private Activity mActivity;
public SkinSettingManager(Activity activity){
this.mActivity=activity;
sp=mActivity.getSharedPreferences(SKIN_PREF, 3);
}
public int SkinType(){
String key="skin_type";
return sp.getInt(key, 0);
}
public void setSkinType(int j){
SharedPreferences.Editor editor=sp.edit();
String key="skin_type";
editor.putInt(key, j);
editor.commit();
}
/**
* 获取当前皮肤背景图片资源
*
*/
public int getCurrentSkinRes(){
int skinLen=skinResources.length;
int getSkinlen=SkinType();
//如果刚好等于从第一张开始
if(getSkinlen>=skinLen){
getSkinlen=0;
}
return skinResources[getSkinlen];
}
public void toggleSkins(){
int skinType=SkinType();
if(skinType==skinResources.length-1){
skinType=0;
}else{
skinType++;
}
setSkinType(skinType);
mActivity.getWindow().setBackgroundDrawable(null);
try {
mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 用于皮肤的初始化
*/
public void initSkins(){
mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());
}
/**
* 切换换肤
*/
public void changeSkin(int id){
setSkinType(id);
mActivity.getWindow().setBackgroundDrawable(null);
try {
mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
然后主SkinActivty
package com.zm.recordtest;
import com.zm.util.LogUtil;
import com.zm.util.SkinSettingManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class SkinActivity extends Activity {
private RadioGroup rg;
private RadioButton rd1,rd2,rd3,rd4,rd5;
private SkinSettingManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
//隐藏标题
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_skin);
manager=new SkinSettingManager(this);
//初始化主题
manager.initSkins();
initView();
}
private void initView() {
//初始化
rg=(RadioGroup) findViewById(R.id.skin_options);
rd1=(RadioButton) findViewById(R.id.rd1);
rd2=(RadioButton) findViewById(R.id.rd2);
rd3=(RadioButton) findViewById(R.id.rd3);
rd4=(RadioButton) findViewById(R.id.rd4);
rd5=(RadioButton) findViewById(R.id.rd5);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//设置点击事件
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 点击raidobutton切换不同主题皮肤
switch (checkedId) {
case R.id.rd1:
manager.changeSkin(1);
//LogUtil.i("换皮肤", "切换第"+checkedId+"皮肤");
break;
case R.id.rd2:
manager.changeSkin(2);
//LogUtil.i("换皮肤", "切换第"+checkedId+"皮肤");
break;
case R.id.rd3:
manager.changeSkin(3);
//LogUtil.i("换皮肤", "切换第"+checkedId+"皮肤");
break;
case R.id.rd4:
manager.changeSkin(4);
//LogUtil.i("换皮肤", "切换第"+checkedId+"皮肤");
break;
case R.id.rd5:
manager.changeSkin(5);
//LogUtil.i("换皮肤", "切换第"+checkedId+"皮肤");
break;
default:
break;
}
}
});
}
// 实现换肤
public boolean onTouchEvent(MotionEvent event) {
manager.toggleSkins();
return super.onTouchEvent(event);
}
}