Android 打造自己的个性化应用(四):仿墨迹天气实现-->自定义扩展名的zip格式的皮肤

来源:互联网 发布:知乎 复活 编辑:程序博客网 时间:2024/05/17 22:05

在这里谈一下墨迹天气的换肤实现方式,不过首先声明我只是通过反编译以及参考了一些网上其他资料的方式推测出的换肤原理, 在这里只供参考. 若大家有更好的方式, 欢迎交流.

墨迹天气下载的皮肤就是一个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. }  
原创粉丝点击