zoukankan      html  css  js  c++  java
  • Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能

    通过应用程序内置资源实现换肤,典型的应用为QQ空间中换肤的实现. 应用场景为: 应用一般不大,且页面较少,风格相对简单,一般只用实现部分资源或者只用实现背景的更换.

       此种换肤方式实现的思路:

      1. 把几套皮肤放在res/drawable目录里,然后用SharedPreferences来记录当前皮肤的资源id.然后在程序启动时加载Activity背景。

      2. 主要的实现在皮肤管理器SkinManager类中. 将皮肤资源的ID加入集合中. 由该类同一调度皮肤更换,如初始化皮肤,获取当前皮肤符号以及具体的对应资源的更换皮肤.

        接下来看一下效果图: 

        

    内置皮肤的实现相对比较简单,下面直接上代码:

    AndroidMainfest.xml

    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.tony.skindemo"  
    4.     android:versionCode="1"  
    5.     android:versionName="1.0" >  
    6.   
    7.     <uses-sdk android:minSdkVersion="8" />  
    8.   
    9.     <application  
    10.         android:icon="@drawable/ic_launcher"  
    11.         android:label="@string/app_name" >  
    12.         <activity  
    13.             android:label="@string/app_name"  
    14.             android:name="com.tony.skindemo.SkinDemoActivity" >  
    15.             <intent-filter >  
    16.                 <action android:name="android.intent.action.MAIN" />  
    17.   
    18.                 <category android:name="android.intent.category.LAUNCHER" />  
    19.             </intent-filter>  
    20.         </activity>  
    21.           
    22.           
    23.     </application>  
    24.   
    25. </manifest>  


    布局文件:

    main.xml

    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <TextView  
    8.         android:textColor="#ff00ff"  
    9.         android:text="程序皮肤更换"  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="wrap_content" />  
    12. <RadioGroup  
    13.         android:id="@+id/skin_options"  
    14.         android:layout_width="fill_parent"  
    15.         android:layout_height="wrap_content"  
    16.          >  
    17.   
    18.         <RadioButton  
    19.             android:layout_weight="1"  
    20.             android:id="@+id/radioButton1"  
    21.             android:layout_width="wrap_content"  
    22.             android:layout_height="wrap_content"  
    23.             android:text="风格1" />  
    24.   
    25.         <RadioButton  
    26.             android:layout_weight="1"  
    27.             android:id="@+id/radioButton2"  
    28.             android:layout_width="wrap_content"  
    29.             android:layout_height="wrap_content"  
    30.             android:text="风格2" />  
    31.   
    32.         <RadioButton  
    33.             android:layout_weight="1"  
    34.             android:id="@+id/radioButton3"  
    35.             android:layout_width="wrap_content"  
    36.             android:layout_height="wrap_content"  
    37.             android:text="风格3" />  
    38.   
    39.         <RadioButton  
    40.             android:layout_weight="1"  
    41.             android:id="@+id/radioButton4"  
    42.             android:layout_width="wrap_content"  
    43.             android:layout_height="wrap_content"  
    44.             android:text="风格4" />  
    45.   
    46.         <RadioButton  
    47.             android:layout_weight="1"  
    48.             android:id="@+id/radioButton5"  
    49.             android:layout_width="wrap_content"  
    50.             android:layout_height="wrap_content"  
    51.             android:text="风格5" />  
    52.     </RadioGroup>  
    53.   
    54. </LinearLayout>  


    程序主Activity

    [java] view plaincopy
     
    1. package com.tony.skindemo;  
    2.   
    3.   
    4. import android.app.Activity;  
    5. import android.os.Bundle;  
    6. import android.view.MotionEvent;  
    7. import android.view.Window;  
    8. import android.view.WindowManager;  
    9. import android.widget.RadioButton;  
    10. import android.widget.RadioGroup;  
    11. import android.widget.RadioGroup.OnCheckedChangeListener;  
    12.   
    13. public class SkinDemoActivity extends Activity {  
    14.   
    15.     private SkinSettingManager mSettingManager;  
    16.     private RadioButton radioButton1;  
    17.     private RadioButton radioButton2;  
    18.     private RadioButton radioButton3;  
    19.     private RadioButton radioButton4;  
    20.     private RadioButton radioButton5;  
    21.   
    22.     @Override  
    23.     public void onCreate(Bundle savedInstanceState) {  
    24.         super.onCreate(savedInstanceState);  
    25.   
    26.         // 取消标题栏  
    27.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
    28.         // 完成窗体的全屏显示 // 取消掉状态栏  
    29.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
    30.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    31.   
    32.         setContentView(R.layout.main);  
    33.         // 初始化皮肤  
    34.         mSettingManager = new SkinSettingManager(this);  
    35.         mSettingManager.initSkins();  
    36.   
    37.         //通过单选按钮设置皮肤(可自定义更换的方式,如导航栏,也可以加上预览功能,此处不再实现)  
    38.         radioButton1 = (RadioButton) findViewById(R.id.radioButton1);  
    39.         radioButton2 = (RadioButton) findViewById(R.id.radioButton2);  
    40.         radioButton3 = (RadioButton) findViewById(R.id.radioButton3);  
    41.         radioButton4 = (RadioButton) findViewById(R.id.radioButton4);  
    42.         radioButton5 = (RadioButton) findViewById(R.id.radioButton5);  
    43.         RadioGroup radioGroup = (RadioGroup) findViewById(R.id.skin_options);  
    44.         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
    45.   
    46.             @Override  
    47.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
    48.   
    49.                 switch (checkedId) {  
    50.                 case R.id.radioButton1:  
    51.                     mSettingManager.changeSkin(1);  
    52.                     break;  
    53.   
    54.                 case R.id.radioButton2:  
    55.                     mSettingManager.changeSkin(2);  
    56.                     break;  
    57.                 case R.id.radioButton3:  
    58.                     mSettingManager.changeSkin(3);  
    59.                     break;  
    60.                 case R.id.radioButton4:  
    61.                     mSettingManager.changeSkin(4);  
    62.                     break;  
    63.                 case R.id.radioButton5:  
    64.                     mSettingManager.changeSkin(5);  
    65.                     break;  
    66.                 default:  
    67.                     break;  
    68.                 }  
    69.             }  
    70.         });  
    71.     }  
    72.   
    73.     // 这里为了简单实现,实现换肤  
    74.     public boolean onTouchEvent(MotionEvent event) {  
    75.         mSettingManager.toggleSkins();  
    76.         return super.onTouchEvent(event);  
    77.     }  
    78.   
    79.   
    80. }  



    [java] view plaincopy
     
      1. <pre name="code" class="java"></pre><pre></pre><p></p><pre></pre>皮肤管理器:<p></p><p></p><pre name="code" class="java">package com.tony.skindemo;  
      2.   
      3.   
      4.   
      5.   
      6. import android.app.Activity;  
      7. import android.content.SharedPreferences;  
      8.   
      9. /** 
      10.  * 皮肤管理器 
      11.  * @author tony 
      12.  * 
      13.  */  
      14. public class SkinSettingManager {  
      15.   
      16.   
      17.     public final static String SKIN_PREF = "skinSetting";  
      18.       
      19.     public SharedPreferences skinSettingPreference;  
      20.       
      21.     private int[] skinResources = { R.drawable.default_wallpaper,  
      22.             R.drawable.wallpaper_c,R.drawable.wallpaper_d,R.drawable.wallpaper_f,  
      23.             R.drawable.wallpaper_g  
      24.     };  
      25.       
      26.     private Activity mActivity;  
      27.       
      28.       
      29.     public SkinSettingManager(Activity activity) {  
      30.         this.mActivity = activity;    
      31.         skinSettingPreference = mActivity.getSharedPreferences(SKIN_PREF, 3);  
      32.     }  
      33.       
      34.     /** 
      35.      * 获取当前程序的皮肤序号 
      36.      *  
      37.      * @return 
      38.      */  
      39.     public int getSkinType() {  
      40.         String key = "skin_type";  
      41.         return skinSettingPreference.getInt(key, 0);  
      42.     }  
      43.   
      44.     /** 
      45.      * 把皮肤序号写到全局设置里去 
      46.      *  
      47.      * @param j 
      48.      */  
      49.     public void setSkinType(int j) {  
      50.         SharedPreferences.Editor editor = skinSettingPreference.edit();  
      51.         String key  = "skin_type";  
      52.           
      53.         editor.putInt(key, j);  
      54.         editor.commit();  
      55.     }  
      56.       
      57.     /** 
      58.      * 获取当前皮肤的背景图资源id 
      59.      *  
      60.      * @return 
      61.      */  
      62.     public int getCurrentSkinRes() {  
      63.         int skinLen = skinResources.length;  
      64.         int getSkinLen = getSkinType();  
      65.         if(getSkinLen >= skinLen){  
      66.             getSkinLen = 0;  
      67.         }  
      68.           
      69.         return skinResources[getSkinLen];  
      70.     }  
      71.       
      72.     public void toggleSkins(){  
      73.           
      74.         int skinType = getSkinType();  
      75.         if(skinType == skinResources.length - 1){  
      76.             skinType = 0;  
      77.         }else{            
      78.             skinType ++;  
      79.         }  
      80.         setSkinType(skinType);  
      81.         mActivity.getWindow().setBackgroundDrawable(null);  
      82.         try {  
      83.             mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());  
      84.         } catch (Throwable e) {  
      85.             e.printStackTrace();  
      86.   
      87.         }  
      88.           
      89.           
      90.     }  
      91.           
      92.     /** 
      93.      * 用于初始化皮肤 
      94.      */  
      95.     public void initSkins(){      
      96.         mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());  
      97.     }  
      98.   
      99.     /** 
      100.      * 随即切换一个背景皮肤 
      101.      */  
      102.     public void changeSkin(int id) {  
      103.           
      104.         setSkinType(id);  
      105.         mActivity.getWindow().setBackgroundDrawable(null);  
      106.         try {  
      107.             mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());  
      108.         } catch (Throwable e) {  
      109.             e.printStackTrace();  
      110.         }  
      111.     }  
      112.   
      113. }  
      114. </pre><br><p></p><p>就这样,通过程序内置皮肤的基本功能完成了.</p><p>若想在自己的应用中实现,仍需注意以下几点(实现起来并不复杂,此处不再写具体实现):<br></p><p>1.  实现多个activity的更换皮肤. 需要利用自定义MyApplication类,继承自Application. 并加入activity的集合属性.用于存储应用所有的activity<br></p><p>    修改SkinManager,在更换皮肤时,从application中取出该集合,进行遍历并更换皮肤</p><p><br></p><p>2. 可以优化用户体验,通过导航栏方式进入更换皮肤界面,并可以加入预览功能,当确定修改配置后,才完成更换皮肤功能.</p><p>3. 加入style.theme等资源,实现更加复杂的皮肤更换. 具体实现同更换背景.<br></p><p><br></p><p><br></p><p><br></p><p><br></p>  
  • 相关阅读:
    Nodejs chrome 调试node-inspector
    Nodejs 文件修改自动重启扩展
    PHP post & get请求
    mysql 忘记root密码
    Linux安装mysql
    Linux 安装reids
    技术 | TypeScript
    秦涛:深度学习的五个挑战和其解决方案
    人脸检测与识别的趋势和分析
    370 门免费编程与计算机科学在线课程
  • 原文地址:https://www.cnblogs.com/dongweiq/p/4249928.html
Copyright © 2011-2022 走看看