Android 打造自己的個性化應用(四):仿墨跡天氣實現-->自定義擴展名的zip格式的皮膚

在這裏談一下墨跡天氣的換膚實現方式,不過首先聲明我只是通過反編譯以及參考了一些網上其他資料的方式推測出的換膚原理, 在這裏只供參考. 若大家有更好的方式, 歡迎交流.

墨跡天氣下載的皮膚就是一個zip格式的壓縮包,在應用的時候把皮膚資源釋放到墨跡天氣應用的目錄下,更換皮膚時新的皮膚資源會替換掉老的皮膚資源每次加載的時候就是從手機硬盤上讀取圖片,這些圖片資源的命名和程序中的資源的命名保持一致,一旦找不到這些資源,可以選擇到系統默認中查找。這種實現是直接讀取了外部資源文件,在程序運行時通過代碼顯示的替換界面的背景資源。這種方式的優點是:皮膚資源的格式定義很隨意可以是zip也可以是自定義的格式,只要程序中能夠解析到資源就行,缺點是效率上的問題.


這裏需要注意的一點是,再這裏對壓縮包的解壓,藉助了第三方工具: ant. jar進行解壓和壓縮文件. 關於ant工具的使用,我在稍後的文章中會具體介紹.


主要技術點:

如何去讀取zip文件中的資源以及皮膚文件存放方式


實現方案:如果軟件每次啓動都去讀取SD卡上的皮膚文件,速度會比較慢。較好的做法是提供一個皮膚設置的界面,用戶選擇了哪一個皮膚,就把那個皮膚文件解壓縮到”/data/data/[package name]/skin”路徑下(讀取的快速及安全性),這樣不需要跨存儲器讀取,速度較快,而且不需要每次都去zip壓縮包中讀取,不依賴SD卡中的文件,即使皮膚壓縮包文件被刪除了也沒有關係。

實現方法:

1. 在軟件的幫助或者官網的幫助中提示用戶將皮膚文件拷貝到SD卡指定路徑下。
2. 在軟件中提供皮膚設置界面。可以在菜單或者在設置中。可參考墨跡、搜狗輸入法、QQ等支持換膚的軟件。
3. 加載指定路徑下的皮膚文件,讀取其中的縮略圖,在皮膚設置界面中顯示,將用戶選中的皮膚文件解壓縮到”/data/data/[package name]/skin”路徑下。
4. 軟件中優先讀取”/data/data/[package name]/skin/”路徑下的資源。如果沒有則使用apk中的資源。


效果圖:



具體代碼:

1. AndroidManifest.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.skin" android:versionCode="1" android:versionName="1.0">  
  4.     <uses-sdk android:minSdkVersion="7" />  
  5.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".Re_Skin2Activity"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.     </application>  
  16. </manifest>  

2.佈局文件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:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#d2d2d2"  
  7.     android:id="@+id/layout">  
  8.     <Button android:text="導入皮膚" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  
  9.     <Button android:text="換膚" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  
  10.     <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"  
  11.          android:text="請先點擊“導入皮膚”,會將/sdcard/skin.zip導入到/sdcard/Skin_kris目錄下,然後點擊‘換膚’會將sdcard裏面的素材用作皮膚"  
  12.          android:textColor="#000"></TextView>  
  13. </LinearLayout>  

3. Re_Skin2Activity:

