Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

仿墨跡天氣:在Android中透過自訂zip包換膚

編輯:關於 Android 程式設計

這裡來聊聊墨跡天氣的換膚做法。需要先聲明:以下原理是我透過反編譯並參考網上一些資料推測出來的,僅供參考;如果你有更理想的方式,非常歡迎一起交流。

墨跡天氣下載的皮膚本質上就是一個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:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.tony.skin" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Re_Skin2Activity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

2.版面配置檔main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#d2d2d2" android:id="@+id/layout"> <Button android:text="導入皮膚" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="換膚" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請先點擊“導入皮膚”,會將/sdcard/skin.zip導入到/sdcard/Skin_kris目錄下,然後點擊‘換膚'會將sdcard裡面的素材用作皮膚" android:textColor="#000"></TextView> </LinearLayout> 

3. Re_Skin2Activity:

 package com.tony.skin; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; import com.tony.skin.utils.ZipUtil; /** * * @author Tony * */ public class Re_Skin2Activity extends Activity implements OnClickListener{ private Button btnSet; private Button btnImport; private LinearLayout layout; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSet = (Button)findViewById(R.id.button1); btnSet.setOnClickListener(this); btnImport = (Button)findViewById(R.id.button2); btnImport.setOnClickListener(this); layout = (LinearLayout)findViewById(R.id.layout); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Bitmap bitmap= BitmapFactory.decodeFile("/sdcard/tony/skin/skin.png"); BitmapDrawable bd=new BitmapDrawable(bitmap); btnSet.setBackgroundDrawable(bd); layout.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeFile("/sdcard/Skin_kris/skin/bg/bg.png"))); break; case R.id.button2: ZipUtil zipp = new ZipUtil(2049); System.out.println("begin do zip"); zipp.unZip("/sdcard/skin.zip","/sdcard/Skin_kris"); Toast.makeText(this, "導入成功", Toast.LENGTH_SHORT).show(); break; default: break; } } } 

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

 package com.tony.skin.utils; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.Deflater; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; /** * Zip包壓縮,解壓處理工具類 * @author a * */ public class ZipUtil { private ZipFile zipFile; private ZipOutputStream zipOut; //壓縮Zip private int bufSize; //size of bytes private byte[] buf; private int readedBytes; public ZipUtil(){ this(512); } public ZipUtil(int bufSize){ this.bufSize = bufSize; this.buf = new byte[this.bufSize]; } /** * * @param srcFile 需要 壓縮的目錄或者文件 * @param destFile 壓縮文件的路徑 */ public void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要壓縮的文件夾名 File zipDir; String dirName; zipDir = new File(srcFile); dirName = zipDir.getName(); try { this.zipOut = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(destFile))); //設置壓縮的注釋 zipOut.setComment("comment"); //設置壓縮的編碼,如果要壓縮的路徑中有中文,就用下面的編碼 zipOut.setEncoding("GBK"); //啟用壓縮 zipOut.setMethod(ZipOutputStream.DEFLATED); //壓縮級別為最強壓縮,但時間要花得多一點 zipOut.setLevel(Deflater.BEST_COMPRESSION); handleDir(zipDir, this.zipOut,dirName); this.zipOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * 由doZip調用,遞歸完成目錄文件讀取 * @param dir * @param zipOut * @param dirName 這個主要是用來記錄壓縮文件的一個目錄層次結構的 * @throws IOException */ private void handleDir(File dir, ZipOutputStream zipOut,String dirName) throws IOException { System.out.println("遍歷目錄:"+dir.getName()); FileInputStream fileIn; File[] files; files = dir.listFiles(); if (files.length == 0) {// 如果目錄為空,則單獨創建之. // ZipEntry的isDirectory()方法中,目錄以"/"結尾. System.out.println("壓縮的 Name:"+dirName); this.zipOut.putNextEntry(new ZipEntry(dirName)); this.zipOut.closeEntry(); } else {// 如果目錄不為空,則分別處理目錄和文件. for (File fileName : files) { // System.out.println(fileName); if (fileName.isDirectory()) { handleDir(fileName, this.zipOut,dirName+File.separator+fileName.getName()+File.separator); } else { System.out.println("壓縮的 Name:"+dirName + File.separator+fileName.getName()); fileIn = new FileInputStream(fileName); this.zipOut.putNextEntry(new ZipEntry(dirName + File.separator+fileName.getName())); while ((this.readedBytes = fileIn.read(this.buf)) > 0) { this.zipOut.write(this.buf, 0, this.readedBytes); } this.zipOut.closeEntry(); } } } } /** * 解壓指定zip文件 * @param unZipfile 壓縮文件的路徑 * @param destFile 解壓到的目錄 */ public void unZip(String unZipfile, String destFile) {// unZipfileName需要解壓的zip文件名 FileOutputStream fileOut; File file; InputStream inputStream; try { this.zipFile = new ZipFile(unZipfile); for (Enumeration entries = this.zipFile.getEntries(); entries .hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); file = new File(destFile+File.separator+entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { // 如果指定文件的目錄不存在,則創建之. File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(file); while ((this.readedBytes = inputStream.read(this.buf)) > 0) { fileOut.write(this.buf, 0, this.readedBytes); } fileOut.close(); inputStream.close(); } } this.zipFile.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } // 設置緩沖區大小 public void setBufSize(int bufSize) { this.bufSize = bufSize; } } 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved