日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Apad Qzone项目总结(二)---换肤功能实现!!!

發布時間:2023/12/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Apad Qzone项目总结(二)---换肤功能实现!!! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Hi,大家好,快元旦啦,提前祝大家元旦快樂,(*^__^*) 嘻嘻,今天給大家分享的是Apad Qzone換膚功能的實現,我們首先看下效果:

圖1:默認的皮膚.

圖2:點擊菜單護膚按鈕,應用更換皮膚.

通過上面的效果圖可以看出Apad Qzone的換膚功能其實是很簡單實現的,由于整個應用采取了單Activity實現方式,更換背景其實就是實現了更換主程序的Activity的背景。

這里我們事先把幾套皮膚放在res/drawable目錄里,然后用SharedPreferences來記錄當前皮膚的資源id.然后在程序啟動時加載Activity背景。

為了讓大家更容易理解,我這里簡單做了一個Demo,步驟分別如下:

第一步:新建一個Android工程命名為SkinDemo.程序結構如下:

第二步:新建一個皮膚管理類SkinSettingManager.java,代碼如下:

view plaincopy to clipboardprint?
  • 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());??
  • ????}??
  • ??
  • }??
  • 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調用,代碼如下:

    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();}//這里為了簡單實現,實現換膚@Overridepublic boolean onTouchEvent(MotionEvent event) {mSettingManager.toggleSkins();return super.onTouchEvent(event);}}
    以上三步就大功告成啦!,哈哈,很容易吧,今天就講到這里,提前祝大家元旦快樂!!!

    源代碼點擊進入==>

    ?

    總結

    以上是生活随笔為你收集整理的Apad Qzone项目总结(二)---换肤功能实现!!!的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。