[java] view plaincopy
  1. package com.tony.skin;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.drawable.BitmapDrawable;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.Toast;  
  13.   
  14. import com.tony.skin.utils.ZipUtil;  
  15.   
  16. /** 
  17.  *  
  18.  * @author Tony 
  19.  * 
  20.  */  
  21. public class Re_Skin2Activity extends Activity implements OnClickListener{  
  22.     private Button  btnSet;  
  23.     private Button  btnImport;  
  24.     private LinearLayout layout;  
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.         btnSet = (Button)findViewById(R.id.button1);  
  31.         btnSet.setOnClickListener(this);  
  32.   
  33.         btnImport = (Button)findViewById(R.id.button2);  
  34.         btnImport.setOnClickListener(this);  
  35.         layout = (LinearLayout)findViewById(R.id.layout);  
  36.     }  
  37.     @Override  
  38.     public void onClick(View v) {  
  39.         switch (v.getId()) {  
  40.         case R.id.button1:  
  41.             Bitmap bitmap= BitmapFactory.decodeFile("/sdcard/Skin_kris/skin/google.png");  
  42.               
  43.              BitmapDrawable bd=new BitmapDrawable(bitmap);  
  44.             btnSet.setBackgroundDrawable(bd);  
  45.               
  46.             layout.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeFile("/sdcard/Skin_kris/skin/bg/bg.png")));  
  47.               
  48.             break;  
  49.         case R.id.button2:  
  50.             ZipUtil zipp = new ZipUtil(2049);  
  51.             System.out.println("begin do zip");  
  52.             zipp.unZip("/sdcard/skin.zip","/sdcard/Skin_kris");  
  53.             Toast.makeText(this"導入成功", Toast.LENGTH_SHORT).show();  
  54.             break;  
  55.         default:  
  56.             break;  
  57.         }  
  58.     }  
  59. }  

4. ZipUtil 解壓縮處理ZIP包的工具類

[java] view plaincopy
  1. package com.tony.skin.utils;  
  2.   
  3. import java.io.BufferedOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.util.Enumeration;  
  10. import java.util.zip.Deflater;  
  11.   
  12. import org.apache.tools.zip.ZipEntry;  
  13. import org.apache.tools.zip.ZipFile;  
  14. import org.apache.tools.zip.ZipOutputStream;  
  15.   
  16. /** 
  17.  * Zip包壓縮,解壓處理工具類 
  18.  * @author a 
  19.  * 
  20.  */  
  21. public class ZipUtil {  
  22.     private ZipFile         zipFile;   
  23.     private ZipOutputStream zipOut;     //壓縮Zip   
  24.     private  int            bufSize;    //size of bytes   
  25.     private byte[]          buf;   
  26.     private int             readedBytes;   
  27.     public ZipUtil(){   
  28.         this(512);   
  29.     }   
  30.   
  31.     public ZipUtil(int bufSize){   
  32.         this.bufSize = bufSize;   
  33.         this.buf = new byte[this.bufSize];   
  34.     }   
  35.        
  36.     /** 
  37.      *  
  38.      * @param srcFile  需要 壓縮的目錄或者文件 
  39.      * @param destFile 壓縮文件的路徑 
  40.      */  
  41.     public void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要壓縮的文件夾名  
  42.         File zipDir;  
  43.         String dirName;  
  44.   
  45.         zipDir = new File(srcFile);  
  46.         dirName = zipDir.getName();  
  47.         try {  
  48.             this.zipOut = new ZipOutputStream(new BufferedOutputStream(  
  49.                     new FileOutputStream(destFile)));  
  50.             //設置壓縮的註釋  
  51.             zipOut.setComment("comment");  
  52.             //設置壓縮的編碼,如果要壓縮的路徑中有中文,就用下面的編碼  
  53.             zipOut.setEncoding("GBK");  
  54.             //啓用壓縮   
  55.             zipOut.setMethod(ZipOutputStream.DEFLATED);   
  56.   
  57.             //壓縮級別爲最強壓縮,但時間要花得多一點   
  58.             zipOut.setLevel(Deflater.BEST_COMPRESSION);   
  59.               
  60.             handleDir(zipDir, this.zipOut,dirName);  
  61.             this.zipOut.close();  
  62.         } catch (IOException ioe) {  
  63.             ioe.printStackTrace();  
  64.         }  
  65.     }  
  66.   
  67.     /** 
  68.      *  由doZip調用,遞歸完成目錄文件讀取 
  69.      * @param dir 
  70.      * @param zipOut 
  71.      * @param dirName  這個主要是用來記錄壓縮文件的一個目錄層次結構的 
  72.      * @throws IOException 
  73.      */  
  74.     private void handleDir(File dir, ZipOutputStream zipOut,String dirName) throws IOException {  
  75.         System.out.println("遍歷目錄:"+dir.getName());  
  76.         FileInputStream fileIn;  
  77.         File[] files;  
  78.   
  79.         files = dir.listFiles();  
  80.   
  81.         if (files.length == 0) {// 如果目錄爲空,則單獨創建之.  
  82.             // ZipEntry的isDirectory()方法中,目錄以"/"結尾.  
  83.             System.out.println("壓縮的 Name:"+dirName);  
  84.             this.zipOut.putNextEntry(new ZipEntry(dirName));  
  85.             this.zipOut.closeEntry();  
  86.         } else {// 如果目錄不爲空,則分別處理目錄和文件.  
  87.             for (File fileName : files) {  
  88.                 // System.out.println(fileName);  
  89.   
  90.                 if (fileName.isDirectory()) {  
  91.                     handleDir(fileName, this.zipOut,dirName+File.separator+fileName.getName()+File.separator);  
  92.                 } else {  
  93.                     System.out.println("壓縮的 Name:"+dirName + File.separator+fileName.getName());  
  94.                     fileIn = new FileInputStream(fileName);  
  95.                     this.zipOut.putNextEntry(new ZipEntry(dirName + File.separator+fileName.getName()));  
  96.   
  97.                     while ((this.readedBytes = fileIn.read(this.buf)) > 0) {  
  98.                         this.zipOut.write(this.buf, 0this.readedBytes);  
  99.                     }  
  100.   
  101.                     this.zipOut.closeEntry();  
  102.                 }  
  103.             }  
  104.         }  
  105.     }  
  106.   
  107.     /** 
  108.      * 解壓指定zip文件 
  109.      * @param unZipfile 壓縮文件的路徑 
  110.      * @param destFile   解壓到的目錄  
  111.      */  
  112.     public void unZip(String unZipfile, String destFile) {// unZipfileName需要解壓的zip文件名  
  113.         FileOutputStream fileOut;  
  114.         File file;  
  115.         InputStream inputStream;  
  116.   
  117.         try {  
  118.             this.zipFile = new ZipFile(unZipfile);  
  119.   
  120.             for (Enumeration entries = this.zipFile.getEntries(); entries  
  121.                     .hasMoreElements();) {  
  122.                 ZipEntry entry = (ZipEntry) entries.nextElement();  
  123.                 file = new File(destFile+File.separator+entry.getName());  
  124.   
  125.                 if (entry.isDirectory()) {  
  126.                     file.mkdirs();  
  127.                 } else {  
  128.                     // 如果指定文件的目錄不存在,則創建之.  
  129.                     File parent = file.getParentFile();  
  130.                     if (!parent.exists()) {  
  131.                         parent.mkdirs();  
  132.                     }  
  133.   
  134.                     inputStream = zipFile.getInputStream(entry);  
  135.   
  136.                     fileOut = new FileOutputStream(file);  
  137.                     while ((this.readedBytes = inputStream.read(this.buf)) > 0) {  
  138.                         fileOut.write(this.buf, 0this.readedBytes);  
  139.                     }  
  140.                     fileOut.close();  
  141.   
  142.                     inputStream.close();  
  143.                 }  
  144.             }  
  145.             this.zipFile.close();  
  146.         } catch (IOException ioe) {  
  147.             ioe.printStackTrace();  
  148.         }  
  149.     }  
  150.   
  151.     // 設置緩衝區大小  
  152.     public void setBufSize(int bufSize) {  
  153.         this.bufSize = bufSize;  
  154.     }  
  155. }  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章