模拟QQ更换主题流程,实现更换主题

首先看下效果图:

为了模仿的像一点,这里也用了之前写的一篇实现博客:模仿QQ拖动清除消息提示的效果(这里修复了适配的问题),用了QQ默认主题图标,,和一套需要下载的主题图标(下载主题包后,可以在目录sdcard/Tencent/MobileQQ/theme_pkg/QQThemePkg/pkg下找到),QQ下面的导航栏,是用TabHost(在模拟器上打开QQ,在Eclipse上,点击DDMS->选中模拟器->点击像三个手机图标可以看到整个布局结构),我这里也用的TabHost,实现效果如图。

QQ更换主题的实现,大概是这样的吧:先读取解析主题压缩包下的配置,比如该套主题对应的一些字体的颜色值,对应的特殊主题配置和加载图片资源放到内存中,然后对每一个控件进行设置更换,我这里只做了更换图标。

我的实现流程:

1、将需要的图片资源,放入到sdcard上,图片的名称,必须要跟默认的主题的图片名称一一对应

2、为每个需要更换图片的控件设置一个特殊的tag,需要跟图片名称关联上来,比如如图片上面的第一个tab里的ImageView,原来的图片名称正常情况下为skin_tab_icon_conversation_normal.png,选中后为skin_tab_icon_conversation_selected.png,我j就给它设的tag为skin_tab_icon_conversation_iv2,iv,表示的是ImageView,2表示有两种状态,如果是1,那只有一种正常状态,如果是继承ViewGround的布局,后面跟的是_vg1,这种布局大概就是设置下背景图片。

3、然后在代码里面,递归找出view中设有的tag,和对应的view id 保存起来,代码实现如下:

private void findViewTag(ViewGroup rootViewGrop) {for (int i = 0; i < rootViewGrop.getChildCount(); i++) {View temp = rootViewGrop.getChildAt(i);if (temp instanceof ViewGroup) {ViewGroup tempViewGrop = (ViewGroup) temp;if (temp.getTag() != null) {String tag = (String) temp.getTag();if (tag.contains("vg1")) {ViewBean bean = new ViewBean();String temp_tag = tag.substring(0, tag.lastIndexOf("_"));bean.viewId = temp.getId();bean.background = temp_tag + ".png";bean.type = Constants.VIEWGROUND;tagList.add(bean);}}findViewTag(tempViewGrop);// 递归查找} else if (temp.getTag() != null) {String tag = (String) temp.getTag();if (tag.contains("_iv1")) {String temp_tag = tag.substring(0, tag.lastIndexOf("_"));ViewBean bean = new ViewBean();bean.viewId = temp.getId();bean.type = Constants.IMAGEVIEW1;bean.normal = temp_tag + ".png";tagList.add(bean);} else if (tag.contains("_iv2")) {String temp_tag = tag.substring(0, tag.lastIndexOf("_"));ViewBean bean = new ViewBean();bean.viewId = temp.getId();bean.type = Constants.IMAGEVIEW2;bean.normal = temp_tag + "_normal.png";bean.selected = temp_tag + "_selected.png";tagList.add(bean);}}}}4、然后根据不同的状态,选择是默认的主题,还是下载到sdcard上的主题,我这里简单的设置theme等于1的时候,就拿sdcard上的图片,不是就拿默认的主题图片,重sdcard上加载出来后,用map保存起来,这里的一些操作处理或判断,可能不够严谨,比如,加载sdcard的图片,应该可能要放在子线程里面做,我这里就不做了,我写的只是一种思路吧public static Drawable getBitmapDrawable(String fileName, Context context) {if (theme == 1) {return getDrawableFromSDCard(fileName);}return getDrawableFromRes(fileName, context);}private static Drawable getDrawableFromSDCard(String fileName) {Drawable drawable = ThemeResManager.getInstance().getThemeDrawable(fileName);if (drawable == null) {InputStream is = null;try {is = new FileInputStream(Constants.PATH + fileName);drawable = new BitmapDrawable(BitmapFactory.decodeStream(is));ThemeResManager.getInstance().saveThemeDrawable(fileName,drawable);is.close();} catch (IOException e) {e.printStackTrace();}}return drawable;}private static Drawable getDrawableFromRes(String fileName, Context context) {Drawable drawable = ThemeResManager.getInstance().getThemeDrawable(fileName);if (drawable == null) {Resources res = context.getResources();int id = res.getIdentifier(fileName.substring(0,fileName.length()-4), "drawable",context.getPackageName());drawable = new BitmapDrawable(BitmapFactory.decodeResource(res, id));ThemeResManager.getInstance().saveThemeDrawable(fileName,drawable);}return drawable;}5、遍历第三步奏保存的tag 和view id,重新设置图片,首先实例一个StateListDrawable,使得点击ImageView有点击选中的效果,整个流程也就完了private StateListDrawable getStateListDrawable(String normal,String selected) {StateListDrawable state = new StateListDrawable();state.addState(new int[] { android.R.attr.state_selected, },ImageUtils.getBitmapDrawable(selected, this));state.addState(new int[] {}, ImageUtils.getBitmapDrawable(normal, this));return state;}public void initViewBitmap() {for (int i = 0; i < tagList.size(); i++) {ViewBean bean = tagList.get(i);switch (bean.type) {case Constants.VIEWGROUND:findViewById(bean.viewId).setBackgroundDrawable(ImageUtils.getBitmapDrawable(bean.background, this));break;case Constants.IMAGEVIEW1:ImageView iv1 = (ImageView) findViewById(bean.viewId);iv1.setImageDrawable(ImageUtils.getBitmapDrawable(bean.normal, this));break;case Constants.IMAGEVIEW2:ImageView iv2 = (ImageView) findViewById(bean.viewId);iv2.setImageDrawable(getStateListDrawable(bean.normal,bean.selected));break;}}}

尊重作者劳动,转载请说明出处

小demo下载

让我们从自身的禁锢中放心地飞出去,重新审视自己,

模拟QQ更换主题流程,实现更换主题

相关文章:

你感兴趣的文章:

标签云: